def open_any_tag(driver: WebDriver) -> str: links = find_elements(driver, TAGS) assert len(links) > 0 link = links[random.randint(1, len(links)) - 1] tag = link.text.lower() link.click() return tag
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 should_see_filtered_results(driver: WebDriver, expected_filters: List[str]): def to_filter_format(name): return name.upper().replace(" ", "_").replace("-", "_") formatted_expected_filters = list(map(to_filter_format, expected_filters)) show_filters(driver) sector_filters = find_elements(driver, SECTOR_FILTERS) checked_sector_filters = [ sector_filter.get_attribute("value") for sector_filter in sector_filters if sector_filter.get_attribute("checked") ] number_expected_filters = len(formatted_expected_filters) number_checked_filters = len(checked_sector_filters) with assertion_msg( f"Expected to see {number_expected_filters} sector filter(s) to be checked but " f"saw {number_checked_filters}"): assert number_checked_filters == number_expected_filters diff = list(set(formatted_expected_filters) - set(checked_sector_filters)) with assertion_msg( f"Couldn't find '{diff}' among checked filters: {checked_sector_filters}" ): assert not diff
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_any_article(driver: WebDriver) -> str: article_links = find_elements(driver, ADVICE_ARTICLE_LINKS) link = random.choice(article_links) link_text = link.text check_if_element_is_visible(link, element_name=link_text) with wait_for_page_load_after_action(driver): link.click() return link_text
def should_see_number_of_product_categories_to_expand( driver: WebDriver, comparison_details: Tuple[BuiltinFunctionType, int]): found_elements = find_elements(driver, PRODUCT_CATEGORIES_SELECTOR) evaluate_comparison( "number of product categories (on {driver.current_url}) to be", len(found_elements), comparison_details, )
def open_profile(driver: WebDriver, number: int): profile_links = find_elements(driver, PROFILE_LINKS) if number == 0: link = random.choice(profile_links) elif number == 1: link = profile_links[0] else: link = profile_links[0] logging.debug(f"Will click on profile link: {link.text}") link.click() take_screenshot(driver, NAME + " after clicking on company profile link")
def click_on_random_search_result(driver: WebDriver, result_type: str): result_type_selectors = { "code": PRODUCT_CODES_SELECTOR, "category": PRODUCT_CATEGORIES_SELECTOR, } found_elements = find_elements(driver, result_type_selectors[result_type.lower()]) search_result = choice(found_elements) value = search_result.get_property("value") href = search_result.get_attribute("href") logging.debug(f"Will click on {result_type}: {value or href}") with wait_for_page_load_after_action(driver, timeout=5): search_result.click()
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 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 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 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 drill_down_hierarchy_tree( driver: WebDriver, *, use_expanded_category: bool = False) -> Tuple[ModuleType, dict]: if use_expanded_category: last_expanded_level = Selector( By.CSS_SELECTOR, "li.app-hierarchy-tree__parent--open:last-of-type") last_opened_levels = find_elements(driver, last_expanded_level) opened_first_level = last_opened_levels[-1] first_id = opened_first_level.get_property("id") logging.debug( f"Commencing from: {first_id} -> {opened_first_level.text}") else: first_level_selector = Selector( By.CSS_SELECTOR, "ul.app-hierarchy-tree li.app-hierarchy-tree__section") first_level = find_elements(driver, first_level_selector) first = choice(first_level) first_id = first.get_property("id") logging.debug(f"First level: {first_id} -> {first.text}") click_and_wait(driver, first) select_code_selector = Selector(By.CSS_SELECTOR, "button[name=product-search-commodity]") is_select_product_button_present = is_element_present( driver, select_code_selector) logging.debug( f"Is Select product code button present: {is_select_product_button_present}" ) current_parent_id = first_id while not is_select_product_button_present: child_level_selector = Selector( By.CSS_SELECTOR, f"#{current_parent_id} ul li.app-hierarchy-tree__chapter") child_level = find_elements(driver, child_level_selector) if not child_level: logging.debug("No more child level elements") break logging.debug( f"Child elements of '{current_parent_id}' are: {child_level}") child = choice(child_level) current_parent_id = child.get_property("id") logging.debug(f"Selected child: {current_parent_id}") click_and_wait(driver, child) is_button_present = is_element_present(driver, select_code_selector) logging.debug( f"Is Select product code button present: {is_button_present}") is_select_product_button_present = is_button_present if is_select_product_button_present: select_codes = find_elements(driver, select_code_selector) select = choice(select_codes) selected_code_value = select.get_attribute("value") try: selected_code_value = json.loads(selected_code_value) except JSONDecodeError: pass logging.debug(f"Selected product code: {selected_code_value}") click_and_wait(driver, select) else: logging.error("Strange! Could not find 'Select' product codes button") selected_code_value = {} return product_detail, selected_code_value
def open_random_marketplace(driver: WebDriver): selector = Selector(By.CSS_SELECTOR, "#random-markets a.card-link") links = find_elements(driver, selector) random.choice(links).click()
def open_random_marketplace(driver: WebDriver): selector = Selector(By.CSS_SELECTOR, "div.market-item-inner a.market-header-link") link = random.choice(find_elements(driver, selector)) logging.debug(f"Clicked on {link.text}") link.click()
def open_news_article(driver: WebDriver, article_number: int): article_links = find_elements(driver, ARTICLES) assert len(article_links) >= article_number article_links[article_number - 1].click()