def test_information_input(driver, browser, mode, device, username, password): """Test the name and postal code input boxes on the checkout page.""" # Proceed to checkout page login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_page.click_cart() cart_page = CartPage(driver) cart_page.click_checkout() # Try to continue without filling out information checkout_page = CheckoutPage(driver) checkout_page.click_continue() assert checkout_page.is_error_message_present() # Input information first_name = "Testy" last_name = "McTest" postal_code = "08221" checkout_page.input_first_name(first_name) checkout_page.input_last_name(last_name) checkout_page.input_postal_code(postal_code) checkout_page.input_payment_details() # Check data was successfully inserted and we can continue assert checkout_page.get_first_name() == first_name assert checkout_page.get_last_name() == last_name assert checkout_page.get_postal_code() == postal_code checkout_page.click_continue() overview_page = OverviewPage(driver) assert overview_page.get_subheader() == "Checkout: Overview"
def test_plp_prices(driver, browser, mode, device, username, password): """ Check that the PLP prices match the PDP prices. """ login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_elements = product_page.get_all_product_elements() product_link_map = {} product_price_map = {} for product in product_elements: product_name = product.find_element_by_class_name("inventory_item_name").text product_url = product.find_element_by_xpath(".//div[2]/a").get_attribute("href") product_price = product.find_element_by_class_name("inventory_item_price").text product_link_map[product_name] = product_url product_price_map[product_name] = product_price bad_prices = [] for product, link in product_link_map.items(): driver.get(link) pdp_page = ProductDetailPage(driver) pdp_price = pdp_page.get_price() if pdp_price != product_price_map[product]: print("The price for {0} was not correct: {1}".format(product, pdp_price)) bad_prices.append(product) time.sleep(3) assert len(bad_prices) == 0, str(len(bad_prices)) + " incorrect prices were found on the PLP."
def test_sort_a_to_z(driver, browser, mode, device, username, password): """Test sorting the PLP's products in alphabetical order.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_page.sort_products_a_to_z() product_names = product_page.get_list_of_product_names() for i in range(len(product_names)-1): assert product_names[i] <= product_names[i+1], "Products {0} and {1} are not ordered correctly.".format(product_names[i], product_names[i+1]) print("test_sort_a_to_z finished successfully.")
def test_get_products(driver, browser, mode, device, username, password): """Test getting all product names from the PLP.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_names = product_page.get_list_of_product_names() for name in product_names: print("PLP has product with name: %s" % name) assert len(product_names) > 0, "No products found on the PLP." print("test_get_products finished successfully.")
def test_checkout_button(driver, browser, mode, device, username, password): """Test the cart page's checkout button.""" # Note: SauceDemo intentionally allows you to checkout with no items. This case is explicitly # tested in test_cart_page.py in test_checkout_no_items() login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_page.click_cart() cart_page = CartPage(driver) cart_page.click_checkout()
def test_continue_shopping_button(driver, browser, mode, device, username, password): """Test the cart page's continue shopping button.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_page.click_cart() cart_page = CartPage(driver) cart_page.click_continue_shopping() assert len(product_page.get_list_of_product_names()) > 0
def test_sort_high_to_low(driver, browser, mode, device, username, password): """Test sorting the PLP's products by price in descending order.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_page.sort_products_high_to_low() product_prices = product_page.get_list_of_product_prices() for i in range(len(product_prices)-1): assert product_prices[i] >= product_prices[i+1], \ "Products {0} and {1} are not ordered correctly.".format(product_prices[i], product_prices[i+1]) print("test_sort_high_to_low finished successfully.")
def test_cancel_button(driver, browser, mode, device, username, password): """Test the checkout page's cancel button.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_page.click_cart() cart_page = CartPage(driver) cart_page.click_checkout() checkout_page = CheckoutPage(driver) checkout_page.click_cancel() assert cart_page.get_subheader() == "Your Cart"
def test_checkout_all_items(driver, browser, mode, device, username, password): """Test the checkout process with all items on the PLP.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_list_page = ProductListPage(driver) add_to_cart_buttons = product_list_page.get_add_to_cart_buttons() num_items_in_cart = 0 assert product_list_page.get_number_cart_items() == num_items_in_cart, \ "An unexpected item has been found in the cart." # Add products, check mini cart total. for button in add_to_cart_buttons: button.click() num_items_in_cart += 1 assert num_items_in_cart == product_list_page.get_number_cart_items(), \ "An unexpected item has been found in the cart." time.sleep(1) assert button.text == REMOVE assert product_list_page.get_number_cart_items() == len(add_to_cart_buttons), \ "Unexpected number of cart items" product_list_page.click_cart() # Continue to the checkout page from the cart page. cart_page = CartPage(driver) cart_total = cart_page.get_sum_prices() cart_page.click_checkout() # Attempt to checkout without entering a name and postal code. checkout_page = CheckoutPage(driver) checkout_page.click_continue() assert checkout_page.is_error_message_present() # Checkout with valid information first_name = "Testy" last_name = "McTest" postal_code = "08221" checkout_page.input_first_name(first_name) checkout_page.input_last_name(last_name) checkout_page.input_postal_code(postal_code) checkout_page.input_payment_details() assert checkout_page.get_first_name() == first_name assert checkout_page.get_last_name() == last_name assert checkout_page.get_postal_code() == postal_code checkout_page.click_continue() # Check the overview page before completing the order. overview_page = OverviewPage(driver) assert overview_page.get_subheader() == "Checkout: Overview" assert cart_total == overview_page.get_subtotal() overview_page.click_finish() # Check the order confirmation page. order_confirmation_page = OrderConfirmationPage(driver) assert order_confirmation_page.get_subheader() == "Finish" assert product_list_page.get_number_cart_items( ) == 0, "Unexpected item found in cart."
def test_add_to_cart_button(driver, browser, mode, device, username, password): """Test the PDP's add to cart button.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_list_page = ProductListPage(driver) # Get a random product products = product_list_page.get_all_product_elements() index = random.randrange(0, len(products)) product = products[index] product_url = product.find_element_by_xpath( product_list_page.URL_XPATH).get_attribute("href") driver.get(product_url) pdp = ProductDetailPage(driver) pdp.click_add_to_cart() assert pdp.get_number_cart_items() == 1
def test_add_to_cart(driver, browser, mode, device, username, password): """Test adding items to the cart.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_elements = product_page.get_all_product_elements() num_items_in_cart = 0 assert product_page.get_number_cart_items() == num_items_in_cart, "An unexpected item has been found in the cart." # Add products, check mini cart total. for product in product_elements: add_cart_button = product.find_element_by_xpath(product_page.ADD_TO_CART_BUTTON_XPATH) add_cart_button.click() num_items_in_cart += 1 assert num_items_in_cart == product_page.get_number_cart_items(), "An unexpected item has been found in the cart." time.sleep(1) assert add_cart_button.text == Constants.REMOVE # Remove products, check mini cart total. for product in product_elements: remove_cart_button = product.find_element_by_xpath(product_page.ADD_TO_CART_BUTTON_XPATH) remove_cart_button.click() num_items_in_cart -= 1 assert num_items_in_cart == product_page.get_number_cart_items(), "An unexpected item has been found in the cart." time.sleep(1) assert remove_cart_button.text == Constants.ADD_TO_CART
def test_checkout_one_item(driver, browser, mode, device, username, password): """Test the checkout process with one item.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_list_page = ProductListPage(driver) add_to_cart_buttons = product_list_page.get_add_to_cart_buttons() num_items_in_cart = 0 assert product_list_page.get_number_cart_items() == num_items_in_cart, \ "An unexpected item has been found in the cart." item_button = random.choice(add_to_cart_buttons) item_button.click() assert item_button.text == REMOVE assert product_list_page.get_number_cart_items() == 1, \ "An unexpected item has been found in the cart." # Continue to the cart page product_list_page.click_cart() cart_page = CartPage(driver) cart_total = cart_page.get_sum_prices() cart_page.click_checkout() checkout_page = CheckoutPage(driver) checkout_page.click_continue() assert checkout_page.is_error_message_present() # Input checkout information. first_name = "Testy" last_name = "McTest" postal_code = "08221" checkout_page.input_first_name(first_name) checkout_page.input_last_name(last_name) checkout_page.input_postal_code(postal_code) checkout_page.input_payment_details( ) # A real e-commerce site would allow you to add payment info. assert checkout_page.get_first_name() == first_name assert checkout_page.get_last_name() == last_name assert checkout_page.get_postal_code() == postal_code checkout_page.click_continue() # Check the overview page before completing the order. overview_page = OverviewPage(driver) assert overview_page.get_subheader() == "Checkout: Overview" assert cart_total == overview_page.get_subtotal() overview_page.click_finish() # Check the order confirmation page. order_confirmation_page = OrderConfirmationPage(driver) assert order_confirmation_page.get_subheader() == "Finish" assert product_list_page.get_number_cart_items( ) == 0, "Unexpected item found in cart."
def test_plp_links(driver, browser, mode, device, username, password): """ Test to ensure that the PLP product link properly redirects to the associated PDP.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_elements = product_page.get_all_product_elements() product_link_map = {} for product in product_elements: product_name = product.find_element_by_class_name("inventory_item_name").text product_url = product.find_element_by_xpath(".//div[2]/a").get_attribute("href") product_link_map[product_name] = product_url bad_links = [] for product, link in product_link_map.items(): driver.get(link) pdp_page = ProductDetailPage(driver) pdp_product_name = pdp_page.get_product_name() if pdp_product_name != product: print("The PLP link for {0} was not correct: {1}".format(product, link)) bad_links.append(product) assert len(bad_links) == 0, str(len(bad_links)) + " incorrect links were found on the PLP."
def test_plp_images(driver, browser, mode, device, username, password): """Test that each product's image link is not broken.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_elements = product_page.get_all_product_elements() image_urls = [] for product in product_elements: image = product.find_element_by_xpath(".//div[1]/a/img") image_urls.append(image.get_attribute("src")) broken_image_links = [] for url in image_urls: driver.get(url) errors = driver.find_elements_by_css_selector( "#webkit-xml-viewer-source-xml > Error > Message" ) if len(errors) > 0: print("Image source not found:" + url) broken_image_links.append(url) print(broken_image_links) assert len(broken_image_links) == 0, str(len(broken_image_links)) + " image sources not found."
def test_checkout_no_items(driver, browser, mode, device, username, password): """Checkout with no items. SwagLabs allows this even when logging in with standard_user.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_list_page = ProductListPage(driver) product_list_page.click_cart() # Continue to cart page with no items. cart_page = CartPage(driver) cart_page.click_checkout() checkout_page = CheckoutPage(driver) checkout_page.click_continue() assert checkout_page.is_error_message_present() # Enter name and postal code first_name = "Testy" last_name = "McTest" postal_code = "08221" checkout_page.input_first_name(first_name) checkout_page.input_last_name(last_name) checkout_page.input_postal_code(postal_code) checkout_page.input_payment_details() assert checkout_page.get_first_name() == first_name assert checkout_page.get_last_name() == last_name assert checkout_page.get_postal_code() == postal_code checkout_page.click_continue() # Check the overview page before completing the order. overview_page = OverviewPage(driver) assert overview_page.get_subheader() == "Checkout: Overview" overview_page.click_finish() # Check the order confirmation page. order_confirmation_page = OrderConfirmationPage(driver) assert order_confirmation_page.get_subheader() == "Finish" assert product_list_page.get_number_cart_items( ) == 0, "Unexpected item found in cart."
def test_remove_from_cart(driver, browser, mode, device, username, password): """Test the cart page's remove from cart button.""" login_page = LoginPage(driver) login_page.perform_complete_login(username, password) product_page = ProductListPage(driver) product_page.add_all_to_cart() product_page.click_cart() cart_page = CartPage(driver) cart_page.remove_all_from_cart()