def test_page_source(mock_webdriver): selenium_driver = SeleniumDriver() page_source = selenium_driver.page_source() mock_driver = mock_webdriver.Chrome.return_value assert mock_driver.page_source == page_source
def test_quit(mock_webdriver): selenium_driver = SeleniumDriver() selenium_driver.quit() mock_driver = mock_webdriver.Chrome.return_value assert mock_driver.quit.called
def test_get(mock_webdriver): expected_url = "https://wwww.google.com" selenium_driver = SeleniumDriver() selenium_driver.get(expected_url) mock_driver = mock_webdriver.Chrome.return_value mock_driver.get.assert_called_with(expected_url)
def test_get_text_by_id(mock_webdriver): expected_id = "element-id" selenium_driver = SeleniumDriver() text = selenium_driver.get_text_by_id(expected_id) mock_driver = mock_webdriver.Chrome.return_value mock_driver.find_element_by_id.assert_called_with(expected_id) assert mock_driver.find_element_by_id.return_value.text == text
def test_get_text_by_css_selector(mock_webdriver): expected_css_selector = "css-selector" selenium_driver = SeleniumDriver() text = selenium_driver.get_text_by_css_selector(expected_css_selector) mock_driver = mock_webdriver.Chrome.return_value mock_driver.find_element_by_css_selector.assert_called_with( expected_css_selector) assert mock_driver.find_element_by_css_selector.return_value.text == text
def test_get_text_by_class_name(mock_webdriver): expected_class_name = "class-name" selenium_driver = SeleniumDriver() text = selenium_driver.get_text_by_class_name(expected_class_name) mock_driver = mock_webdriver.Chrome.return_value mock_driver.find_element_by_class_name.assert_called_with( expected_class_name) assert mock_driver.find_element_by_class_name.return_value.text == text
def test_get_text_by_id_not_found(mock_webdriver): expected_id = "element-id" mock_driver = mock_webdriver.Chrome.return_value mock_driver.find_element_by_id.side_effect = MagicMock( side_effect=NoSuchElementException('Test')) selenium_driver = SeleniumDriver() text = selenium_driver.get_text_by_id(expected_id) mock_driver.find_element_by_id.assert_called_with(expected_id) assert not text
def test_wait_by_class_name_true(mock_webdriver, mock_webdriverwait): expected_class_name = "class-name" selenium_driver = SeleniumDriver() is_wait = selenium_driver.wait_by_class_name(expected_class_name) mock_driver = mock_webdriver.Chrome.return_value assert mock_webdriverwait.called mock_webdriverwait.assert_called_with(mock_driver, WAIT_TIME) assert mock_webdriverwait.return_value.until.called assert is_wait
def test_wait_by_id_true(mock_webdriver, mock_webdriverwait): expected_id = "element-id" selenium_driver = SeleniumDriver() is_wait = selenium_driver.wait_by_id(expected_id) mock_driver = mock_webdriver.Chrome.return_value assert mock_webdriverwait.called mock_webdriverwait.assert_called_with(mock_driver, WAIT_TIME) assert mock_webdriverwait.return_value.until.called assert is_wait
def test_get_text_by_css_selector_not_found(mock_webdriver): expected_css_selector = "css-selector" mock_driver = mock_webdriver.Chrome.return_value mock_driver.find_element_by_css_selector.side_effect = MagicMock( side_effect=NoSuchElementException('Test')) selenium_driver = SeleniumDriver() text = selenium_driver.get_text_by_css_selector(expected_css_selector) mock_driver.find_element_by_css_selector.assert_called_with( expected_css_selector) assert not text
def test_wait_by_css_selector_true(mock_webdriver, mock_webdriverwait): expected_css_selector = "css-selector" selenium_driver = SeleniumDriver() is_wait = selenium_driver.wait_by_css_selector(expected_css_selector) mock_driver = mock_webdriver.Chrome.return_value assert mock_webdriverwait.called mock_webdriverwait.assert_called_with(mock_driver, WAIT_TIME) assert mock_webdriverwait.return_value.until.called assert is_wait
def test_wait_by_id_false(mock_webdriver, mock_webdriverwait): expected_id = "element-id" mock_webdriverwait.side_effect = MagicMock( side_effect=TimeoutException('Test')) selenium_driver = SeleniumDriver() is_wait = selenium_driver.wait_by_id(expected_id) mock_driver = mock_webdriver.Chrome.return_value assert mock_webdriverwait.called mock_webdriverwait.assert_called_with(mock_driver, WAIT_TIME) assert not is_wait
def test_selenium_driver_initializes(mock_webdriver, mock_options): expected_is_headless = IS_HEADLESS == "True" expected_user_agent = f'--user-agent={USER_AGENT}' selenium_driver = SeleniumDriver() options_instance = mock_options.return_value options_instance.add_argument.assert_any_call("--start-maximized") options_instance.add_argument.assert_any_call("--kiosk") options_instance.add_argument.assert_any_call("--disable-dev-shm-usage") options_instance.add_argument.assert_any_call("--no-sandbox") options_instance.add_argument.assert_any_call("--no-sandbox") options_instance.add_argument.assert_any_call(expected_user_agent) assert options_instance.headless == expected_is_headless assert options_instance.binary_location == GOOGLE_CHROME_BIN assert selenium_driver.options == options_instance mock_webdriver.Chrome.assert_called_with(options=options_instance, executable_path=CHROMEDRIVER_PATH) assert selenium_driver.driver == mock_webdriver.Chrome.return_value
from src.dao.selenium_driver import SeleniumDriver if __name__ == "__main__": selenium_driver = SeleniumDriver() driver = selenium_driver.driver driver.get("https://www.google.com") assert "Google" in driver.page_source selenium_driver.quit() print("Selenium with Chrome configured correctly")