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
33 changes: 33 additions & 0 deletions features/steps/amazon_main_page_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from selenium.webdriver.common.by import By
from behave import given, when, then

HAM_MENU = (By.ID, 'nav-hamburger-menu')
FOOTER_LINKS = (By.CSS_SELECTOR, '.navFooterDescItem a')


@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()


@then('Verify hamburger menu is present')
def verify_ham_menu_present(context):
context.driver.find_element(*HAM_MENU)


@then('Verify that footer has {expected_link_count} links')
def verify_link_count(context, expected_link_count):
expected_link_count = int(expected_link_count)

links = context.driver.find_elements(*FOOTER_LINKS)

assert len(links) == expected_link_count, \
f'Expected {expected_link_count} links, but got {len(links)}'
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}'
10 changes: 10 additions & 0 deletions features/tests/amazon_main.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Created by svetlanalevinsohn at 10/1/22
Feature: Tests for Amazon main page

Scenario: Hamburger menu is present
Given Open amazon page
Then Verify hamburger menu is present

Scenario: Footer has correct amount of links
Given Open amazon page
Then Verify that footer has 38 links
33 changes: 33 additions & 0 deletions features/tests/amazon_search.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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

Scenario Outline: User can search for a product
Given Open amazon page
When Search for <product>
Then Search results for <search_result> are shown
Examples:
|product |search_result |
|coffee |"coffee" |
|mug |"mug" |
|dress |"dress" |
|Tritan Farm to Table Pitcher on amazon |"Tritan Farm to Table Pitcher on amazon" |

Scenario: User can add a product to the cart
Given Open Amazon page
When Search for Tritan Farm to Table Pitcher on amazon
And Click on the first product
And Store product name
And Click on Add to cart button
And Open cart page
Then Verify cart has 1 item(s)
And Verify cart has correct product