From 9d92732b4122be46f3bd229439b8005438514e26 Mon Sep 17 00:00:00 2001 From: Svetlana Levinsohn Date: Sat, 24 Sep 2022 12:10:10 -0700 Subject: [PATCH 1/2] Lesson3, code --- css_selectors.py | 38 +++++++++++++++++++++ features/environment.py | 2 +- features/steps/amazon_main_page_steps.py | 15 ++++++++ features/steps/search_results_page_steps.py | 8 +++++ features/tests/amazon_search.feature | 12 +++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 css_selectors.py create mode 100644 features/steps/amazon_main_page_steps.py create mode 100644 features/steps/search_results_page_steps.py create mode 100644 features/tests/amazon_search.feature diff --git a/css_selectors.py b/css_selectors.py new file mode 100644 index 000000000..bffa9a978 --- /dev/null +++ b/css_selectors.py @@ -0,0 +1,38 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By + +driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/12-python-selenium-automation/chromedriver') + +# By tag & ID +driver.find_element(By.CSS_SELECTOR, "input#twotabsearchtextbox") +# By ID +driver.find_element(By.CSS_SELECTOR, "#twotabsearchtextbox") +driver.find_element(By.ID, "twotabsearchtextbox") + +# By class +driver.find_element(By.CSS_SELECTOR, ".icp-nav-flag-us") +# By multiple classes +driver.find_element(By.CSS_SELECTOR, ".icp-nav-flag-us.icp-nav-flag") +# By class + tag +driver.find_element(By.CSS_SELECTOR, "span.icp-nav-flag-us.icp-nav-flag") + +# By attribute: +driver.find_element(By.CSS_SELECTOR, "input[name='email']") +driver.find_element(By.CSS_SELECTOR, "[name='email']") +driver.find_element(By.CSS_SELECTOR, "[href='/gp/help/customer/display.html/ref=ap_signin_notification_condition_of_use?ie=UTF8&nodeId=508088']") +# By multiple attributes: +driver.find_element(By.CSS_SELECTOR, "input[name='email'][type='email'][maxlength='128']") +# By attribute + class +driver.find_element(By.CSS_SELECTOR, "input.a-button-input[type='submit']") +driver.find_element(By.CSS_SELECTOR, "input.a-button-input.class[type='submit'][]") +driver.find_element(By.CSS_SELECTOR, ".a-button-input.class[type='submit'][]") +# By partial attribute +driver.find_element(By.CSS_SELECTOR, "a[href*='ap_signin_notification_condition_of_use']") +# For classes, to get partial match: +driver.find_element(By.CSS_SELECTOR, "a[class*='button']") + +# From parent => child +driver.find_element(By.CSS_SELECTOR, "div#legalTextRow a[href*='condition']") + +# Xpath backwards (from child to parent) +driver.find_element(By.XPATH, "//*[./a[contains(@href, 'signin_notification_condition_of_use')]]") \ No newline at end of file diff --git a/features/environment.py b/features/environment.py index d9d7a6ac2..2cdaadfb1 100755 --- a/features/environment.py +++ b/features/environment.py @@ -5,7 +5,7 @@ def browser_init(context): """ :param context: Behave context """ - context.driver = webdriver.Chrome() + context.driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/12-python-selenium-automation/chromedriver') # context.browser = webdriver.Safari() # context.browser = webdriver.Firefox() diff --git a/features/steps/amazon_main_page_steps.py b/features/steps/amazon_main_page_steps.py new file mode 100644 index 000000000..5cf5ee781 --- /dev/null +++ b/features/steps/amazon_main_page_steps.py @@ -0,0 +1,15 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then + + +@given('Open amazon page') +def open_amazon(context): + context.driver.get('https://www.amazon.com/') + + +@when('Search for {product}') +def search_product(context, product): + element = context.driver.find_element(By.ID, 'twotabsearchtextbox') + element.clear() + element.send_keys(product) + context.driver.find_element(By.ID, 'nav-search-submit-button').click() diff --git a/features/steps/search_results_page_steps.py b/features/steps/search_results_page_steps.py new file mode 100644 index 000000000..a0885d878 --- /dev/null +++ b/features/steps/search_results_page_steps.py @@ -0,0 +1,8 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then + + +@then('Search results for {expected_result} are shown') +def verify_search_results(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'Error! Expected {expected_result}, but got {actual_result}' diff --git a/features/tests/amazon_search.feature b/features/tests/amazon_search.feature new file mode 100644 index 000000000..0d067a332 --- /dev/null +++ b/features/tests/amazon_search.feature @@ -0,0 +1,12 @@ +# Created by svetlanalevinsohn at 9/24/22 +Feature: Tests for amazon search + + Scenario: User can search for coffee + Given Open amazon page + When Search for coffee + Then Search results for "coffee" are shown + + Scenario: User can search for mug + Given Open amazon page + When Search for mug + Then Search results for "mug" are shown From fdf07f42b6c62273f1f89d64e3a5021dda536d0e Mon Sep 17 00:00:00 2001 From: Svetlana Levinsohn Date: Sat, 1 Oct 2022 12:20:47 -0700 Subject: [PATCH 2/2] Lesson 4 --- features/steps/amazon_main_page_steps.py | 18 ++++++++++++++++ features/tests/amazon_main.feature | 10 +++++++++ features/tests/amazon_search.feature | 27 +++++++++++++++++++++--- 3 files changed, 52 insertions(+), 3 deletions(-) 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 index 5cf5ee781..da11927a0 100644 --- a/features/steps/amazon_main_page_steps.py +++ b/features/steps/amazon_main_page_steps.py @@ -1,6 +1,9 @@ from selenium.webdriver.common.by import By from behave import given, when, then +HAM_MENU = (By.ID, 'nav-hamburger-menu') +FOOTER_LINKS = (By.CSS_SELECTOR, '.navFooterDescItem a') + @given('Open amazon page') def open_amazon(context): @@ -13,3 +16,18 @@ def search_product(context, product): element.clear() element.send_keys(product) context.driver.find_element(By.ID, 'nav-search-submit-button').click() + + +@then('Verify hamburger menu is present') +def verify_ham_menu_present(context): + context.driver.find_element(*HAM_MENU) + + +@then('Verify that footer has {expected_link_count} links') +def verify_link_count(context, expected_link_count): + expected_link_count = int(expected_link_count) + + links = context.driver.find_elements(*FOOTER_LINKS) + + assert len(links) == expected_link_count, \ + f'Expected {expected_link_count} links, but got {len(links)}' diff --git a/features/tests/amazon_main.feature b/features/tests/amazon_main.feature new file mode 100644 index 000000000..f6988bad9 --- /dev/null +++ b/features/tests/amazon_main.feature @@ -0,0 +1,10 @@ +# Created by svetlanalevinsohn at 10/1/22 +Feature: Tests for Amazon main page + + Scenario: Hamburger menu is present + Given Open amazon page + Then Verify hamburger menu is present + + Scenario: Footer has correct amount of links + Given Open amazon page + Then Verify that footer has 38 links diff --git a/features/tests/amazon_search.feature b/features/tests/amazon_search.feature index 0d067a332..fc02c76ab 100644 --- a/features/tests/amazon_search.feature +++ b/features/tests/amazon_search.feature @@ -5,8 +5,29 @@ Feature: Tests for amazon search Given Open amazon page When Search for coffee Then Search results for "coffee" are shown +# +# Scenario: User can search for mug +# Given Open amazon page +# When Search for mug +# Then Search results for "mug" are shown - Scenario: User can search for mug + Scenario Outline: User can search for a product Given Open amazon page - When Search for mug - Then Search results for "mug" are shown + When Search for + Then Search results for are shown + Examples: + |product |search_result | + |coffee |"coffee" | + |mug |"mug" | + |dress |"dress" | + |Tritan Farm to Table Pitcher on amazon |"Tritan Farm to Table Pitcher on amazon" | + + Scenario: User can add a product to the cart + Given Open Amazon page + When Search for Tritan Farm to Table Pitcher on amazon + And Click on the first product + And Store product name + And Click on Add to cart button + And Open cart page + Then Verify cart has 1 item(s) + And Verify cart has correct product \ No newline at end of file