def test_element_handles_index_error(): driver = MockDriver() find = Finder(driver, None) element = find.element(PageElement(By.CLASS_NAME, "")) assert_that(element, equal_to(None), "None should be returned when no elements can be found")
def test_elements(): driver = MockDriver() find = Finder(driver, None) elements = find.elements(PageElement(By.ID, "test-id")) assert_that(elements[0].info, equal_to("elements found by 'id' using value 'test-id'"), "An element list should be found")
def test_number_of_elements(): elements_to_return = [MockElement(), MockElement(), MockElement()] driver = MockDriver(elements_to_return) find = Finder(driver, None) num_elements = find.number_of_elements(PageElement(By.CLASS_NAME, "")) assert_that(num_elements, equal_to(3), "Incorrect number of elements returned")
def test_element(): driver = MockDriver() find = Finder(driver, None) element = find.element(PageElement(By.CLASS_NAME, "test-class")) assert_that( element.info, equal_to("elements found by 'class name' using value 'test-class'"), "An element should be found")
def test_elements_logs_out_info(): driver = MockDriver() logger = MagicMock() find = Finder(driver, logger) find.elements(PageElement(By.ID, "test-id")) assert_that(logger.info.call_count, equal_to(2), "Two messages should have been logged") logger.info.assert_any_call( "Looking for elements matching PageElement id='test-id'") logger.info.assert_any_call("Found 1 element(s)")
def test_switch_to_frame(): mock_driver = MagicMock(name="driver") finder = Finder(mock_driver, MagicMock(name="logger")) interrogator = Interrogator(mock_driver, finder, "logger") waiter = Waiter(mock_driver, finder, "logger") test_interactor = Interactor(mock_driver, finder, interrogator, waiter, "logger") frame_to_find = PageElement(By.ID, "frame_id") test_interactor.switch_to_frame(frame_to_find) mock_driver.switch_to.frame.assert_called_once_with( finder.element(frame_to_find))
def test_visible_elements_returns_correct_elements(): elements_to_return = [ MockElement("", True, "false"), MockElement("", False, "false"), MockElement("", True, "true"), MockElement("", False, "true") ] driver = MockDriver(elements_to_return) find = Finder(driver, None) visible_elements = find.visible_elements( PageElement(By.CLASS_NAME, "test-class")) assert_that(len(visible_elements), equal_to(1), "Incorrect number of elements returned")
def test_get_value_from_cookie_with_wrong_key_value(): finder = Finder("driver", "logger") test_interrogator = Interrogator(MockDriver(), finder, "logger") name = "nhsuk-consent" result = test_interrogator.get_value_from_cookie(name) assert_that(result, equal_to(""), f"Incorrect cookie value when searching for name: '{name}'")
def test_get_value_from_cookie_with_correct_value(): finder = Finder("driver", "logger") test_interrogator = Interrogator(MockDriver(), finder, "logger") name = "cookie-consent" result = test_interrogator.get_value_from_cookie(name) assert_that(result, equal_to("%7B%22preferences%22%3Atrue%7D"), f"Incorrect cookie value when searching for name: '{name}'")
def test_accept_alert(): mock_driver = MagicMock(name="driver") finder = Finder(mock_driver, "logger") interrogator = Interrogator(mock_driver, finder, "logger") waiter = Waiter(mock_driver, finder, "logger") test_interactor = Interactor(mock_driver, finder, interrogator, waiter, "logger") test_interactor.accept_alert() mock_driver.switch_to.alert.accept.assert_called_once()
def test_enter_text_into_alert(): mock_driver = MagicMock(name="driver") finder = Finder(mock_driver, "logger") interrogator = Interrogator(mock_driver, finder, "logger") waiter = Waiter(mock_driver, finder, "logger") test_interactor = Interactor(mock_driver, finder, interrogator, waiter, "logger") text = "my text value" test_interactor.enter_text_into_alert(text) mock_driver.switch_to.alert.send_keys.assert_called_once_with(text)
def __init__(self, driver, existing_logger=None, wait_time=10): """ Default constructor which passes the control of webDriver to the current page :param driver: the Selenium web driver :param existing_logger: logger object used to save information to a log file, None by default :param wait_time: number of seconds as an Integer, defaults to 10 """ self.driver = driver self.logger = existing_logger or logging.getLogger(__name__) self.implicit_wait = wait_time self.find = Finder(driver, existing_logger) self.wait = Waiter(driver, self.find, wait_time, existing_logger) self.interrogate = Interrogator(driver, self.find, existing_logger) self.interact = Interactor(driver, self.find, self.interrogate, self.wait, existing_logger)