def check_if_populated(driver: WebDriver, expected_form_details: dict): for key, expected_value in expected_form_details.items(): existing_field_selector = PREPOPULATED_FORM_FIELDS.get(key, None) if not existing_field_selector: continue existing_field = find_element(driver, existing_field_selector, element_name=key) existing_field_value = existing_field.get_attribute("value") if expected_value: error = ( f"Expected '{key}' value to be '{expected_value}' but got " f"'{existing_field_value}'") if existing_field_value.lower() == expected_value.lower(): with assertion_msg(error): assert existing_field_value.lower( ) == expected_value.lower() logging.debug( f"'{key}' field was prepopulated with expected value: " f"'{expected_value}'") else: with assertion_msg(error): assert existing_field_value.lower( ) in expected_value.lower() logging.debug( f"Prepopulated '{key}' field is part of expected value: " f"'{expected_value}'")
def should_see_page_number(driver: WebDriver, page_num: int): scroll_to(driver, find_element(driver, ACTIVE_PAGE)) take_screenshot(driver, NAME) selector = find_element(driver, ACTIVE_PAGE) with assertion_msg(f"Expected to see {page_num} but got {int(selector.text)}"): assert int(selector.text) == page_num
def should_see_it_on(driver: WebDriver, page: ModuleType): take_screenshot(driver, NAME + page.NAME) selector = find_selector_by_name(page.SELECTORS, "language selector") language_selector = find_element(driver, selector, element_name="language selector") with assertion_msg(f"Language selector is not visible on {driver.current_url}"): assert language_selector.is_displayed() logging.debug(f"Language selector is visible on {driver.current_url}")
def has_pagination(driver: WebDriver, min_page_num: int): scroll_to(driver, find_element(driver, PAGINATION)) take_screenshot(driver, NAME) selectors = find_elements(driver, PAGES) with assertion_msg( f"Expected to see more that {min_page_num} search results page but got just {len(selectors)}" ): assert len(selectors) > min_page_num
def check_if_populated(driver: WebDriver, shared_url: str): status_update_message = find_element(driver, MESSAGE_BOX) with assertion_msg( "Expected to see article URL '%s' in LinkedIn's status update " "textbox, but couldn't find it in : %s", shared_url, status_update_message.text, ): assert shared_url in status_update_message.text
def check_if_populated(driver: WebDriver, shared_url: str): found_shared_url = extract_shared_url(driver.current_url) with assertion_msg( "Expected to find link to Article '%s' in the Facebook share page " "URL, but got '%s' instead", shared_url, found_shared_url, ): assert shared_url == found_shared_url
def should_see_content_for(driver: WebDriver, region_name: str): source = driver.page_source region_name = clean_name(region_name) logging.debug("Looking for: {}".format(region_name)) with assertion_msg( "Expected to find term '%s' in the source of the page %s", region_name, driver.current_url, ): assert region_name.lower() in source.lower()
def navigate_through_links_with_keyboard(driver: WebDriver, page: ModuleType): selector = find_selector_by_name(page.SELECTORS, "language selector") language_selector = find_element(driver, selector, element_name="language selector") options = language_selector.find_elements_by_tag_name("option") for _ in options: language_selector.send_keys(Keys.DOWN) for _ in options: language_selector.send_keys(Keys.UP) with assertion_msg(f"Language selector is not visible on {driver.current_url}"): assert language_selector.is_displayed()
def check_if_link_opens_email_client(driver: WebDriver): share_button_selector = SHARE_BUTTONS["email"] share_button = find_element(driver, share_button_selector) href = share_button.get_attribute("href") with assertion_msg( "Expected the 'share via email' link to open in Email Client, but " "got a invalid link: %s", href, ): assert href.startswith("mailto:")
def check_if_populated(driver: WebDriver, shared_url: str): found_shared_url = extract_shared_url(driver) with assertion_msg( "Expected to find link to Article '%s' in the LinkedIn share page " "URL, but got '%s' instead", shared_url, found_shared_url, ): assert shared_url == found_shared_url logging.debug( f"Link to share page on LinkedIn contains expected {shared_url}")
def check_page_language_is(driver: WebDriver, expected_language: str): expected_language_code = LANGUAGE_INDICATOR_VALUES[expected_language] language_selector = find_element(driver, LANGUAGE_SELECTOR) options = language_selector.find_elements_by_tag_name("option") selected = [option for option in options if option.is_selected()][0] with assertion_msg( "Expected to see page in '%s' but got '%s'", expected_language_code, selected.get_attribute("value"), ): assert selected.get_attribute("value") == expected_language_code
def check_if_link_opens_new_tab(driver: WebDriver, social_media: str): share_button_selector = SHARE_BUTTONS[social_media.lower()] share_button = find_element(driver, share_button_selector) target = share_button.get_attribute("target") with assertion_msg( "Expected link to '%s' share page to open in new tab, but instead " "found a link with target attribute set to '%s'", social_media, target, ): assert target == "_blank"
def check_share_via_email_link_details(driver: WebDriver, expected_subject: str, expected_body: str): share_button_selector = SHARE_BUTTONS["email"] share_button = find_element(driver, share_button_selector) href = share_button.get_attribute("href") parsed_url = urlparse.urlparse(href) query_parameters = urlparse.parse_qs(parsed_url.query) subject = query_parameters["subject"][0] body = query_parameters["body"][0] with assertion_msg( "Expected 'share via email' link to contain message body '%s' but " "got '%s' instead", expected_body, body, ): assert body == expected_body with assertion_msg( "Expected 'share via email' link's message subject to contain " "Article title '%s' but got '%s' instead", expected_subject, subject, ): assert expected_subject in subject
def click_on_result_of_type(driver: WebDriver, type_of: str): results = find_elements(driver, SEARCH_RESULTS) results_of_matching_type = [ result for result in results if result.find_element_by_css_selector("span.type").text.lower() == type_of.lower() ] with assertion_msg( f"Expected to see at least 1 search result of type '{type_of}' but found none" ): assert results_of_matching_type logging.debug(f"Found {len(results_of_matching_type)} results of type '{type_of}'") result = random.choice(results_of_matching_type) result_link = result.find_element_by_css_selector("a") logging.debug( f"Will click on {result_link.text} -> {result_link.get_property('href')}" ) result_link.click()
def should_see_marketplaces(driver: WebDriver, country: str): expected_countries = [country, "Global"] markets_selector = Selector(By.CSS_SELECTOR, "div.market-item-inner") marketplace_countries = { marketplace.find_element_by_tag_name("a") .text: marketplace.find_element_by_css_selector( "div.market-item-inner p.market-operating-countries" ) .text for marketplace in find_elements(driver, markets_selector) } error = f"Found marketplace without a list of countries it operates in" assert marketplace_countries, error for marketplace, countries in marketplace_countries.items(): with assertion_msg( f"{marketplace} does not operate in '{country}' or Globally!" f"but in '{countries}' instead" ): assert any(country in countries for country in expected_countries) logging.debug( f"{marketplace} operates in '{country}' or Globally! -> {countries}" )
def should_see_link_to(driver: WebDriver, section: str, item_name: str): item_selector = SELECTORS[section.lower()][item_name.lower()] menu_item = find_element(driver, item_selector, element_name=item_name) with assertion_msg("It looks like '%s' in '%s' section is not visible", item_name, section): assert menu_item.is_displayed()