def setUp(self, *args):
     redis_lock.reset_all(get_redis_connection("default"))
     self.service = BrowserService()
class BrowserServiceTest(TestCase):
    """
    Test for BrowserService
    """
    @mock.patch('selenium.webdriver.Chrome')
    def setUp(self, *args):
        redis_lock.reset_all(get_redis_connection("default"))
        self.service = BrowserService()

    def test_init_chrome_raise_exception(self, mock_chrome):
        mock_chrome.side_effect = [Exception(), mock.Mock()]
        BrowserService()
        self.assertEqual(2, mock_chrome.call_count)

    def test_delete_instance_with_exception(self, *args):
        self.service.screen = None
        self.service.browser = None

        del self.service
        self.assertTrue(True)

    def test_when_delete_instance_relase_lock(self, *args):
        mock_lock = mock.Mock()
        self.service.browser_lock = mock_lock
        del self.service
        mock_lock.release.assert_called_once_with()

    @mock.patch('os.remove')
    def test_when_use_tmp_path_delete_instance_will_remove_folder(self, mock_remove, *args):
        self.service.browser_lock = None
        path = self.service.browser_user_path
        del self.service
        mock_remove.assert_called_once_with(path)

    def test_get(self, *args):
        self.service.browser = mock.Mock()
        self.service.get('abc')
        self.service.browser.get.assert_called_once_with('abc')
        self.service.browser.execute_script.assert_called_once_with(self.service.jquery)

    @mock.patch.object(EC, 'presence_of_element_located', return_value=mock.Mock(return_value='element1'))
    def test_find_element(self, mock_method, *args):
        self.assertEqual('element1', self.service.find_element('abc'))

    @mock.patch.object(EC, 'element_to_be_clickable', return_value=mock.Mock(return_value='element2'))
    def test_find_element_visible_and_clickable(self, mock_method, *args):
        self.assertEqual('element2', self.service.find_element_visible_and_clickable('abc'))

    @mock.patch.object(EC, 'presence_of_all_elements_located', return_value=mock.Mock(return_value='element3'))
    def test_find_elements(self, mock_method, *args):
        self.assertEqual('element3', self.service.find_elements('abc'))

    def test_fill_input(self, *args):
        mock_element = mock.Mock()
        self.service.fill_input(mock_element, 123)
        mock_element.clear.assert_called_once_with()
        mock_element.send_keys.assert_called_once_with(123)

    def test_select_checkbox_from_uncheck_to_check(self, *args):
        mock_element = mock.Mock()
        mock_element.is_selected.return_value = False
        self.service.select_checkbox(mock_element)
        mock_element.click.assert_called_once_with()

    def test_select_checkbox_from_check_to_uncheck(self, *args):
        mock_element = mock.Mock()
        mock_element.is_selected.return_value = True
        self.service.select_checkbox(mock_element, value=False)
        mock_element.click.assert_called_once_with()

    def test_execute_script(self, *args):
        self.service.browser = mock.Mock()
        self.service.execute_script('abc')
        self.service.browser.execute_script.assert_called_once_with('abc')