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
38 changes: 38 additions & 0 deletions css_selectors.py
Original file line number Diff line number Diff line change
@@ -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')]]")
2 changes: 1 addition & 1 deletion features/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
15 changes: 15 additions & 0 deletions features/steps/amazon_main_page_steps.py
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 8 additions & 0 deletions features/steps/search_results_page_steps.py
Original file line number Diff line number Diff line change
@@ -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}'
12 changes: 12 additions & 0 deletions features/tests/amazon_search.feature
Original file line number Diff line number Diff line change
@@ -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