def js_country_select(driver: WebDriver, *, value): """Select random country from an input field with JS autocompletion. This callback works on following pages: * HPO contact form - high-potential-opportunities/contact/ """ option_values = extract_attributes_by_css( driver.page_source, "#js-country-select-select option", attrs=["value"], text=True, ) # ignore options with no value and flatten the dict to a list option_values = [item["text"] for item in option_values if item["value"]] logging.debug(f"Available countries: {option_values}") value = random.choice(option_values) logging.debug(f"Selected country: {value}") js_field = find_element(driver, Selector(By.ID, "js-country-select")) js_field.click() js_field.clear() js_field.send_keys(value) first_suggestion_selector = Selector( By.CSS_SELECTOR, "#js-country-select__listbox li:nth-child(1)") first_suggestion = find_element(driver, first_suggestion_selector, wait_for_it=True) first_suggestion.click()
def search(driver: WebDriver, *, keyword: str = None, sector: str = None): input_field = find_element(driver, SEARCH_INPUT, element_name="Search input field", wait_for_it=False) input_field.clear() if keyword: input_field.send_keys(keyword) if sector: sector_dropdown = find_element( driver, SEARCH_SECTOR, element_name="Sector dropdown menu", wait_for_it=False, ) sector_value = "option[value='{}']".format(sector.upper().replace( " ", "_")) sector_option = sector_dropdown.find_element_by_css_selector( sector_value) sector_option.click() take_screenshot(driver, NAME + " after entering the keyword") button = find_element(driver, SEARCH_BUTTON, element_name="Search button", wait_for_it=False) button.click() take_screenshot(driver, NAME + " after submitting the search form")
def fill_out(driver: WebDriver, email: str, password: str): email_input = find_element(driver, EMAIL_INPUT) password_input = find_element(driver, PASSWORD_INPUT) email_input.clear() email_input.send_keys(email) password_input.clear() password_input.send_keys(password)
def search(driver: WebDriver, phrase: str): search_input = find_element(driver, find_selector_by_name(SELECTORS, "search box")) search_button = find_element( driver, find_selector_by_name(SELECTORS, "search button")) search_input.clear() search_input.send_keys(phrase) search_button.click()
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_form_choices(driver: WebDriver, names: List[str]): radio_selectors = get_selectors(SELECTORS["form"], ElementType.RADIO) for name in names: radio_selector = radio_selectors[name.lower()] find_element(driver, radio_selector, element_name=name, wait_for_it=False) logging.debug( f"All expected form choices: '{names}' are visible on " f"{driver.current_url}" )
def should_see_office_details(driver: WebDriver, trade_office: str, city: str): office_selector = find_selector_by_name(SELECTORS, "office name") office = find_element(driver, office_selector) error = f"Expected to find details for '{trade_office}' but got {office.text}" assert trade_office.lower() in office.text.lower(), error address_selector = find_selector_by_name(SELECTORS, "office address") address = find_element(driver, address_selector) error = (f"Expected to find details for trade office in '{city}' but got " f"{address.text}") assert city.lower() in address.text.lower(), error
def should_see_content_for(driver: WebDriver, industry_name: str): industry_name = clean_name(industry_name) logging.debug("Looking for: {}".format(industry_name)) industry_breadcrumb = find_element( driver, INDUSTRY_BREADCRUMB, element_name="Industry breadcrumb", wait_for_it=False, ) current_industry = industry_breadcrumb.text with assertion_msg( "Expected to see breadcrumb for '%s' industry but got '%s' instead" " on %s", industry_name, current_industry, driver.current_url, ): assert industry_name == current_industry source = driver.page_source with assertion_msg( "Expected to find term '%s' in the source of the page %s", industry_name, driver.current_url, ): from html import escape assert escape(industry_name) in source
def autocomplete_company_name(driver: WebDriver): autocomplete = Selector(By.CSS_SELECTOR, "ul.SelectiveLookupDisplay", is_visible=True) find_element(driver, autocomplete, element_name="Autocomplete", wait_for_it=True) options = find_elements( driver, Selector(By.CSS_SELECTOR, "li[role='option']", is_visible=True)) option = random.choice(options) logging.debug( f"Autocomplete selected country name: {option.get_attribute('data-value')} {option.text}" ) option.click()
def autocomplete_country(driver: WebDriver, *, value): if isinstance(value, bool) and not value: logging.debug(f"Won't use autocomplete") return if isinstance(value, bool): logging.debug(f"Will select random country to type") options = extract_by_css( driver.page_source, "select[id^=id] option::text", first=False ) logging.debug(f"Available country options: {options}") value = random.choice(options) logging.debug(f"Will select '{value}' from country autocomplete list") # enter text value into the input field with autocomplete input_selector = Selector( By.CSS_SELECTOR, "input.autocomplete__input", is_visible=True ) input = find_element( driver, input_selector, element_name="country input", wait_for_it=False ) input.click() input.send_keys(value) logging.debug(f"Get list of options from autocomplete listbox") autocomplete_list = find_elements( driver, Selector(By.CSS_SELECTOR, "ul[role=listbox] li", is_visible=True) ) input.send_keys(Keys.DOWN) random.choice(autocomplete_list).send_keys(Keys.SPACE)
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 search(driver: WebDriver, *, keyword: str = None, sector: str = None): """ sector is not used, but kept for compatibility with search() in other POs. """ input_field = find_element( driver, SEARCH_INPUT, element_name="Search input field", wait_for_it=False ) input_field.clear() if keyword: input_field.send_keys(keyword) take_screenshot(driver, NAME + " after entering the keyword") button = find_element( driver, SEARCH_BUTTON, element_name="Search button", wait_for_it=False ) button.click() take_screenshot(driver, NAME + " after submitting the search form")
def open(driver: WebDriver, page: ModuleType, *, with_keyboard: bool = False): selector = find_selector_by_name(page.SELECTORS, "language selector") language_selector = find_element(driver, selector, element_name="language selector") if with_keyboard: language_selector.send_keys(Keys.ENTER) else: language_selector.click()
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 share_via(driver: WebDriver, social_media: str): share_button_selector = SHARE_BUTTONS[social_media.lower()] share_button = find_element(driver, share_button_selector) href = share_button.get_attribute("href") logging.debug("Opening 'Share on %s' link '%s' in the same tab", social_media, href) driver.get(href)
def enrol_autocomplete_company_name(driver: WebDriver, value: str = None): """Value is ignored as we want to choose random company name""" autocomplete = Selector(By.CSS_SELECTOR, "ul.SelectiveLookupDisplay", is_visible=True) find_element(driver, autocomplete, element_name="Autocomplete", wait_for_it=True) options = find_elements(driver, Selector(By.CSS_SELECTOR, "li[role='option']")) option = random.choice(options) logging.debug( f"Selected company: {option.get_attribute('data-value')} {option.text}" ) option.click()
def open_guide(driver: WebDriver, guide_name: str): guide_name = guide_name.split(" - ")[1].strip() selector = Selector(By.PARTIAL_LINK_TEXT, guide_name) logging.debug("Looking for: {}".format(guide_name)) guide = find_element(driver, selector, element_name="Guide card", wait_for_it=False) guide.click() take_screenshot(driver, PAGE_TITLE + " after opening " + guide_name)
def get_form_details(driver: WebDriver) -> dict: elements = find_elements(driver, SELECTED_REGIONS) result = {"regions": []} for element in elements: region_name = element.get_property("value") result["regions"].append(region_name) result["organisation name"] = "AUTOMATED TESTS" result["given mane"] = find_element( driver, find_selector_by_name(SELECTORS, "given name") ).get_property("value") result["family mane"] = find_element( driver, find_selector_by_name(SELECTORS, "family name") ).get_property("value") result["email"] = find_element( driver, find_selector_by_name(SELECTORS, "email") ).get_property("value") return result
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 open_article(driver: WebDriver, number: int): selector = Selector(By.CSS_SELECTOR, ARTICLE_LINK.format(number=number)) link = find_element(driver, selector) if link.get_attribute("target") == "_blank": href = link.get_attribute("href") driver.get(href) else: link.click() take_screenshot(driver, NAME + " after clicking on article link")
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 extract_shared_url(driver: WebDriver) -> str: link = find_element(driver, LINKEDIN, element_name="Share on LinkedIn link") url = link.get_attribute("href") parsed_url = urlparse.urlparse(url) query_parameters = urlparse.parse_qs(parsed_url.query) shared_url = query_parameters["url"][0] return shared_url
def check_dit_favicon(driver: WebDriver): try: favicon = find_element(driver, FAVICON, element_name="Favicon", wait_for_it=False) except NoSuchElementException: try: favicon = find_element(driver, EXOPPS_FAVICON, element_name="Favicon", wait_for_it=False) except NoSuchElementException: raise src = favicon.get_attribute("href") check_hash_of_remote_file(MD5_CHECKSUM_DIT_FAVICON, src) logging.debug("Favicon %s has correct MD5sum %s", src, MD5_CHECKSUM_DIT_FAVICON)
def open_industry(driver: WebDriver, industry_name: str): industry_name = industry_name.split(" - ")[1].strip() selector = Selector(By.PARTIAL_LINK_TEXT, industry_name) logging.debug("Looking for: {}".format(industry_name)) industry_link = find_element( driver, selector, element_name="Industry card", wait_for_it=False ) industry_link.click() take_screenshot(driver, NAME + " after opening " + industry_name + " page")
def open_guide(driver: WebDriver, guide_name: str): guide_name = clean_name(guide_name) selector = Selector(By.PARTIAL_LINK_TEXT, guide_name) logging.debug("Looking for: {}".format(guide_name)) guide = find_element(driver, selector, element_name="Guide card", wait_for_it=False) guide.click()
def open_industry(driver: WebDriver, industry_name: str): industry_name = clean_name(industry_name) selector = Selector(By.PARTIAL_LINK_TEXT, industry_name) logging.debug("Looking for: {}".format(industry_name)) industry_link = find_element(driver, selector, element_name="Industry card", wait_for_it=False) industry_link.click()
def autocomplete_industry(driver: WebDriver, *, value): if isinstance(value, bool) and not value: logging.debug(f"Won't use autocomplete") return if isinstance(value, bool): logging.debug(f"Will select random industry to type") options = extract_by_css( driver.page_source, "#id_imported-products-usage-imported_good_sector-select option::text", first=False, ) logging.debug(f"Available country options: {options}") value = random.choice(options) logging.debug(f"Will select '{value}' from Industry Autocomplete list") # enter text value into the input field with autocomplete input_selector = Selector( By.ID, "id_imported-products-usage-imported_good_sector", is_visible=True ) input = find_element( driver, input_selector, element_name="industry input", wait_for_it=True ) input.click() input.send_keys(value) logging.debug(f"Get list of options from autocomplete listbox") autocomplete_list = find_element( driver, Selector( By.ID, "id_imported-products-usage-imported_good_sector__listbox", is_visible=False, ), wait_for_it=True, ) autocomplete_list_options = autocomplete_list.find_elements(By.TAG_NAME, "li") with assertion_msg(f"Expected to find at least 1 region suggestion but got 0"): assert autocomplete_list_options logging.debug(f"Selecting random element from the autocomplete listbox") option = random.choice(autocomplete_list_options) option.click()
def search(driver: WebDriver, country: str, category: str): form_selectors = SELECTORS["search form"] find_a_marketplace = find_element(driver, SEARCH_BUTTON, element_name="find a marketplace") scroll_to(driver, find_a_marketplace) details = generate_form_details(country, category) pick_option(driver, form_selectors, form_details=details) find_a_marketplace.click() take_screenshot(driver, "After submitting the form")
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:")