def generic_language_switcher_should_be_set_to(context: Context, language: str): language_code = Language[language.upper()].value with assertion_msg("Required dictionary with page views is missing"): assert hasattr(context, "views") views = context.views page_names = ( [row["page"] for row in context.table] if context.table else views.keys() ) results = defaultdict() for page_name in page_names: response = views[page_name] content = response.content.decode("utf-8") check_for_errors(content, response.url) selector = f"#great-header-language-select option[selected]::attr(value)" selected_language_switcher_option = ( Selector(text=content).css(selector).extract() ) error = f"Couldn't find language switcher on {response.url}" with assertion_msg(error): assert selected_language_switcher_option selected_language_switcher_option = selected_language_switcher_option[0] results[page_name] = selected_language_switcher_option logging.debug(f"Selected language in Language Switcher on: {dict(results)}") undetected_languages = { page: selected_language_switcher_option for page, selected_language_switcher_option in results.items() if language_code not in selected_language_switcher_option } with assertion_msg( f"'{language}' was not selected in Language Switcher for following pages: {undetected_languages}" ): assert not undetected_languages
def generic_page_language_should_be_set_to(context: Context, language: str): language_code = Language[language.upper()].value with assertion_msg("Required dictionary with page views is missing"): assert hasattr(context, "views") views = context.views page_names = ( [row["page"] for row in context.table] if context.table else views.keys() ) results = defaultdict() for page_name in page_names: response = views[page_name] content = response.content.decode("utf-8") check_for_errors(content, response.url) html_tag_language = Selector(text=content).css("html::attr(lang)").extract()[0] results[page_name] = html_tag_language logging.debug(f"HTML tag language attributes for: {dict(results)}") undetected_languages = { page: html_tag_lang for page, html_tag_lang in results.items() if language_code not in html_tag_lang } with assertion_msg( f"HTML document language was not set to '{language_code}' in following pages: {undetected_languages}" ): assert not undetected_languages
def erp_should_see_number_of_product_categories_to_expand( context: Context, actor_alias: str, comparison_description: str): comparison_details = get_comparison_details(comparison_description) page = get_last_visited_page(context, actor_alias) has_action(page, "should_see_number_of_product_categories_to_expand") take_screenshot(context.driver, "should_see_number_of_product_categories_to_expand") check_for_errors(context.driver.page_source, context.driver.current_url) page.should_see_number_of_product_categories_to_expand( context.driver, comparison_details) logging.debug( f"{actor_alias} saw: {comparison_description} product categories(s) to expand" )
def can_find_supplier_by_term(session: Session, company_title: str, term: str, term_type: str, max_pages: int = 5) -> (str, Response): """ :param session: Buyer's session object :param company_title: sought Supplier name :param term: a text used to find the Supplier :param term_type: type of the term, e.g.: product, service, keyword etc. :param max_pages: maximum number of search results pages to go through :return: a tuple with search result (True/False), last search Response and an endpoint to company's profile """ response = fas.search.go_to(session, term=term) check_for_errors(response.content.decode("UTF-8"), response.url) profile_link = fas.search.find_profile_link(response, company_title) if profile_link: logging.debug( f"SEARCH: Found Supplier '{company_title}' on the first FAS search result page. " f"Search was done using '{term_type}': '{term}'") return profile_link, response number_of_pages = get_number_of_search_result_pages(response) if number_of_pages < 2: logging.debug( f"SEARCH: Search results returned {number_of_pages} pages") return profile_link, response page_number = 2 while page_number <= max_pages: logging.debug( f"SEARCH: page {page_number} out of {number_of_pages} profile_link={profile_link}" ) logging.debug( f"SEARCH: Checking search result page number: {page_number}") response = fas.search.go_to(session, term=term, page=page_number) check_for_errors(response.content.decode("UTF-8"), response.url) profile_link = fas.search.find_profile_link(response, company_title) if profile_link: logging.debug( f"SEARCH: Breaking out of vicious search loop as company " f"'{company_title}' was found using '{term_type}': '{term}' on" f" {response.url}") break page_number += 1 return profile_link, response
def should_be_on_page(context: Context, actor_alias: str, page_name: str): page = get_page_object(page_name) page_source = context.driver.page_source revisit_page_on_access_denied(context.driver, page, page_name) take_screenshot(context.driver, f"should be on {page_name}") check_for_errors(page_source, context.driver.current_url) accept_all_cookies(context.driver) has_action(page, "should_be_here") if hasattr(page, "SubURLs"): special_page_name = page_name.split(" - ")[1].lower() if signature(page.should_be_here).parameters.get("page_name"): page.should_be_here(context.driver, page_name=special_page_name) else: raise TypeError( f"{page.__name__}.should_be_here() doesn't accept 'page_name' keyword " f"argument but it should as this Page Object has 'SubURLs' attribute." ) else: page.should_be_here(context.driver) update_actor(context, actor_alias, visited_page=page) logging.debug( f"{actor_alias} is on {page.SERVICE} - {page.NAME} - {page.TYPE} -> " f"{page}")
def should_be_on_working_page(context: Context, actor_alias: str): check_for_errors(context.driver.page_source, context.driver.current_url) logging.debug(f"{actor_alias} is on {context.driver.current_url}")
def generic_content_of_viewed_pages_should_in_selected_language( context: Context, language: str, *, page_part: str = None, probability: float = 0.9 ): """Check if all viewed pages contain content in expected language NOTE: This requires all responses with page views to be stored in context.views :param context: behave `context` object :param language: expected language of the view FAS page content :param page_part: detect language of the whole page or just the main part :param probability: expected probability of expected language """ with assertion_msg("Required dictionary with page views is missing"): assert hasattr(context, "views") views = context.views page_names = ( [row["page"] for row in context.table] if context.table else views.keys() ) if page_part: if page_part == "main": main = True elif page_part == "whole": main = False else: raise KeyError("Please select valid part of the page: main or whole") else: main = False if language.lower() == "chinese": expected_language_code = "zh-cn" elif language.lower() == "english": expected_language_code = "en" else: expected_language_code = Language[language.upper()].value results = defaultdict() for page_name in page_names: response = views[page_name] content = response.content.decode("utf-8") check_for_errors(content, response.url) logging.debug(f"Detecting the language of '{page_name}'' page {response.url}") lang_detect_results = detect_page_language(page_name, "", content, main=main) median_results = { language: median(probabilities) for language, probabilities in lang_detect_results.items() } results[page_name] = median_results undetected_languages = { page: medians for page, medians in results.items() if expected_language_code not in medians } with assertion_msg( f"Could not detect '{expected_language_code}' in page content on following pages: {undetected_languages}" ): assert not undetected_languages unmet_probabilities = { page: medians for page, medians in results.items() if medians[expected_language_code] < probability } with assertion_msg( f"Median '{expected_language_code}' language detection probability of " f"{probability} was not met on following pages: {unmet_probabilities}" ): assert not unmet_probabilities