def type_password_into_password_of_login_form(context, password): wc = WebCommon() password_locator_type = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'login_user_password']['type'] password_locator_string = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'login_user_password']['locator'] wc.type_into_element(context, password, password_locator_type, password_locator_string)
def type_email_into_username_of_login_form(context, email): wc = WebCommon() email_locator_type = locatorsconfig.MY_ACCOUNT_LOCATORS['login_user_name'][ 'type'] email_locator_string = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'login_user_name']['locator'] wc.type_into_element(context, email, email_locator_type, email_locator_string)
def i_get_the_total_dollar_amount_of_the_cart(context): import time time.sleep(2) wc = WebCommon() total_locator = CART_PAGE_LOCATORS['total_cart_value'] total_raw = wc.get_element_text(context, total_locator['type'], total_locator['locator']) context.total = total_raw.replace('£', '')
def click_on_button_name(context, btn_name): wc = WebCommon() if btn_name.lower() in ('login', 'log in'): login_btn_locator_type = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'login_button']['type'] login_btn_locator_string = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'login_button']['locator'] else: raise Exception("Not implemented") wc.click(context, login_btn_locator_type, login_btn_locator_string)
def error_message_with_unknown_email_should_be_displayed(context): wc = WebCommon() expected_msg = "Unknown email address. Check again or try your username." error_box_type = locatorsconfig.MY_ACCOUNT_LOCATORS['error_box']['type'] error_box_text = locatorsconfig.MY_ACCOUNT_LOCATORS['error_box']['locator'] is_exist = wc.element_contains_text(context, expected_msg, error_box_type, error_box_text) if is_exist: print("pass") else: raise Exception("Incorrect message shown for non existing user.")
def verify_nav_bars_visible(context, nav_bar): wc = WebCommon() expected_bars = ['main navigation', 'top navigation', 'options'] if nav_bar not in expected_bars: raise Exception("The passed in nav_bar type is not one of expected." "Actual: {}, Expected in: {}".format( nav_bar, expected_bars)) locator_info = locatorsconfig.LOCATORS.get(nav_bar) locator_type = locator_info['type'] locator_text = locator_info['locator'] nav_element = wc.find_element(context, locator_type, locator_text) wc.is_element_visible(nav_element)
def error_message_with_email_should_be_displayed(context, email): wc = WebCommon() expected_msg = "Error: The password you entered for the email address {email} is incorrect. Lost your password?".format( email=email) error_box_type = locatorsconfig.MY_ACCOUNT_LOCATORS['error_box']['type'] error_box_text = locatorsconfig.MY_ACCOUNT_LOCATORS['error_box']['locator'] is_exist = wc.element_contains_text(context, expected_msg, error_box_type, error_box_text) if is_exist: print("pass") else: raise Exception( "Incorrect message shown on failed log in. Email: {}".format( email))
def i_add_random_item_to_cart_from_the_homepage(context, qty): wc = WebCommon() logger.info("11111") logger.info(f"Adding {qty} items to cart") logger.info(f"Adding {qty} items to cart") cart_btns = HOME_PAGE_LOCATORS['all_add_cart_btns'] cart_btns_type = cart_btns['type'] cart_btns_text = cart_btns['locator'] cart_btns = wc.find_elements(context, cart_btns_type, cart_btns_text) random_btns = random.sample(cart_btns, int(qty)) for i in random_btns: wc.click(i) time.sleep(1)
def and_i_click_on_proceed_to_checkout_button(context): proceed_button = CART_PAGE_LOCATORS['proceed_to_checkout_btn'] wc = WebCommon() max_tries = 3 try_count = 0 while try_count < max_tries: try: wc.click(context, proceed_button['type'], proceed_button['locator']) break except Exception as e: try_count += 1 print( f"failed to click on proceed to checkout. Retry number: {try_count}" ) else: raise Exception( f"failed to click on proceed to checkout button after 3 retries")
def i_select_free_shipping_option(context): wc = WebCommon() logger.info("11111") logger.info("about to select free shipping option") free_ship = CART_PAGE_LOCATORS['free_shipping_radio'] wc.click(context, free_ship['type'], free_ship['locator']) wc.assert_radio_is_selected(context, free_ship['type'], free_ship['locator'])
def i_apply_coupon_to_the_cart(context): coupon_field_locator = CART_PAGE_LOCATORS['coupon_code_field'] apply_coupon_locator = CART_PAGE_LOCATORS['apply_coupon_button'] wc = WebCommon() wc.type_into_element(context, context.coupon_code, coupon_field_locator['type'], coupon_field_locator['locator']) wc.click(context, apply_coupon_locator['type'], apply_coupon_locator['locator'])
def user_should_be_logged_in(context): wc = WebCommon() nav_bar_type = locatorsconfig.MY_ACCOUNT_LOCATORS['left_nav']['type'] nav_bar_text = locatorsconfig.MY_ACCOUNT_LOCATORS['left_nav']['locator'] logout_type = locatorsconfig.MY_ACCOUNT_LOCATORS['left_nav_logout_btn'][ 'type'] logout_text = locatorsconfig.MY_ACCOUNT_LOCATORS['left_nav_logout_btn'][ 'locator'] wc.assert_element_visible(context, nav_bar_type, nav_bar_text) wc.assert_element_visible(context, logout_type, logout_text)
def i_click_on_cart_in_the_top_nav_bar_and_verify_cart_page_opens(context): wc = WebCommon() max_tries = 5 try_count = 0 cart_info_locator = HOME_PAGE_LOCATORS['cart_info_box'] while try_count < max_tries: try: wc.click(context, cart_info_locator['type'], cart_info_locator['locator']) wc.assert_url_contains(context, '/cart/') break except Exception as e: try_count += 1 print(f"failed to click on proceed to cart. Retry number: {try_count}") else: raise Exception(f"failed to click on proceed to cart button after 5 retries")
def users_can_log_in(context): number_of_users = len(context.users_list) wc = WebCommon() for i in range(0, number_of_users): type_email_into_username_of_login_form(context, context.users_list[i][7]) type_password_into_password_of_login_form(context, context.users_list[i][8]) click_on_button_name(context, 'login') registered_locator_type = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_confirmed']['type'] registered_locator_string = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_confirmed']['locator'] wc.assert_element_visible(context, registered_locator_type, registered_locator_string) wc.click(context, registered_locator_type, registered_locator_string)
def verify_homepage_title(context, expected_title): wc = WebCommon() wc.assert_page_title(context, expected_title)
def go_to_page(context, page): wc = WebCommon() print("Navigating to the site: {}".format(page)) context.driver = wc.go_to(context, page)
def type_email_into_username_of_registration_form(context, user_number): #import pdb; pdb.set_trace() converted_user_number = int(user_number) wc = WebCommon() email = context.users_list[converted_user_number][7] password = context.users_list[converted_user_number][8] email_locator_type = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_name']['type'] email_locator_string = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_name']['locator'] wc.type_into_element(context, email, email_locator_type, email_locator_string) password_locator_type = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_password']['type'] password_locator_string = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_password']['locator'] wc.type_into_element(context, password, password_locator_type, password_locator_string) register_locator_type = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_confirm']['type'] register_locator_string = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_confirm']['locator'] wc.click(context, register_locator_type, register_locator_string) registered_locator_type = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_confirmed']['type'] registered_locator_string = locatorsconfig.MY_ACCOUNT_LOCATORS[ 'register_user_confirmed']['locator'] wc.assert_element_visible(context, registered_locator_type, registered_locator_string) wc.click(context, registered_locator_type, registered_locator_string)
def verify_current_url(context, expected_url): wc = WebCommon() wc.assert_current_url(context, expected_url)