From 9d92732b4122be46f3bd229439b8005438514e26 Mon Sep 17 00:00:00 2001 From: Svetlana Levinsohn Date: Sat, 24 Sep 2022 12:10:10 -0700 Subject: [PATCH] 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