diff --git a/amazon_search_script.py b/amazon_search_script.py new file mode 100644 index 000000000..841c75390 --- /dev/null +++ b/amazon_search_script.py @@ -0,0 +1,22 @@ +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/') + +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/chromedriver b/chromedriver new file mode 100755 index 000000000..f7e9dc7f4 Binary files /dev/null and b/chromedriver differ diff --git a/css_selectors.py b/css_selectors.py new file mode 100644 index 000000000..37067b678 --- /dev/null +++ b/css_selectors.py @@ -0,0 +1,37 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service + +driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') + +# By ID +driver.find_element(By.ID, 'twotabsearchtextbox') + +# By CSS, using ID +driver.find_element(By.CSS_SELECTOR, '#twotabsearchtextbox') + +# By CSS, using class +driver.find_element(By.CSS_SELECTOR, 'span.icp-nav-flag-lop') +driver.find_element(By.CSS_SELECTOR, '.icp-nav-flag-lop') +# multiple classes => +driver.find_element(By.CSS_SELECTOR, 'span.icp-nav-flag-us.icp-nav-flag-lop.icp-nav-flag') + +# By CSS, using attributes (except ID amd Class) +driver.find_element(By.CSS_SELECTOR, "a[data-csa-c-content-id='nav_cs_bestsellers']") +driver.find_element(By.CSS_SELECTOR, "a[href='/gp/bestsellers/?ref_=nav_cs_bestsellers']") +# multiple attr +driver.find_element(By.CSS_SELECTOR, "a[data-csa-c-content-id='nav_cs_bestsellers'][tabindex='0']") + +# class + attr +driver.find_element(By.CSS_SELECTOR, "a.nav-a[data-csa-c-content-id='nav_cs_bestsellers'][tabindex='0']") + +# Attributes, partial match *= +driver.find_element(By.CSS_SELECTOR, "a[href*='?ref_=nav_cs_bestsellers']") + +# CSS, from parent to child +driver.find_element(By.CSS_SELECTOR, "#legalTextRow a[href*=condition_of_use]") +driver.find_element(By.CSS_SELECTOR, "div.a-section #legalTextRow a[href*=condition_of_use]") + + +# XPATH backwards (from child up to parent) +driver.find_element(By.XPATH, "//*[./a[contains(@href, 'ap_signin_notification_condition_of_use')]]") diff --git a/features/environment.py b/features/environment.py index d9d7a6ac2..202a83978 100755 --- a/features/environment.py +++ b/features/environment.py @@ -1,11 +1,13 @@ from selenium import webdriver +from selenium.webdriver.chrome.service import Service def browser_init(context): """ :param context: Behave context """ - context.driver = webdriver.Chrome() + service = Service('/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') + context.driver = webdriver.Chrome(service=service) # context.browser = webdriver.Safari() # context.browser = webdriver.Firefox() @@ -28,5 +30,4 @@ def after_step(context, step): def after_scenario(context, feature): - context.driver.delete_all_cookies() context.driver.quit() diff --git a/features/steps/amazon_search.py b/features/steps/amazon_search.py new file mode 100644 index 000000000..ad530120a --- /dev/null +++ b/features/steps/amazon_search.py @@ -0,0 +1,24 @@ +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('Input text {search_word}') +def input_search_word(context, search_word): + context.driver.find_element(By.ID, 'twotabsearchtextbox').send_keys(search_word) + + +@when('Click on search button') +def click_search(context): + context.driver.find_element(By.ID, 'nav-search-submit-button').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/tests/amazon_search.feature b/features/tests/amazon_search.feature new file mode 100644 index 000000000..02a3c7bfa --- /dev/null +++ b/features/tests/amazon_search.feature @@ -0,0 +1,14 @@ +# Created by svetlanalevinsohn at 2/11/23 +Feature: Amazon search tests + + Scenario: User can search for coffee on Amazon + Given Open Amazon page + When Input text coffee + And Click on search button + Then Verify that text "coffee" is shown + + 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 diff --git a/locators.py b/locators.py new file mode 100644 index 000000000..31c20fbfc --- /dev/null +++ b/locators.py @@ -0,0 +1,39 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service + +driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') + +# By ID +driver.find_element(By.ID, 'twotabsearchtextbox') + +# 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']") + +# By Xpath, multiple attr +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']") + +# By xpath, attr starts with certain value: +driver.find_element(By.XPATH, '//a[starts-with(@href, "/gp/bestsellers/?")]') + +# By xpath, text +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 ']") + +# By xpath, going from parent node ==> child +driver.find_element(By.XPATH, "//div[@id='nav-xshop']//a[text()='Best Sellers']") + diff --git a/sample_script.py b/sample_script.py index 5d9332048..7ea10c096 100755 --- a/sample_script.py +++ b/sample_script.py @@ -3,7 +3,7 @@ from selenium.webdriver.common.by import By # init driver -driver = webdriver.Chrome() +driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/13-python-selenium-automation/chromedriver') driver.maximize_window() # open the url