From 2526419f2eb4a74eaf559a24712fd6ec401f529d Mon Sep 17 00:00:00 2001 From: Lopamudra S Date: Tue, 20 Aug 2024 22:10:14 -0600 Subject: [PATCH 1/7] Update automated search script --- features/tests/product_search.feature | 4 ++-- sample_script.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/features/tests/product_search.feature b/features/tests/product_search.feature index 36d6913cf..a4df556ae 100755 --- a/features/tests/product_search.feature +++ b/features/tests/product_search.feature @@ -2,6 +2,6 @@ Feature: Test Scenarios for Search functionality Scenario: User can search for a product Given Open Google page - When Input Car into search field + When Input Table into search field And Click on search icon - Then Product results for Car are shown \ No newline at end of file + Then Product results for Table are shown \ No newline at end of file diff --git a/sample_script.py b/sample_script.py index 3925e9462..0804f58ce 100755 --- a/sample_script.py +++ b/sample_script.py @@ -18,7 +18,7 @@ # populate search field search = driver.find_element(By.NAME, 'q') search.clear() -search.send_keys('Car') +search.send_keys('Table') # wait for 4 sec sleep(4) @@ -27,7 +27,7 @@ driver.find_element(By.NAME, 'btnK').click() # verify search results -assert 'car'.lower() in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" +assert 'table'.lower() in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" print('Test Passed') driver.quit() From 2d5cda4064e8d337194b0fe388e5cc87c845d2ab Mon Sep 17 00:00:00 2001 From: Lopamudra S Date: Mon, 2 Sep 2024 18:40:58 -0600 Subject: [PATCH 2/7] homework for lesson 2 --- locators.py | 41 +++++++++++++++++++++++++++++++++++++++++ target_search.py | 31 +++++++++++++++++++++++++++++++ target_signin.py | 26 ++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 locators.py create mode 100644 target_search.py create mode 100644 target_signin.py diff --git a/locators.py b/locators.py new file mode 100644 index 000000000..b6ad147ca --- /dev/null +++ b/locators.py @@ -0,0 +1,41 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service +from webdriver_manager.chrome import ChromeDriverManager +from time import sleep + +# get the path to the ChromeDriver executable +driver_path = ChromeDriverManager().install() + +# create a new Chrome browser instance +service = Service(driver_path) +driver = webdriver.Chrome(service=service) +driver.maximize_window() + +# open the url +driver.get('https://www.amazon.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_ya_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&') +sleep(15) + +# locate element: +# driver.find_element() By. / value +# by Xpath: +driver.find_element(By.XPATH, "//i[@class='a-icon a-icon-logo']") + +# locate by ID: + +driver.find_element(By.ID, "ap_email") +# locate by ID: +driver.find_element(By.ID, "continue") +# by XPath: +driver.find_element(By.XPATH, "//a[text()='Conditions of Use']") +# by XPath: +driver.find_element(By.XPATH, "//a[text()='Privacy Notice']") +# by XPath: +driver.find_element(By.XPATH, "//span[@class='a-expander-prompt']") +# by ID: +driver.find_element(By.ID, "auth-fpp-link-bottom") +# by ID: +driver.find_element(By.ID, "ap-other-signin-issues-link") +# by ID: +driver.find_element(By.ID, "createAccountSubmit") + diff --git a/target_search.py b/target_search.py new file mode 100644 index 000000000..35602cc89 --- /dev/null +++ b/target_search.py @@ -0,0 +1,31 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service +from webdriver_manager.chrome import ChromeDriverManager +from time import sleep + +# get the path to the ChromeDriver executable +driver_path = ChromeDriverManager().install() + +# create a new Chrome browser instance +service = Service(driver_path) +driver = webdriver.Chrome(service=service) +driver.maximize_window() + +driver.get("https://www.target.com/") + +# search key => enter coffee +driver.find_element(By.ID, 'search').send_keys('coffee') +# search button => click +driver.find_element(By.XPATH,"//button[@data-test='@web/Search/SearchButton']").click() +#Wait for search to complete +sleep(5) + +# verification +#After wait find what you are looking for +actual_result = driver.find_element(By.XPATH, "//div[@data-test='resultsHeading']").text + +expected_result ='coffee' + +assert expected_result in actual_result +print('Test case passed') \ No newline at end of file diff --git a/target_signin.py b/target_signin.py new file mode 100644 index 000000000..0d75b05c2 --- /dev/null +++ b/target_signin.py @@ -0,0 +1,26 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service +from webdriver_manager.chrome import ChromeDriverManager +from time import sleep + +# get the path to the ChromeDriver executable +driver_path = ChromeDriverManager().install() + +# create a new Chrome browser instance +service = Service(driver_path) +driver = webdriver.Chrome(service=service) +driver.maximize_window() + +driver.get("https://www.target.com/") + +# find and click sign in button +driver.find_element(By.XPATH, "//a[@data-test='@web/AccountLink']").click() +sleep(10) +# click SignIn from side navigation +driver.find_element(By.XPATH, "//a[@data-test='accountNav-signIn']").click() +sleep(10) +# verification for “Sign into your Target account” text is shown +driver.find_element(By.XPATH, "//span[text()='Sign into your Target account']") +# verification for SignIn button is shown +driver.find_element(By.XPATH, "//button[@type='submit']") From eddf4823ab1146a0c9fec7a269fcf9d1ca1f2b26 Mon Sep 17 00:00:00 2001 From: Lopamudra S Date: Mon, 2 Sep 2024 18:44:21 -0600 Subject: [PATCH 3/7] homework for lesson 2 --- locators.py => lesson2/locators.py | 0 target_search.py => lesson2/target_search.py | 0 target_signin.py => lesson2/target_signin.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename locators.py => lesson2/locators.py (100%) rename target_search.py => lesson2/target_search.py (100%) rename target_signin.py => lesson2/target_signin.py (100%) diff --git a/locators.py b/lesson2/locators.py similarity index 100% rename from locators.py rename to lesson2/locators.py diff --git a/target_search.py b/lesson2/target_search.py similarity index 100% rename from target_search.py rename to lesson2/target_search.py diff --git a/target_signin.py b/lesson2/target_signin.py similarity index 100% rename from target_signin.py rename to lesson2/target_signin.py From 15c62936791a7bce3c7d71a3f1bd875837d15157 Mon Sep 17 00:00:00 2001 From: Lopamudra S Date: Sun, 15 Sep 2024 22:24:28 -0600 Subject: [PATCH 4/7] committing changes for lesson3 --- features/steps/target.py | 41 +++++++++++++++++++++++++++++++++++ features/tests/target.feature | 13 +++++++++++ lesson3/css_selectors.py | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 features/steps/target.py create mode 100644 features/tests/target.feature create mode 100644 lesson3/css_selectors.py diff --git a/features/steps/target.py b/features/steps/target.py new file mode 100644 index 000000000..80a087434 --- /dev/null +++ b/features/steps/target.py @@ -0,0 +1,41 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then +from time import sleep + + +@given('Open target main page') +def open_target(context): + context.driver.get('https://www.target.com/') + + +@when('Click on cart icon') +def click_cart(context): + context.driver.find_element(By.XPATH, "//a[@data-test='@web/CartLink']").click() + sleep(3) + + +@then('Verify cart Empty message shown') +def verify_cart_empty(context): + expected_text='Your cart is empty' + actual_text= context.driver.find_element(By.CSS_SELECTOR, "[data-test='boxEmptyMsg'] h1").text + assert expected_text == actual_text, f'Expected {expected_text} did not match actual {actual_text}' + + +@when('Click Sign in') +def click_sign_in(context): + context.driver.find_element(By.XPATH, "//a[@data-test='@web/AccountLink']").click() + sleep(3) + + +@when('From right side navigation menu, click Sign in') +def click_right_side_nav_sign_in(context): + context.driver.find_element(By.XPATH, "//a[@data-test='accountNav-signIn']").click() + + +@then('Verify Sign into your Target account text is shown') +def verify_sign_into_target_account(context): + expected_text = 'Sign into your Target account' + sleep(5) + actual_text= context.driver.find_element(By.XPATH, "//span[text()='Sign into your Target account']").text + print(actual_text) + assert expected_text == actual_text, f'Expected {expected_text} did not match actual {actual_text}' \ No newline at end of file diff --git a/features/tests/target.feature b/features/tests/target.feature new file mode 100644 index 000000000..59d2297f0 --- /dev/null +++ b/features/tests/target.feature @@ -0,0 +1,13 @@ +# Created by LOPA at 9/15/2024 +Feature: Verify empty cart message on Target website + + Scenario: User can see Cart Empty message + Given Open target main page + When Click on cart icon + Then Verify cart Empty message shown + + Scenario: User can see Sign into your Target account text is shown + Given Open target main page + When Click Sign in + When From right side navigation menu, click Sign in + Then Verify Sign into your Target account text is shown \ No newline at end of file diff --git a/lesson3/css_selectors.py b/lesson3/css_selectors.py new file mode 100644 index 000000000..13bed1ace --- /dev/null +++ b/lesson3/css_selectors.py @@ -0,0 +1,40 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service +from webdriver_manager.chrome import ChromeDriverManager +from time import sleep + +# get the path to the ChromeDriver executable +driver_path = ChromeDriverManager().install() + +# create a new Chrome browser instance +service = Service(driver_path) +driver = webdriver.Chrome(service=service) +driver.maximize_window() + +# open the url +driver.get('https://www.amazon.com/ap/register?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_ya_signin&prevRID=77J0CGYVZ1HQVZVB3Z4R&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&prepopulatedLoginId=&failedSignInCount=0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=usflex&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0') +sleep(15) +# locate element: +# driver.find_element() By. / value +#By CSS, by class +driver.find_element(By.CSS_SELECTOR,".a-icon-logo") +# By CSS, by class +driver.find_element(By.CSS_SELECTOR,".a-spacing-small") +# By CSS, by ID +driver.find_element(By.CSS_SELECTOR,"#ap_customer_name") +# By CSS, by ID +driver.find_element(By.CSS_SELECTOR,"#ap_email") +# By CSS, by ID +driver.find_element(By.CSS_SELECTOR,"#ap_password") +# By CSS, by ID +driver.find_element(By.CSS_SELECTOR,"#ap_password_check") +# By CSS, by ID +driver.find_element(By.CSS_SELECTOR,"#ap_password_check") +# By CSS, by ID +driver.find_element(By.CSS_SELECTOR,"#continue") +# By CSS, from parent => to child, separate by space: +driver.find_element(By.CSS_SELECTOR, ".a-box-inner #legalTextRow [href*='notification_condition_of_use']") +driver.find_element(By.CSS_SELECTOR, "#legalTextRow [href*='privacy']") +#By CSS, by class +driver.find_element(By.CSS_SELECTOR,".a-link-emphasis") From 8cd44f1347aca5bb810b05ebd60c3806897f01f6 Mon Sep 17 00:00:00 2001 From: Lopamudra S Date: Wed, 18 Sep 2024 22:51:03 -0600 Subject: [PATCH 5/7] Committing homework for lesson 4 --- features/steps/cart_steps.py | 31 ++++++++++++++++ features/steps/circle_page_ui_tests.py | 15 ++++++++ features/steps/main_page_steps.py | 34 +++++++++++++++++ features/steps/search_results.py | 36 ++++++++++++++++++ features/steps/sign_in_steps.py | 11 ++++++ features/steps/target.py | 41 --------------------- features/tests/cart.feature | 18 +++++++++ features/tests/circle_page_ui_tests.feature | 6 +++ features/tests/product_search.feature | 4 +- features/tests/sign_in.feature | 9 +++++ features/tests/target.feature | 13 ------- features/tests/target_search.feature | 11 ++++++ 12 files changed, 174 insertions(+), 55 deletions(-) create mode 100644 features/steps/cart_steps.py create mode 100644 features/steps/circle_page_ui_tests.py create mode 100644 features/steps/main_page_steps.py create mode 100644 features/steps/search_results.py create mode 100644 features/steps/sign_in_steps.py delete mode 100644 features/steps/target.py create mode 100644 features/tests/cart.feature create mode 100644 features/tests/circle_page_ui_tests.feature create mode 100644 features/tests/sign_in.feature delete mode 100644 features/tests/target.feature create mode 100644 features/tests/target_search.feature diff --git a/features/steps/cart_steps.py b/features/steps/cart_steps.py new file mode 100644 index 000000000..a2007c3a6 --- /dev/null +++ b/features/steps/cart_steps.py @@ -0,0 +1,31 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then + + +CART_SUMMARY = (By.XPATH, "//div[./span[contains(text(), 'subtotal')]]") +CART_ITEM_TITLE = (By.CSS_SELECTOR, "[data-test='cartItem-title']") + + +@when('Open cart page') +def open_cart(context): + context.driver.get('https://www.target.com/cart') + + +@then('Verify cart Empty message shown') +def verify_cart_empty(context): + expected_text='Your cart is empty' + actual_text= context.driver.find_element(By.CSS_SELECTOR, "[data-test='boxEmptyMsg'] h1").text + assert expected_text == actual_text, f'Expected {expected_text} did not match actual {actual_text}' + + +@then('Verify cart has correct product') +def verify_product_name(context): + actual_name = context.driver.find_element(*CART_ITEM_TITLE).text + print(f'Actual product in cart name: {actual_name}') + #assert "Traditional Medicinals Organic Chamomile with Lavender Herbal Tea - 16ct" in actual_name, f"Expected {"Traditional Medicinals Organic Chamomile with Lavender Herbal Tea - 16ct"} but got {actual_name}" + assert context.product_name in actual_name, f"Expected {context.product_name} but got {actual_name}" + +@then('Verify cart has {amount} item(s)') +def verify_cart_items(context, amount): + cart_summary = context.driver.find_element(*CART_SUMMARY).text + assert f'{amount} item' in cart_summary, f"Expected {amount} items but got {cart_summary}" \ No newline at end of file diff --git a/features/steps/circle_page_ui_tests.py b/features/steps/circle_page_ui_tests.py new file mode 100644 index 000000000..b7a5f72c6 --- /dev/null +++ b/features/steps/circle_page_ui_tests.py @@ -0,0 +1,15 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then +from time import sleep + + +@given('Open target circle page') +def open_target(context): + context.driver.get('https://www.target.com/circle') + + +@then('Verify circle page has {expected_amount} benefit cells') +def verify_circle_page_benefit_cells(context, expected_amount): + expected_amount = int(expected_amount) + cells = context.driver.find_elements(By.CSS_SELECTOR, "[data-test*='@web/slingshot-components/CellsComponent/Link']") + assert len(cells) == expected_amount, f'Expected int{expected_amount} cells but got {len(cells)}' \ No newline at end of file diff --git a/features/steps/main_page_steps.py b/features/steps/main_page_steps.py new file mode 100644 index 000000000..8bac182a3 --- /dev/null +++ b/features/steps/main_page_steps.py @@ -0,0 +1,34 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then +from time import sleep + + +@given('Open target main page') +def open_target(context): + context.driver.get('https://www.target.com/') + + +@when('Click on cart icon') +def click_cart(context): + context.driver.find_element(By.XPATH, "//a[@data-test='@web/CartLink']").click() + sleep(3) + +@when('Click Sign in') +def click_sign_in(context): + context.driver.find_element(By.XPATH, "//a[@data-test='@web/AccountLink']").click() + sleep(3) + + +@when('From right side navigation menu, click Sign in') +def click_right_side_nav_sign_in(context): + context.driver.find_element(By.XPATH, "//a[@data-test='accountNav-signIn']").click() + + +@when('Search for {product}') +def search_product(context, product): + print(product) + context.driver.find_element(By.ID, 'search').send_keys('tea') + context.driver.find_element(By.XPATH, "//button[@data-test='@web/Search/SearchButton']").click() + sleep(5) + + diff --git a/features/steps/search_results.py b/features/steps/search_results.py new file mode 100644 index 000000000..a6af54d97 --- /dev/null +++ b/features/steps/search_results.py @@ -0,0 +1,36 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then +from time import sleep + + +@then('Verify that correct search results shown for {product}') +def verify_results(context, product): + actual_result = context.driver.find_element(By.XPATH, "//div[@data-test='resultsHeading']").text + assert product in actual_result, f'Expected {product}, got actual {actual_result}' + + +ADD_TO_CART_BTN = (By.CSS_SELECTOR, "[id*='addToCartButton']") +ADD_TO_CART_BTN_SIDE_NAV = (By.CSS_SELECTOR, "[data-test='content-wrapper'] [id*='addToCart']") +SIDE_NAV_PRODUCT_NAME = (By.CSS_SELECTOR, "[data-test='content-wrapper'] h4") + + +@when('Click on Add to Cart button') +def click_add_to_cart(context): + sleep(5) + context.driver.find_element(*ADD_TO_CART_BTN).click() + #If we want to find a specific product + #context.driver.find_element(By.CSS_SELECTOR, "[data-test='chooseOptionsButton']").click() + sleep(3) + + +@when('Store product name') +def store_product_name(context): + context.product_name = context.driver.find_element(*SIDE_NAV_PRODUCT_NAME).text + print(f'Product stored: {context.product_name}') + + +@when('Confirm Add to Cart button from side navigation') +def side_nav_click_add_to_cart(context): + context.driver.find_element(*ADD_TO_CART_BTN_SIDE_NAV).click() + sleep(3) + diff --git a/features/steps/sign_in_steps.py b/features/steps/sign_in_steps.py new file mode 100644 index 000000000..75734af9c --- /dev/null +++ b/features/steps/sign_in_steps.py @@ -0,0 +1,11 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then +from time import sleep + +@then('Verify Sign into your Target account text is shown') +def verify_sign_into_target_account(context): + expected_text = 'Sign into your Target account' + sleep(5) + actual_text= context.driver.find_element(By.XPATH, "//span[text()='Sign into your Target account']").text + print(actual_text) + assert expected_text == actual_text, f'Expected {expected_text} did not match actual {actual_text}' \ No newline at end of file diff --git a/features/steps/target.py b/features/steps/target.py deleted file mode 100644 index 80a087434..000000000 --- a/features/steps/target.py +++ /dev/null @@ -1,41 +0,0 @@ -from selenium.webdriver.common.by import By -from behave import given, when, then -from time import sleep - - -@given('Open target main page') -def open_target(context): - context.driver.get('https://www.target.com/') - - -@when('Click on cart icon') -def click_cart(context): - context.driver.find_element(By.XPATH, "//a[@data-test='@web/CartLink']").click() - sleep(3) - - -@then('Verify cart Empty message shown') -def verify_cart_empty(context): - expected_text='Your cart is empty' - actual_text= context.driver.find_element(By.CSS_SELECTOR, "[data-test='boxEmptyMsg'] h1").text - assert expected_text == actual_text, f'Expected {expected_text} did not match actual {actual_text}' - - -@when('Click Sign in') -def click_sign_in(context): - context.driver.find_element(By.XPATH, "//a[@data-test='@web/AccountLink']").click() - sleep(3) - - -@when('From right side navigation menu, click Sign in') -def click_right_side_nav_sign_in(context): - context.driver.find_element(By.XPATH, "//a[@data-test='accountNav-signIn']").click() - - -@then('Verify Sign into your Target account text is shown') -def verify_sign_into_target_account(context): - expected_text = 'Sign into your Target account' - sleep(5) - actual_text= context.driver.find_element(By.XPATH, "//span[text()='Sign into your Target account']").text - print(actual_text) - assert expected_text == actual_text, f'Expected {expected_text} did not match actual {actual_text}' \ No newline at end of file diff --git a/features/tests/cart.feature b/features/tests/cart.feature new file mode 100644 index 000000000..45064fc99 --- /dev/null +++ b/features/tests/cart.feature @@ -0,0 +1,18 @@ +# Created by LOPA at 9/17/2024 +Feature: Test for cart functionality + + Scenario: User can see Cart Empty message + Given Open target main page + When Click on cart icon + Then Verify cart Empty message shown + + +Scenario: User can add a product to cart + Given Open target main page + When Search for tea + And Click on Add to Cart button + And Store product name + And Confirm Add to Cart button from side navigation + And Open cart page + Then Verify cart has 1 item(s) + And Verify cart has correct product \ No newline at end of file diff --git a/features/tests/circle_page_ui_tests.feature b/features/tests/circle_page_ui_tests.feature new file mode 100644 index 000000000..4e3d37d4f --- /dev/null +++ b/features/tests/circle_page_ui_tests.feature @@ -0,0 +1,6 @@ +# Created by LOPA at 9/17/2024 +Feature: Tests for circle page UI + + Scenario: Verify circle page has correct amount benefit cells + Given Open Target circle page + Then Verify circle page has 10 benefit cells \ No newline at end of file diff --git a/features/tests/product_search.feature b/features/tests/product_search.feature index a4df556ae..fd4cc0257 100755 --- a/features/tests/product_search.feature +++ b/features/tests/product_search.feature @@ -4,4 +4,6 @@ Feature: Test Scenarios for Search functionality Given Open Google page When Input Table into search field And Click on search icon - Then Product results for Table are shown \ No newline at end of file + Then Product results for Table are shown + + diff --git a/features/tests/sign_in.feature b/features/tests/sign_in.feature new file mode 100644 index 000000000..d11724bfc --- /dev/null +++ b/features/tests/sign_in.feature @@ -0,0 +1,9 @@ +# Created by LOPA at 9/17/2024 +Feature: Test for Sign in functionality + + + Scenario: User can see Sign into your Target account text is shown + Given Open target main page + When Click Sign in + When From right side navigation menu, click Sign in + Then Verify Sign into your Target account text is shown \ No newline at end of file diff --git a/features/tests/target.feature b/features/tests/target.feature deleted file mode 100644 index 59d2297f0..000000000 --- a/features/tests/target.feature +++ /dev/null @@ -1,13 +0,0 @@ -# Created by LOPA at 9/15/2024 -Feature: Verify empty cart message on Target website - - Scenario: User can see Cart Empty message - Given Open target main page - When Click on cart icon - Then Verify cart Empty message shown - - Scenario: User can see Sign into your Target account text is shown - Given Open target main page - When Click Sign in - When From right side navigation menu, click Sign in - Then Verify Sign into your Target account text is shown \ No newline at end of file diff --git a/features/tests/target_search.feature b/features/tests/target_search.feature new file mode 100644 index 000000000..172a538d5 --- /dev/null +++ b/features/tests/target_search.feature @@ -0,0 +1,11 @@ +# Created by LOPA at 9/17/2024 +Feature: Test for Target search functionality + + Scenario: User can search for a product + Given Open target main page + When Search for a tea + Then Verify that correct search results shown for tea + + + + From 16f8e620e732451c9db6459ba55275dd3ac6fe0b Mon Sep 17 00:00:00 2001 From: Lopamudra S Date: Thu, 26 Sep 2024 10:29:57 -0600 Subject: [PATCH 6/7] Committing changes for lesson 5 --- features/environment.py | 3 ++- features/steps/main_page_steps.py | 2 +- features/steps/produt_details_steps.py | 36 ++++++++++++++++++++++++++ features/steps/search_results.py | 24 +++++++++++------ features/tests/cart.feature | 4 +-- features/tests/product_details.feature | 6 +++++ 6 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 features/steps/produt_details_steps.py create mode 100644 features/tests/product_details.feature diff --git a/features/environment.py b/features/environment.py index 1275460a0..968d5cbfc 100755 --- a/features/environment.py +++ b/features/environment.py @@ -1,5 +1,6 @@ from selenium import webdriver from selenium.webdriver.chrome.service import Service +from selenium.webdriver.support.wait import WebDriverWait from webdriver_manager.chrome import ChromeDriverManager @@ -13,7 +14,7 @@ def browser_init(context): context.driver.maximize_window() context.driver.implicitly_wait(4) - + context.driver.wait = WebDriverWait(context.driver, timeout=10) def before_scenario(context, scenario): print('\nStarted scenario: ', scenario.name) diff --git a/features/steps/main_page_steps.py b/features/steps/main_page_steps.py index 8bac182a3..93c03963c 100644 --- a/features/steps/main_page_steps.py +++ b/features/steps/main_page_steps.py @@ -29,6 +29,6 @@ def search_product(context, product): print(product) context.driver.find_element(By.ID, 'search').send_keys('tea') context.driver.find_element(By.XPATH, "//button[@data-test='@web/Search/SearchButton']").click() - sleep(5) + sleep(9) diff --git a/features/steps/produt_details_steps.py b/features/steps/produt_details_steps.py new file mode 100644 index 000000000..75f5e316b --- /dev/null +++ b/features/steps/produt_details_steps.py @@ -0,0 +1,36 @@ +from selenium.webdriver.common.by import By +from behave import given, then +from time import sleep + + +COLOR_OPTIONS = (By.CSS_SELECTOR, "div[aria-label='Carousel'] li img") +SELECTED_COLOR = (By.CSS_SELECTOR, "[data-test='@web/VariationComponent'] div") + + +@given('Open target product {product_id} page') +def open_target(context, product_id): + context.driver.get(f'https://www.target.com/p/{product_id}') + sleep(11) + + +@then('Verify user can click through colors') +def click_and_verify_colors(context): + expected_colors = ['Black', 'White'] + actual_colors = [] + + colors = context.driver.find_elements(*COLOR_OPTIONS) + print(colors) + + for color in colors: + print(color) + color.click() + selected_color = context.driver.find_element(*SELECTED_COLOR).text + print('Current color text', selected_color) + selected_color = selected_color.split('\n')[1] + actual_colors.append(selected_color) + print('actual_colors list: ', actual_colors) + + assert expected_colors == actual_colors, f'Expected {expected_colors} did not match actual {actual_colors}' + + + diff --git a/features/steps/search_results.py b/features/steps/search_results.py index a6af54d97..1d9540a97 100644 --- a/features/steps/search_results.py +++ b/features/steps/search_results.py @@ -1,36 +1,44 @@ from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC from behave import given, when, then -from time import sleep +#from time import sleep @then('Verify that correct search results shown for {product}') def verify_results(context, product): - actual_result = context.driver.find_element(By.XPATH, "//div[@data-test='resultsHeading']").text - assert product in actual_result, f'Expected {product}, got actual {actual_result}' + actual_result = context.driver.find_element(By.XPATH, "//div[@data-test='resultsHeading']").text + assert product in actual_result, f'Expected {product}, got actual {actual_result}' ADD_TO_CART_BTN = (By.CSS_SELECTOR, "[id*='addToCartButton']") ADD_TO_CART_BTN_SIDE_NAV = (By.CSS_SELECTOR, "[data-test='content-wrapper'] [id*='addToCart']") SIDE_NAV_PRODUCT_NAME = (By.CSS_SELECTOR, "[data-test='content-wrapper'] h4") - +CART_ITEM_TITLE = (By.CSS_SELECTOR, "[data-test='cartItem-title']") @when('Click on Add to Cart button') def click_add_to_cart(context): - sleep(5) context.driver.find_element(*ADD_TO_CART_BTN).click() #If we want to find a specific product #context.driver.find_element(By.CSS_SELECTOR, "[data-test='chooseOptionsButton']").click() - sleep(3) + context.driver.wait.until( + EC.visibility_of_element_located(SIDE_NAV_PRODUCT_NAME), + message='Side navigation product name not visible' + ) + #sleep(9) @when('Store product name') def store_product_name(context): - context.product_name = context.driver.find_element(*SIDE_NAV_PRODUCT_NAME).text + context.product_name = context.driver.find_element(*CART_ITEM_TITLE).text print(f'Product stored: {context.product_name}') @when('Confirm Add to Cart button from side navigation') def side_nav_click_add_to_cart(context): context.driver.find_element(*ADD_TO_CART_BTN_SIDE_NAV).click() - sleep(3) + context.driver.wait.until( + EC.element_to_be_clickable(ADD_TO_CART_BTN_SIDE_NAV), + message='add to cart button not clickable' + ) + #sleep(9) diff --git a/features/tests/cart.feature b/features/tests/cart.feature index 45064fc99..4ef43cb06 100644 --- a/features/tests/cart.feature +++ b/features/tests/cart.feature @@ -7,12 +7,12 @@ Feature: Test for cart functionality Then Verify cart Empty message shown -Scenario: User can add a product to cart + Scenario: User can add a product to cart Given Open target main page When Search for tea And Click on Add to Cart button - And Store product name And Confirm Add to Cart button from side navigation And Open cart page + And Store product name Then Verify cart has 1 item(s) And Verify cart has correct product \ 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..ca35a922c --- /dev/null +++ b/features/tests/product_details.feature @@ -0,0 +1,6 @@ +# Created by LOPA at 9/22/2024 +Feature: Tests for product page + + Scenario: User can select colors + Given Open target product A-80690060 page + Then Verify user can click through colors From 4e03620de392740edb699971e5049f1dcc8020e5 Mon Sep 17 00:00:00 2001 From: Lopamudra S Date: Thu, 26 Sep 2024 12:38:18 -0600 Subject: [PATCH 7/7] Committing changes for lesson 6 --- app/__init__.py | 0 app/application.py | 15 +++++++++++++++ features/environment.py | 2 ++ features/steps/cart_steps.py | 6 +++--- features/steps/main_page_steps.py | 6 ++++-- features/steps/search_results.py | 2 ++ pages/__init__.py | 0 pages/base_page.py | 19 +++++++++++++++++++ pages/cart_page.py | 11 +++++++++++ pages/header.py | 18 ++++++++++++++++++ pages/main_page.py | 7 +++++++ pages/search_results_page.py | 10 ++++++++++ 12 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 app/__init__.py create mode 100644 app/application.py create mode 100644 pages/__init__.py create mode 100644 pages/base_page.py create mode 100644 pages/cart_page.py create mode 100644 pages/header.py create mode 100644 pages/main_page.py create mode 100644 pages/search_results_page.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/application.py b/app/application.py new file mode 100644 index 000000000..dc9d20762 --- /dev/null +++ b/app/application.py @@ -0,0 +1,15 @@ +from pages.base_page import Page +from pages.cart_page import CartPage +from pages.header import Header +from pages.main_page import MainPage +from pages.search_results_page import SearchResultsPage + + +class Application: + + def __init__(self, driver): + self.page = Page(driver) + self.cart_page = CartPage(driver) + self.main_page = MainPage(driver) + self.header = Header(driver) + self.search_results_page = SearchResultsPage(driver) \ No newline at end of file diff --git a/features/environment.py b/features/environment.py index 968d5cbfc..37aff8ecf 100755 --- a/features/environment.py +++ b/features/environment.py @@ -3,6 +3,7 @@ from selenium.webdriver.support.wait import WebDriverWait from webdriver_manager.chrome import ChromeDriverManager +from app.application import Application def browser_init(context): """ @@ -15,6 +16,7 @@ def browser_init(context): context.driver.maximize_window() context.driver.implicitly_wait(4) context.driver.wait = WebDriverWait(context.driver, timeout=10) + context.app = Application(context.driver) def before_scenario(context, scenario): print('\nStarted scenario: ', scenario.name) diff --git a/features/steps/cart_steps.py b/features/steps/cart_steps.py index a2007c3a6..31947ace6 100644 --- a/features/steps/cart_steps.py +++ b/features/steps/cart_steps.py @@ -13,10 +13,10 @@ def open_cart(context): @then('Verify cart Empty message shown') def verify_cart_empty(context): - expected_text='Your cart is empty' - actual_text= context.driver.find_element(By.CSS_SELECTOR, "[data-test='boxEmptyMsg'] h1").text + expected_text = 'Your cart is empty' + actual_text = context.driver.find_element(By.CSS_SELECTOR, "[data-test='boxEmptyMsg'] h1").text assert expected_text == actual_text, f'Expected {expected_text} did not match actual {actual_text}' - + context.app.cart_page.verify_cart_empty() @then('Verify cart has correct product') def verify_product_name(context): diff --git a/features/steps/main_page_steps.py b/features/steps/main_page_steps.py index 93c03963c..31f3008ae 100644 --- a/features/steps/main_page_steps.py +++ b/features/steps/main_page_steps.py @@ -5,11 +5,12 @@ @given('Open target main page') def open_target(context): - context.driver.get('https://www.target.com/') - + #context.driver.get('https://www.target.com/') + context.app.main_page.open_main() @when('Click on cart icon') def click_cart(context): + #context.app.header.click_cart() context.driver.find_element(By.XPATH, "//a[@data-test='@web/CartLink']").click() sleep(3) @@ -30,5 +31,6 @@ def search_product(context, product): context.driver.find_element(By.ID, 'search').send_keys('tea') context.driver.find_element(By.XPATH, "//button[@data-test='@web/Search/SearchButton']").click() sleep(9) + context.app.header.search_product(product) diff --git a/features/steps/search_results.py b/features/steps/search_results.py index 1d9540a97..03d53d28f 100644 --- a/features/steps/search_results.py +++ b/features/steps/search_results.py @@ -8,6 +8,7 @@ def verify_results(context, product): actual_result = context.driver.find_element(By.XPATH, "//div[@data-test='resultsHeading']").text assert product in actual_result, f'Expected {product}, got actual {actual_result}' + context.app.search_results_page.verify_results(product) ADD_TO_CART_BTN = (By.CSS_SELECTOR, "[id*='addToCartButton']") @@ -42,3 +43,4 @@ def side_nav_click_add_to_cart(context): ) #sleep(9) + diff --git a/pages/__init__.py b/pages/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pages/base_page.py b/pages/base_page.py new file mode 100644 index 000000000..44f0fd07e --- /dev/null +++ b/pages/base_page.py @@ -0,0 +1,19 @@ +class Page: + + def __init__(self, driver): + self.driver = driver + + def open(self, url): + self.driver.get(url) + + def find_element(self, *locator): + return self.driver.find_element(*locator) + + def find_elements(self, *locator): + return self.driver.find_elements(*locator) + + def click(self, *locator): + self.driver.find_element(*locator).click() + + def input_text(self, text, *locator): + self.driver.find_element(*locator).send_keys(text) \ No newline at end of file diff --git a/pages/cart_page.py b/pages/cart_page.py new file mode 100644 index 000000000..c202aacd2 --- /dev/null +++ b/pages/cart_page.py @@ -0,0 +1,11 @@ +from selenium.webdriver.common.by import By + +from pages.base_page import Page + +class CartPage(Page): + CART_EMPTY_TXT = (By.CSS_SELECTOR, "[data-test='boxEmptyMsg'] h1") + + def verify_cart_empty(self): + expected_text='Your cart is empty' + actual_text= self.driver.find_element(*self.CART_EMPTY_TXT).text + assert expected_text == actual_text, f'Expected {expected_text} did not match actual {actual_text}' \ No newline at end of file diff --git a/pages/header.py b/pages/header.py new file mode 100644 index 000000000..968fcb780 --- /dev/null +++ b/pages/header.py @@ -0,0 +1,18 @@ +from selenium.webdriver.common.by import By +from time import sleep + +from pages.base_page import Page + +class Header(Page): + CART_BTN = (By.CSS_SELECTOR, "[data-test='@web/CartLink']") + SEARCH_FIELD = (By.ID, 'search') + SEARCH_BTN = (By.XPATH, "//button[@data-test='@web/Search/SearchButton']") + + def search_product(self, product): + self.input_text(product, *self.SEARCH_FIELD) + self.click(*self.SEARCH_BTN) + sleep(6) # wait for search results page to load + + def click_cart(self): + self.click(*self.CART_BTN) + # self.driver.find_element(*self.CART_BTN).click() \ No newline at end of file diff --git a/pages/main_page.py b/pages/main_page.py new file mode 100644 index 000000000..cd05cee1e --- /dev/null +++ b/pages/main_page.py @@ -0,0 +1,7 @@ +from pages.base_page import Page + + +class MainPage(Page): + + def open_main(self): + self.open('https://www.target.com/') \ No newline at end of file diff --git a/pages/search_results_page.py b/pages/search_results_page.py new file mode 100644 index 000000000..5438e98dc --- /dev/null +++ b/pages/search_results_page.py @@ -0,0 +1,10 @@ +from selenium.webdriver.common.by import By + +from pages.base_page import Page + +class SearchResultsPage(Page): + SEARCH_RESULTS_HEADER = (By.XPATH, "//div[@data-test='resultsHeading']") + + def verify_results(self, product): + actual_result = self.driver.find_element(*self.SEARCH_RESULTS_HEADER).text + assert product in actual_result, f'Expected {product}, got actual {actual_result}' \ No newline at end of file