Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions amazon_search_script.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file added chromedriver
Binary file not shown.
37 changes: 37 additions & 0 deletions css_selectors.py
Original file line number Diff line number Diff line change
@@ -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')]]")
5 changes: 3 additions & 2 deletions features/environment.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -28,5 +30,4 @@ def after_step(context, step):


def after_scenario(context, feature):
context.driver.delete_all_cookies()
context.driver.quit()
24 changes: 24 additions & 0 deletions features/steps/amazon_search.py
Original file line number Diff line number Diff line change
@@ -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}'

14 changes: 14 additions & 0 deletions features/tests/amazon_search.feature
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions locators.py
Original file line number Diff line number Diff line change
@@ -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']")

2 changes: 1 addition & 1 deletion sample_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down