diff --git a/features/environment.py b/features/environment.py index 2cdaadfb1..73467eaa7 100755 --- a/features/environment.py +++ b/features/environment.py @@ -1,4 +1,5 @@ from selenium import webdriver +from selenium.webdriver.support.wait import WebDriverWait def browser_init(context): @@ -11,6 +12,7 @@ def browser_init(context): context.driver.maximize_window() context.driver.implicitly_wait(4) + context.driver.wait = WebDriverWait(context.driver, 10) def before_scenario(context, scenario): diff --git a/features/steps/amazon_main_page_steps.py b/features/steps/amazon_main_page_steps.py index da11927a0..4c5835260 100644 --- a/features/steps/amazon_main_page_steps.py +++ b/features/steps/amazon_main_page_steps.py @@ -1,8 +1,11 @@ from selenium.webdriver.common.by import By from behave import given, when, then +from selenium.webdriver.support import expected_conditions as EC HAM_MENU = (By.ID, 'nav-hamburger-menu') FOOTER_LINKS = (By.CSS_SELECTOR, '.navFooterDescItem a') +SIGN_IN = (By.CSS_SELECTOR, "#nav-signin-tooltip .nav-action-button") +SEARCH_INPUT = (By.ID, 'twotabsearchtextbox') @given('Open amazon page') @@ -12,12 +15,18 @@ def open_amazon(context): @when('Search for {product}') def search_product(context, product): - element = context.driver.find_element(By.ID, 'twotabsearchtextbox') + element = context.driver.find_element(*SEARCH_INPUT) element.clear() element.send_keys(product) context.driver.find_element(By.ID, 'nav-search-submit-button').click() +@when('Click on button from SignIn popup') +def click_sign_in(context): + e = context.driver.wait.until(EC.element_to_be_clickable(SIGN_IN), message='Sign in not clickable') + e.click() + + @then('Verify hamburger menu is present') def verify_ham_menu_present(context): context.driver.find_element(*HAM_MENU) diff --git a/features/steps/product_page_steps.py b/features/steps/product_page_steps.py new file mode 100644 index 000000000..6a2674fae --- /dev/null +++ b/features/steps/product_page_steps.py @@ -0,0 +1,44 @@ +from selenium.webdriver.common.by import By +from behave import when, given, then +from time import sleep + + +ADD_TO_CART_BTN = (By.ID, 'add-to-cart-button') +PRODUCT_NAME = (By.ID, 'productTitle') +COLOR_OPTIONS = (By.CSS_SELECTOR, "#variation_color_name li") +CURRENT_COLOR = (By.CSS_SELECTOR, "#variation_color_name .selection") + + +@given('Open Amazon product {product_id} page') +def open_amazon_product(context, product_id): + context.driver.get(f'https://www.amazon.com/dp/{product_id}/') + + +@when('Click on Add to cart button') +def click_add_to_cart(context): + context.driver.find_element(*ADD_TO_CART_BTN).click() + sleep(2) + + +@when('Store product name') +def get_product_name(context): + context.product_name = context.driver.find_element(*PRODUCT_NAME).text + print(f'Current product: {context.product_name}') + + +@then('Verify user can click through colors') +def verify_can_click_colors(context): + expected_colors = ['Black', 'Solid Black'] + actual_colors = [] + + colors = context.driver.find_elements(*COLOR_OPTIONS) + + for color in colors[:3]: + color.click() + current_color = context.driver.find_element(*CURRENT_COLOR).text + actual_colors += [current_color] + + assert expected_colors == actual_colors, \ + f'Expected colors {expected_colors} did not match actual {actual_colors}' + + diff --git a/features/steps/sign_in_steps.py b/features/steps/sign_in_steps.py new file mode 100644 index 000000000..6fb2dcb8a --- /dev/null +++ b/features/steps/sign_in_steps.py @@ -0,0 +1,8 @@ +from selenium.webdriver.common.by import By +from behave import then +from selenium.webdriver.support import expected_conditions as EC + + +@then('Verify Sign in page opened') +def verify_sign_in_opened(context): + context.driver.wait.until(EC.url_contains('ap/signin'), message='Signin URL did not open') \ No newline at end of file diff --git a/features/tests/product_details.feature b/features/tests/product_details.feature new file mode 100644 index 000000000..b34a6b6fa --- /dev/null +++ b/features/tests/product_details.feature @@ -0,0 +1,5 @@ +Feature: Tests for product page + + Scenario: User can select colors + Given Open Amazon product B07MNHBRCJ page + Then Verify user can click through colors \ No newline at end of file diff --git a/features/tests/sign_in.feature b/features/tests/sign_in.feature new file mode 100644 index 000000000..93bbd5304 --- /dev/null +++ b/features/tests/sign_in.feature @@ -0,0 +1,7 @@ +# Created by svetlanalevinsohn at 7/16/22 +Feature: Sign in test cases + + Scenario: Sign In page can be opened from SignIn popup + Given Open Amazon page + When Click on button from SignIn popup + Then Verify Sign in page opened diff --git a/sample_script.py b/sample_script.py index 5d9332048..37ce493cc 100755 --- a/sample_script.py +++ b/sample_script.py @@ -1,26 +1,32 @@ from time import sleep from selenium import webdriver from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC # init driver -driver = webdriver.Chrome() +driver = webdriver.Chrome(executable_path='/Users/svetlanalevinsohn/JobEasy/12-python-selenium-automation/chromedriver') driver.maximize_window() +driver.implicitly_wait(5) # applied to find_element(s) / #100 ms if element is found +driver.wait = WebDriverWait(driver, 10) # open the url driver.get('https://www.google.com/') search = driver.find_element(By.NAME, 'q') search.clear() -search.send_keys('Dress') +search.send_keys('Car') # wait for 4 sec -sleep(4) +# sleep(4) +btn = (By.NAME, 'btnK') +driver.wait.until(EC.element_to_be_clickable(btn), message='Search btn not clickable') # 500 ms # click search driver.find_element(By.NAME, 'btnK').click() # verify -assert 'dress' in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" +assert 'car' in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" print('Test Passed') driver.quit()