def test_remote_remove_capabilities_config_after_setting_desired( self, webdriver_create_mock, browser_create_mock): """Test `remote` method. Note: Test removing `capabilities` after setting `desired_capabilities` in remote browser config with this empty property. """ selenium_config = { 'REMOTE': { 'desired_capabilities': {}, 'capabilities': { 'driver_name': DEFAULT_BROWSER } } } mock_config = MagicMock(name='selenium_config') mock_config.get.side_effect = selenium_config.get mock_config.__contains__.side_effect = selenium_config.__contains__ s = Selenium(mock_config) s.remote(DEFAULT_BROWSER) self.assertNotIn('capabilities', mock_config.get('REMOTE'))
def test_firefox(self): par1 = Mock() par2 = Mock() mock = MagicMock().return_value("conf") mock.browser = drivers.FIREFOX s = Selenium(mock) with self.assertRaises(WebDriverException) as context: s.firefox()
def test_firefox_return_browser(self, webdriver_create_mock, browser_create_mock): """Test `firefox` method. Note: Test returning browser by `firefox` method. """ selenium_config = MagicMock() s = Selenium(selenium_config) browser = browser_create_mock.return_value self.assertEqual(browser, s.firefox())
def test_phantomjs_return_browser(self, webdriver_create_mock, browser_create_mock): """Test `phantomjs` method. Note: Test returning browser by `phantomjs` method. """ selenium_config = MagicMock() s = Selenium(selenium_config) browser = browser_create_mock.return_value self.assertEqual(browser, s.phantomjs())
def test_stop_check_is_running_equals_false(self): """Test `stop` method. Note: Test that `is_running` attribute after stop equals `False`. """ mock_selenium_config = MagicMock() s = Selenium(mock_selenium_config) setattr(s, '_Selenium__is_running', True) setattr(s, '_Selenium__browser', MagicMock()) s.stop() self.assertFalse(s.is_running)
def test_phantomjs_browser_creating(self, webdriver_create_mock, browser_create_mock): """Test `phantomjs` method. Note: Test passing args to create browser. """ selenium_config = MagicMock() s = Selenium(selenium_config) webdriver = webdriver_create_mock.return_value browser_create_mock.return_value(None) s.phantomjs() browser_create_mock.assert_called_once_with(s, webdriver)
def test_start_check_calling_remote_browser(self, mock_waiting_for): """"Test `start` method. Note: Test calling remote browser when passing `USE_REMOTE` true flag in selenium config file. """ selenium_config = {'USE_REMOTE': True} mock = MagicMock() mock.get.side_effect = selenium_config.get s = Selenium(mock) s.start() args, kwargs = mock_waiting_for.call_args self.assertEqual(kwargs['args'][0].__name__, 'remote')
def test_opera_with_empty_opera_config(self): """Test `opera` method. Note: Test to throw exception when opera config in general selenium config exists but is empty. """ selenium_config = {'OPERA': {}} mock = MagicMock() mock.get.side_effect = selenium_config.get s = Selenium(mock) with self.assertRaises(SeleniumExError) as context: s.opera() self.assertIn('settings for opera browser is not found in config', context.exception)
def test_phantomjs_with_lack_phantomjs_config(self): """Test `phantomjs` method. Note: Test to throw exception when phantomjs config in general selenium config doesn't exist. """ selenium_config = {} mock = MagicMock() mock.get.side_effect = selenium_config.get s = Selenium(mock) with self.assertRaises(SeleniumExError) as context: s.phantomjs() self.assertIn('settings for phantom js is not found in config', context.exception)
def test_firefox_with_empty_firefox_config(self): """Test `firefox` method. Note: Test to throw exception when firefox config in general selenium config exists but is empty. """ selenium_config = {'FIREFOX': {}} mock = MagicMock() mock.get.side_effect = selenium_config.get s = Selenium(mock) with self.assertRaises(SeleniumExError) as context: s.firefox() self.assertIn('settings for firefox browser is not found in config', context.exception)
def test_chrome_with_lack_chrome_config(self): """Test `chrome` method. Note: Test to throw exception when chrome config in general selenium config doesn't exist. """ selenium_config = {} mock = MagicMock() mock.get.side_effect = selenium_config.get s = Selenium(mock) with self.assertRaises(SeleniumExError) as context: s.chrome() self.assertIn('settings for chrome browser is not found in config', context.exception)
def test_remote_set_desired_capabilities_config(self, webdriver_create_mock, browser_create_mock): """Test `remote` method. Note: Test setting `desired_capabilities` in remote browser config with this empty property. """ selenium_config = {'REMOTE': {'desired_capabilities': {}}} mock_config = MagicMock(name='selenium_config') mock_config.get.side_effect = selenium_config.get s = Selenium(mock_config) s.remote(DEFAULT_BROWSER) self.assertEqual( mock_config.get('REMOTE').get('desired_capabilities'), DRIVER_TO_CAPABILITIES[DEFAULT_BROWSER])
def test_remote_with_empty_remote_config(self): """Test `remote` method. Note: Test to throw exception when remote browser config in general selenium config exists but is empty. """ selenium_config = {'REMOTE': {}} mock_config = MagicMock(name='selenium_config') mock_config.get.side_effect = selenium_config.get mock_driver = MagicMock(name='driver_name') s = Selenium(mock_config) with self.assertRaises(SeleniumExError) as context: s.remote(mock_driver) self.assertIn('settings for remote is not found in config', context.exception)
def test_stop_check_close_and_quit_executed(self): """Test `stop` method. Note: Test that `close` and `quit` methods were executed. """ selenium_config = MagicMock() selenium = Selenium(selenium_config) setattr(selenium, '_Selenium__is_running', True) browser = MagicMock() browser.close = MagicMock() browser.quit = MagicMock() setattr(selenium, '_Selenium__browser', browser) selenium.stop() self.assertTrue(browser.close.called) self.assertTrue(browser.quit.called)
def setUp(self): selenium_config = { 'PAGE_LOAD_TIMEOUT': None, 'SCRIPT_TIMEOUT': None, 'WAIT_TIMEOUT': None, 'WINDOW_SIZE': None, 'MAXIMIZE_WINDOW': None } mock_selenium_config = MagicMock() mock_selenium_config.get.side_effect = selenium_config.get self.selenium = Selenium(mock_selenium_config) self.driver = MagicMock()
def setUp(self): self.selenium_config = { 'WAIT_TIMEOUT': 1488, 'POLLING_DELAY': 42, 'POLLING_TIMEOUT': 1407, 'PAGE_LOAD_TIMEOUT': 11235813 } mock_selenium_config = MagicMock() mock_selenium_config.get.side_effect = self.selenium_config.get selenium = Selenium(mock_selenium_config) driver = MagicMock(spec=WebDriver) browser_config = BrowserConfig(selenium, driver) self.browser = WebDriverProxy(driver, browser_config)
def test_create_check_set_reason_storage(self): """Test function `create`. Note: Test setting `browser_name` for `reason_storage`. """ with patch('seismograph.ext.selenium.proxy.WebDriverProxy' ) as mock_driver_proxy: d = dict() browser = MagicMock() browser.reason_storage.__setitem__.side_effect = d.__setitem__ browser.reason_storage.__getitem__.side_effect = d.__getitem__ mock_driver_proxy.return_value = browser selenium = Selenium(MagicMock()) setattr(selenium, '_Selenium__browser_name', DEFAULT_BROWSER) driver = MagicMock(spec=WebDriver) returned_browser = create(selenium, driver) self.assertEqual(returned_browser.reason_storage['browser name'], DEFAULT_BROWSER)
def test_stop(self): mock = MagicMock().return_value("conf") mock.browser = "IE" s = Selenium(mock) s.stop() self.assertFalse(s.is_running)
def test_remote(self): mock = MagicMock().return_value("REMOTE") mock.browser = drivers.OPERA s = Selenium(mock) with self.assertRaises(WebDriverException) as context: s.remote(drivers.OPERA)
def test_chrome(self): mock = MagicMock().return_value("conf") mock.browser = drivers.CHROME s = Selenium(mock) with self.assertRaises(WebDriverException) as context: s.chrome()
def test_start_SeleniumExError(self): mock = MagicMock().return_value("conf") mock.browser = "IE" s = Selenium(mock) with self.assertRaises(SeleniumExError) as context: s.start("IE", 1, 1)
def test_phantomjs(self): mock = MagicMock().return_value("conf") mock.browser = drivers.PHANTOMJS s = Selenium(mock) with self.assertRaises(WebDriverException) as context: s.phantomjs()
class SeleniumStartMethodTestCase(unittest.TestCase): """Tests for `Selenium` class `start` method. """ def setUp(self): selenium_config = MagicMock() self.selenium = Selenium(selenium_config) def tearDown(self): pass @patch('seismograph.ext.selenium.extension.waiting_for') def test_start_check_setting_browser_name(self, mock_waiting_for): """Test for `start` method. Note: Test to set `browser_name` via passing arg in this method. """ mock_waiting_for.return_value = None browser_name = 'chrome' self.selenium.start(browser_name=browser_name) self.assertEqual(self.selenium._Selenium__browser_name, browser_name) @patch('seismograph.ext.selenium.extension.waiting_for') def test_start_check_setting_delay(self, mock_waiting_for): """Test for `start` method. Note: Test to set `delay` and pass like arg to `waiting_for` function. """ mock_waiting_for.return_value = None browser_delay = 7 self.selenium.start(delay=browser_delay) args, kwargs = mock_waiting_for.call_args self.assertEqual(kwargs['delay'], browser_delay) @patch('seismograph.ext.selenium.extension.waiting_for') def test_start_check_setting_timeout(self, mock_waiting_for): """Test for `start` method. Note: Test to set `timeout` and pass like arg to `waiting_for` function. """ mock_waiting_for.return_value = None browser_timeout = 88 self.selenium.start(timeout=browser_timeout) args, kwargs = mock_waiting_for.call_args self.assertEqual(kwargs['timeout'], browser_timeout) @patch('seismograph.ext.selenium.extension.waiting_for') def test_start_check_is_running_equals_true(self, mock_waiting_for): """Test `start` method. Note: Test that `is_running` class attribute equals `True` after executing `start` method. """ mock_waiting_for.return_value = None self.selenium.start() self.assertTrue(self.selenium.is_running)
def setUp(self): selenium_config = MagicMock() self.selenium = Selenium(selenium_config)
def test_opera(self): mock = MagicMock().return_value("conf") mock.browser = drivers.OPERA s = Selenium(mock) with self.assertRaises(WebDriverException) as context: s.opera()