diff --git a/amazon_search_script.py b/amazon_search_script.py new file mode 100644 index 000000000..3454b734f --- /dev/null +++ b/amazon_search_script.py @@ -0,0 +1,25 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from time import sleep +from selenium.webdriver.chrome.service import Service + +# driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +service = Service('/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +driver = webdriver.Chrome(service=service) + +driver.get('https://www.amazon.com/') + +sleep(5) + +driver.find_element(By.ID, 'twotabsearchtextbox').send_keys('table') +driver.find_element(By.ID, 'nav-search-submit-button').click() + +expected_result = '"table"' +actual_result = driver.find_element(By.XPATH, "//span[@class='a-color-state a-text-bold']").text +print(actual_result) + +assert expected_result == actual_result, f'Expected {expected_result} but got actual {actual_result}' +print('Test case passed') + +driver.quit() + diff --git a/ccs_selectors.py b/ccs_selectors.py new file mode 100644 index 000000000..cf8b7e4b5 --- /dev/null +++ b/ccs_selectors.py @@ -0,0 +1,68 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from time import sleep +from selenium.webdriver.chrome.service import Service + +# driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +service = Service('/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +driver = webdriver.Chrome(service=service) + +# by ID +driver.find_element(By.ID, 'twotabsearchtextbox') +driver.find_element(By.ID, 'ap_customer_name') +driver.find_element(By.ID, 'ap_email') + + +# By CSS, using ID Syntax tag#ID but you can omit tag. instead of 'input#twotabsearchtextbox'use '#twotabsearchtextbox' +driver.find_element(By.CSS_SELECTOR, '#twotabsearchtextbox') +driver.find_element(By.CSS_SELECTOR, '#ap_customer_name') +driver.find_element(By.CSS_SELECTOR, '#ap_email') +driver.find_element(By.CSS_SELECTOR, '#ap_password') +driver.find_element(By.CSS_SELECTOR, '#ap_password_check') +driver.find_element(By.CSS_SELECTOR, '#continue') +driver.find_element(By.CSS_SELECTOR, '#ab-registration-link') + + +# By class - Syntax tag.class You can omit tag --> .class +driver.find_element(By.CSS_SELECTOR, ".icp-nav-flag-us") +driver.find_element(By.CSS_SELECTOR, ".a-icon-logo") +driver.find_element(By.CSS_SELECTOR, ".a-spacing-small") +# By multiple classes Syntax .class.class --> classes are separated by dot . +driver.find_element(By.CSS_SELECTOR, ".icp-nav-flag-us.icp-nav-flag") +# By tag + multiple classes Syntax tag.class.class +driver.find_element(By.CSS_SELECTOR, "span.icp-nav-flag-us.icp-nav-flag") + +# By CSS using attributes (except ID and class see syntax for it above) Syntax tag[attribute='value'] +driver.find_element("a[href='/gp/bestsellers/?ref_=nav_cs_bestsellers']") +driver.find_element("a[data-csa-c-content-id='nav_cs_bestsellers']") +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, Syntax tag[attribute='value'][attribute='value'] +driver.find_element(By.CSS_SELECTOR, "input[name='email'][type='email'][maxlength='128']") +driver.find_element(By.CSS_SELECTOR, "a[href='/music/unlimited?ref_=nav_cs_music'][data-csa-c-content-id='nav_cs_music']" +# By attribute + class Syntax tag.class[attribute='value'] +driver.find_element(By.CSS_SELECTOR, "a.nav-a[data-csa-c-content-id='nav_cs_books']" +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 Syntax tag[attribute*=''] +driver.find_element(By.CSS_SELECTOR, "a[href*='ap_signin_notification_condition_of_use']" +driver.find_element(By.CSS_SELECTOR, "a[href*='ap_register_notification_condition_of_use']") +driver.find_element(By.CSS_SELECTOR, "a[href*='ap_register_notification_privacy_notice']") + +# By class and partial attribute Syntax tag.class[attribute*='partial_value'] +driver.find_element(By.CSS_SELECTOR, "a.a-link-emphasis[href*='ap/signin?showRmrMe']") + +# For classes, to get partial match (use *): +driver.find_element(By.CSS_SELECTOR, "a[class*='button']") + +# From parent => child Syntax tag#id_value tag[attribute*='value'] +driver.find_element(By.CSS_SELECTOR, "div#legalTextRow a[href*='condition']") +driver.find_element(By.CSS_SELECTOR, "div#legalTextRow a[href*='ap_register_notification_condition_of_use']") +driver.find_element(By.CSS_SELECTOR, "div#legalTextRow a[href*='ap_register_notification_privacy_notice']") + + + + diff --git a/chromedriver b/chromedriver new file mode 100755 index 000000000..c6d9aabd1 Binary files /dev/null and b/chromedriver differ diff --git a/features/steps/amazon_main_page_steps.py b/features/steps/amazon_main_page_steps.py new file mode 100644 index 000000000..4a46d1064 --- /dev/null +++ b/features/steps/amazon_main_page_steps.py @@ -0,0 +1,28 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then + + +AMAZON_SEARCH_INPUT_FIELD = (By.ID, 'twotabsearchtextbox') +SEARCH_ICON = (By.ID, 'nav-search-submit-button') + +@given('Open Amazon page') +def open_amazon(context): + context.driver.get('https://www.amazon.com/') + + +@when('Input text {search_word}') +def input_search_word(context, search_word): + context.driver.find_element(*AMAZON_SEARCH_INPUT_FIELD).send_keys(search_word) + + +@when('Click on search button') +def click_search(context): + context.driver.find_element(*SEARCH_ICON).click() + +# +# @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/steps/cart_page_steps.py b/features/steps/cart_page_steps.py new file mode 100644 index 000000000..4a31efc5e --- /dev/null +++ b/features/steps/cart_page_steps.py @@ -0,0 +1,16 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then + +CART = (By.ID, 'nav-cart-count') +PRODUCT_NAME = () + + +@when('Open cart page') +def open_cart_page(context): + context.driver.get('https://www.amazon.com/cart?ref_=ewc_gtc') + + +@then('Verify cart has {expected_count} item(s)') +def verify_cart_count(context, expected_count): + actual_text = context.driver.find_element(*CART).text + assert expected_count == actual_text, f'Expected {expected_count}, but got {actual_text} ' \ No newline at end of file diff --git a/features/steps/product_page_steps.py b/features/steps/product_page_steps.py new file mode 100644 index 000000000..4c2d9f47f --- /dev/null +++ b/features/steps/product_page_steps.py @@ -0,0 +1,12 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then +from time import sleep + + +ADD_TO_CART_BTN = (By.ID, 'add-to-cart-button') + + +@when('Click on Add to cart button') +def click_add_to_cart(context): + context.driver.find_element(*ADD_TO_CART_BTN).click() + sleep(4) diff --git a/features/steps/search_result_steps.py b/features/steps/search_result_steps.py new file mode 100644 index 000000000..751e870fc --- /dev/null +++ b/features/steps/search_result_steps.py @@ -0,0 +1,17 @@ +from selenium.webdriver.common.by import By +from behave import given, 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}' \ No newline at end of file diff --git a/features/steps/sign_in.py b/features/steps/sign_in.py new file mode 100644 index 000000000..a60bef22e --- /dev/null +++ b/features/steps/sign_in.py @@ -0,0 +1,22 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then + + +@when('Click Returns and Orders') +def click_orders(context): + context.driver.find_element(By.ID, 'nav-orders').click() + + +@then('Verify that header {expected_result} is visible') +def verify_sign_in_header(context, expected_result): + actual_result = context.driver.find_element(By.XPATH, "//h1[contains(text(), 'Sign in')]").text + + assert expected_result == actual_result, f'Expected {expected_result} but found actual {actual_result}' + + +@then('Verify that Email field is present') +def verify_email_field(context): + actual_result = context.driver.find_element(By.ID, 'ap_email').is_displayed(), 'Email field not shown' + + + diff --git a/features/tests/amazon_search.feature b/features/tests/amazon_search.feature new file mode 100644 index 000000000..8d4e4856a --- /dev/null +++ b/features/tests/amazon_search.feature @@ -0,0 +1,26 @@ +# Created by ekaterinasuvorova at 5/5/23 +Feature: Amazon search tests + + Scenario Outline: User can search for a product on Amazon + Given Open Amazon page + When Input text + When Click on search button + Then Verify that text is shown + Examples: + |search_word |search_result | + |coffee |"coffee" | + |table |"table" | + |mug |"mug" | + + 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) + + + + diff --git a/features/tests/product_search.feature b/features/tests/product_search.feature index 7af6a71a5..161287276 100755 --- a/features/tests/product_search.feature +++ b/features/tests/product_search.feature @@ -1,4 +1,3 @@ -# Created by Svetlana at 4/4/19 Feature: Test Scenarios for Search functionality Scenario: User can search for a product diff --git a/features/tests/sign_in.feature b/features/tests/sign_in.feature new file mode 100644 index 000000000..ee3bc9e7f --- /dev/null +++ b/features/tests/sign_in.feature @@ -0,0 +1,9 @@ +# Created by ekaterinasuvorova at 5/10/23 +Feature: Sign in page display for new/logged out user + #Verify that logged out user sees Sign In when clicking on Returns and Orders + + Scenario: User can see Sign In page + Given Open Amazon page + When Click Returns and Orders + Then Verify that header Sign in is visible + Then Verify that Email field is present \ No newline at end of file diff --git a/locators.py b/locators.py new file mode 100644 index 000000000..8373e784d --- /dev/null +++ b/locators.py @@ -0,0 +1,72 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service + +# driver = webdriver.Chrome(executable_path='/Users/ekaterinasuvorova/Automation2023/python-selenium-automation/chromedriver') +service = Service('/Users/ekaterinasuvorova/Automation2023/python-selenium-automation/chromedriver') +driver = webdriver.Chrome(service=service) + + +# By ID +driver.find_element(By.ID, 'twotabsearchtextbox') +driver.find_element(By.ID, 'nav-link-accountList') +driver.find_element(By.ID, 'continue') +driver.find_element(By.ID, 'createAccountSubmit') +driver.find_element(By.ID, 'ap_customer_name') +driver.find_element(By.ID, 'ap_email') +driver.find_element(By.ID, 'ap_password') +driver.find_element(By.ID, 'ap_password_check') +driver.find_element(By.ID, 'continue') +driver.find_element(By.ID, 'ab-registration-link') +driver.find_element(By.ID, 'nav-orders') + + +# By Xpath, tag and attribute +driver.find_element(By.XPATH, "//input[@placeholder='Search Amazon']") +driver.find_element(By.XPATH, "//input[@aria-label='Search Amazon']") +driver.find_element(By.XPATH, "//img[@alt='PAVOI Jewelry']") +driver.find_element(By.XPATH, "//i[@aria-label='Amazon']") +driver.find_element(By.XPATH, "//span[@class='a-expander-prompt']") +driver.find_element(By.XPATH, "//span[@class='a-expander-prompt']") +driver.find_element(By.XPATH, "//h1[@class='a-spacing-small']") + +# By Xpath, multiple attributes +driver.find_element(By.XPATH, "//a[@aria-label='Amazon' and @href='/ref=nav_logo']") +driver.find_element(By.XPATH, "//a[@href='/gp/bestsellers/?ref_=nav_cs_bestsellers' and @data-csa-c-content-id='nav_cs_bestsellers' and @data-csa-c-type='link']") + + + +# By Xpath, contains: +driver.find_element(By.XPATH, "//a[contains(@href, 'nav_cs_bestsellers')]") +# contains AND attr +driver.find_element(By.XPATH, "//a[contains(@href, 'bestsellers') and @data-csa-c-type='link']") + +# By Xpath, without a tag +driver.find_element(By.XPATH, "//*[contains(@href, 'bestsellers') and @data-csa-c-type='link']") +driver.find_element(By.XPATH, "//*[@aria-label='Search Amazon']") +driver.find_element(By.XPATH, "//*[contains(@href, 'ap_signin_notification_condition_of_use')]") +driver.find_element(By.XPATH, ) +driver.find_element(By.XPATH, "//*[contains(@href, 'ap_signin_notification_privacy_notice')]") + + + +# By xpath, attr starts with certain value: +driver.find_element(By.XPATH, '//a[starts-with(@href, "/gp/bestsellers/?")]') + +# By xpath, text Syntax //tag[text()='value'] +driver.find_element(By.XPATH, "//h2[text()='The warm-weather edit']") +# Contains text: +driver.find_element(By.XPATH, "//h2[contains(text(), 'The warm-weather')]") +driver.find_element(By.XPATH, "//a[text()='Best Sellers' and @class='nav-a ']") +driver.find_element(By.XPATH, "//h1[contains(text(), 'Sign in')]") +driver.find_element(By.XPATH, "//div[contains(text(), ' Passwords must be at least 6 characters.')]") + + + +# By xpath, going from parent node ==> child +driver.find_element(By.XPATH, "//div[@id='nav-xshop']//a[text()='Best Sellers']") +driver.find_element(By.XPATH, "//div[@id='legalTextRow']//a[text()='Conditions of Use']") +driver.find_element(By.XPATH, "//div[@data-component-type='s-search-result']//a[.//span[@class='a-price']]") + +# 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/sample_script.py b/sample_script.py index 5d9332048..e62d5fcf0 100755 --- a/sample_script.py +++ b/sample_script.py @@ -1,10 +1,11 @@ -from time import sleep from selenium import webdriver from selenium.webdriver.common.by import By +from time import sleep +from selenium.webdriver.chrome.service import Service -# init driver -driver = webdriver.Chrome() -driver.maximize_window() +# driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +service = Service('/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +driver = webdriver.Chrome(service=service) # open the url driver.get('https://www.google.com/') diff --git a/sign_in_page_amazon_logo.py b/sign_in_page_amazon_logo.py new file mode 100644 index 000000000..728db7d01 --- /dev/null +++ b/sign_in_page_amazon_logo.py @@ -0,0 +1,24 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from time import sleep +from selenium.webdriver.chrome.service import Service + +# driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +service = Service('/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +driver = webdriver.Chrome(service=service) + +# Test 'Verify Amazon logo is present on Sign In page' +driver.get('https://www.amazon.com/') +sleep(3) +driver.find_element(By.ID, 'nav-link-accountList').click() + +#expected_result = True +#actual_result = driver.find_element(By.XPATH, "//i[@aria-label='Amazon']").is_displayed() +#assert actual_result == expected_result, 'Amazon logo not shown' + +assert driver.find_element(By.XPATH, "//i[@aria-label='Amazon']").is_displayed(), 'Amazon logo not shown' + +driver.quit() + + + diff --git a/sign_in_text_email_verification.py b/sign_in_text_email_verification.py new file mode 100644 index 000000000..33eb0fe15 --- /dev/null +++ b/sign_in_text_email_verification.py @@ -0,0 +1,25 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from time import sleep +from selenium.webdriver.chrome.service import Service + +# driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +service = Service('/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') +driver = webdriver.Chrome(service=service) + +# Test Case: Logged out user sees Sign in page when clicking Orders +driver.get('https://www.amazon.com/') +sleep(2) +driver.find_element(By.ID, 'nav-orders').click() + +expected_result = 'Sign in' +actual_result = driver.find_element(By.XPATH, "//h1[@class='a-spacing-small']").text + +assert expected_result == actual_result, f'Expected {expected_result} but found {actual_result}' + +#Verify email field present +#you can just ask driver to find element if it's not found it will return no element exeption ' +# OR + +assert driver.find_element(By.ID, 'ap_email').is_displayed(), 'Email field not shown' +driver.quit()