From d3b17c1dd5aed0b4e86e7a9243f5ae83cec15f13 Mon Sep 17 00:00:00 2001 From: Svetlana Levinsohn Date: Sat, 18 Feb 2023 12:25:44 -0800 Subject: [PATCH] Lesson 4 --- features/steps/amazon_main_page_steps.py | 50 ++++++++++++++++++++++++ features/steps/search_result_steps.py | 17 ++++++++ features/tests/amazon_main.feature | 10 +++++ features/tests/amazon_search.feature | 36 +++++++++++++++-- 4 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 features/steps/amazon_main_page_steps.py create mode 100644 features/steps/search_result_steps.py create mode 100644 features/tests/amazon_main.feature diff --git a/features/steps/amazon_main_page_steps.py b/features/steps/amazon_main_page_steps.py new file mode 100644 index 000000000..d44c65646 --- /dev/null +++ b/features/steps/amazon_main_page_steps.py @@ -0,0 +1,50 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then + + +AMAZON_SEARCH_FIELD = (By.ID, 'twotabsearchtextbox') +SEARCH_ICON = (By.ID, 'nav-search-submit-button') +HAM_MENU = (By.ID, 'nav-hamburger-menu') +FOOTER_LINKS = (By.CSS_SELECTOR, "table.navFooterMoreOnAmazon td.navFooterDescItem") +HEADER_LINKS = (By.CSS_SELECTOR, "#nav-xshop a.nav-a[data-csa-c-type='link']") + + +@given('Open Amazon page') +def open_amazon(context): + context.driver.get('https://www.amazon.com/') + + +@when('Input text {text}') +def input_search_word(context, text): + context.driver.find_element(*AMAZON_SEARCH_FIELD).send_keys(text) + + +@when('Click on search button') +def click_search(context): + context.driver.find_element(*SEARCH_ICON).click() + + +@then('Verify hamburger menu icon present') +def verify_ham_menu_present(context): + context.driver.find_element(*HAM_MENU) + # print(element) + + +@then('Verify that footer has {expected_amount} links') +def verify_footer_link_count(context, expected_amount): + print('Original Type: ', type(expected_amount)) # '42' + expected_amount = int(expected_amount) + print('Type after converting: ', type(expected_amount)) # => 42 + + footer_links = context.driver.find_elements(*FOOTER_LINKS) + print(footer_links) + print('\nLink count: ', len(footer_links)) + # assert 42 == 42 + assert len(footer_links) == expected_amount, f'Expected {expected_amount} links, but got {len(footer_links)}' + + +@then('Verify that header has {expected_amount} links') +def verify_header_link_count(context, expected_amount): + expected_amount = int(expected_amount) + header_links = context.driver.find_elements(*HEADER_LINKS) + assert len(header_links) == expected_amount, f'Expected {expected_amount} links but got {len(header_links)}' \ No newline at end of file diff --git a/features/steps/search_result_steps.py b/features/steps/search_result_steps.py new file mode 100644 index 000000000..235d3c559 --- /dev/null +++ b/features/steps/search_result_steps.py @@ -0,0 +1,17 @@ +from selenium.webdriver.common.by import By +from behave import when, then +from time import sleep + +PRODUCT_PRICE = (By.XPATH, "//div[@data-component-type='s-search-result']//a[.//span[@class='a-price']]") + + +@when('Click on the first product') +def click_first_product(context): + context.driver.find_element(*PRODUCT_PRICE).click() + sleep(2) + + +@then('Verify that text {expected_result} is shown') +def verify_search_result(context, expected_result): + actual_result = context.driver.find_element(By.XPATH, "//span[@class='a-color-state a-text-bold']").text + assert expected_result == actual_result, f'Expected {expected_result} but got actual {actual_result}' diff --git a/features/tests/amazon_main.feature b/features/tests/amazon_main.feature new file mode 100644 index 000000000..0d176b7e8 --- /dev/null +++ b/features/tests/amazon_main.feature @@ -0,0 +1,10 @@ +Feature: Amazon main page tests + + Scenario: User can see hamburger menu + Given Open Amazon page + Then Verify hamburger menu icon present + + Scenario: Footer and header has correct amount of links + Given Open amazon page + Then Verify that footer has 42 links + Then Verify that header has 29 links \ No newline at end of file diff --git a/features/tests/amazon_search.feature b/features/tests/amazon_search.feature index 02a3c7bfa..a98955839 100644 --- a/features/tests/amazon_search.feature +++ b/features/tests/amazon_search.feature @@ -1,14 +1,42 @@ # Created by svetlanalevinsohn at 2/11/23 Feature: Amazon search tests - Scenario: User can search for coffee on Amazon + Scenario Outline: User can search for coffee on Amazon Given Open Amazon page - When Input text coffee + When Input text And Click on search button - Then Verify that text "coffee" is shown + Then Verify that text is shown + Examples: + |search_word |search_result | + |coffee |"coffee" | + |table |"table" | + |mug |"mug" | Scenario: User can search for a table on Amazon Given Open Amazon page When Input text table When Click on search button - Then Verify that text "table" is shown \ No newline at end of file + Then Verify that text "table" is shown +# +# Scenario: User can search for a mug on Amazon +# Given Open Amazon page +# When Input text mug +# When Click on search button +# Then Verify that text "mug" is shown + + Scenario: User can add a product to the cart + Given Open Amazon page + When Input text Tritan Farm to Table Pitcher + When Click on search button + And Click on the first product + And Click on Add to cart button + And Open cart page + Then Verify cart has 1 item(s) + +# Scenario: User can click on a hamburger menu +# Given Open Amazon page +# Then Verify hamburger menu icon present +# +# Scenario: Footer has correct amount of links +# Given Open amazon page +# Then Verify that footer has 38 links \ No newline at end of file