From e179ea2c3e427fc20c9f6d2571ac4a23fcea53c2 Mon Sep 17 00:00:00 2001 From: "jay404077047@gmail.com" Date: Thu, 10 Apr 2025 21:45:05 -0500 Subject: [PATCH 01/17] Update automated search script --- features/tests/product_search.feature | 4 +++- sample_script.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/features/tests/product_search.feature b/features/tests/product_search.feature index 36d6913cf..f072ebd7d 100755 --- a/features/tests/product_search.feature +++ b/features/tests/product_search.feature @@ -1,6 +1,8 @@ Feature: Test Scenarios for Search functionality - Scenario: User can search for a product + + +kk Scenario: User can search for a product Given Open Google page When Input Car into search field And Click on search icon diff --git a/sample_script.py b/sample_script.py index 23d64fc06..67d712d23 100755 --- a/sample_script.py +++ b/sample_script.py @@ -7,6 +7,8 @@ # 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) From 6ba7e8fdb86ed60ac5b37e95b498113c9348aec5 Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 11:23:28 -0500 Subject: [PATCH 02/17] Create hw3 --- hw3 | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 hw3 diff --git a/hw3 b/hw3 new file mode 100644 index 000000000..12cc2986d --- /dev/null +++ b/hw3 @@ -0,0 +1,41 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from behave import given, when, then +import time + +@given('I open the Target homepage') +def step_open_target(context): + context.driver = webdriver.Chrome() + context.driver.get("https://www.target.com") + context.driver.maximize_window() + time.sleep(3) + +@when('I click on the Cart icon') +def step_click_cart(context): + cart_icon = context.driver.find_element(By.ID, "utilityNav-cart") + cart_icon.click() + time.sleep(3) + +@then('I should see the message "Your cart is empty"') +def step_verify_empty_cart(context): + empty_text = context.driver.find_element(By.XPATH, "//*[contains(text(), 'Your cart is empty')]") + assert "Your cart is empty" in empty_text.text + context.driver.quit() + +@when('I click on the Sign In button') +def step_click_signin_button(context): + signin = context.driver.find_element(By.XPATH, "//span[text()='Sign in']") + signin.click() + time.sleep(2) + +@when('I click Sign In from the right side menu') +def step_click_signin_menu(context): + menu_signin = context.driver.find_element(By.XPATH, "//a[@data-test='accountNav-signIn']") + menu_signin.click() + time.sleep(3) + +@then('I should see the Sign In form') +def step_verify_signin_form(context): + form_header = context.driver.find_element(By.XPATH, "//h1[contains(text(),'Sign into your Target account')]") + assert "Sign into your Target account" in form_header.text + context.driver.quit() From 53658a1bfa6509e6ea8071bab8f05d3875427526 Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 11:34:18 -0500 Subject: [PATCH 03/17] hw4 hw4 target --- hw4 | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 hw4 diff --git a/hw4 b/hw4 new file mode 100644 index 000000000..fc0c7554d --- /dev/null +++ b/hw4 @@ -0,0 +1,38 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.common.keys import Keys +from behave import given, when, then +import time + +@given("I open the Target homepage") +def step_open_homepage(context): + context.driver = webdriver.Chrome() + context.driver.get("https://www.target.com") + context.driver.maximize_window() + time.sleep(3) + +@when('I search for "headphones"') +def step_search(context): + search_box = context.driver.find_element(By.ID, "search") + search_box.send_keys("headphones") + search_box.send_keys(Keys.RETURN) + time.sleep(3) + +@when("I add the first product to the cart") +def step_add_first_product(context): + product = context.driver.find_elements(By.CSS_SELECTOR, '[data-test="product-title"]')[0] + product.click() + time.sleep(3) + + add_to_cart_button = context.driver.find_element(By.XPATH, "//button[contains(text(), 'Add to cart')]") + add_to_cart_button.click() + time.sleep(3) + +@then("I should see the product in the cart") +def step_verify_cart(context): + context.driver.get("https://www.target.com/co-cart") + time.sleep(3) + + cart_items = context.driver.find_elements(By.CSS_SELECTOR, '[data-test="cartItem"]') + assert len(cart_items) > 0, "Cart is empty after adding product." + context.driver.quit() From a6049981fdaf75f9d37759642f4aac73b6329ee3 Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 11:39:04 -0500 Subject: [PATCH 04/17] Create hw5 --- hw5 | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 hw5 diff --git a/hw5 b/hw5 new file mode 100644 index 000000000..8bf16f5b0 --- /dev/null +++ b/hw5 @@ -0,0 +1,38 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from behave import given, when, then + +@given("I open the product page with multiple colors") +def step_open_product(context): + context.driver = webdriver.Chrome() + context.driver.get("https://www.target.com/p/A-54551690") + context.driver.maximize_window() + context.wait = WebDriverWait(context.driver, 10) + context.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '[data-test="colorSwatch"]'))) + +@when("I click through each color option") +def step_click_colors(context): + # Collect all swatches + swatches = context.driver.find_elements(By.CSS_SELECTOR, '[data-test="colorSwatch"]') + context.selected_colors = [] + + for i in range(len(swatches)): + swatches = context.driver.find_elements(By.CSS_SELECTOR, '[data-test="colorSwatch"]') + swatches[i].click() + + # Wait for the swatch to be selected + context.wait.until( + EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test="colorSwatch"].Selected')) + ) + + selected = context.driver.find_element(By.CSS_SELECTOR, '[data-test="colorSwatch"].Selected') + color_name = selected.get_attribute("aria-label") + context.selected_colors.append(color_name) + +@then("I should verify that the color is selected") +def step_verify_selected_color(context): + assert len(context.selected_colors) > 0 + print("Verified selected colors:", context.selected_colors) + context.driver.quit() From ba6bd9f93b49ced148a5379cc302640cf40b152f Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 11:43:59 -0500 Subject: [PATCH 05/17] Create hw6 --- hw6 | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 hw6 diff --git a/hw6 b/hw6 new file mode 100644 index 000000000..e1c60be45 --- /dev/null +++ b/hw6 @@ -0,0 +1,9 @@ +from selenium.webdriver.common.by import By + +class HomePage: + def __init__(self, driver): + self.driver = driver + self.cart_icon = (By.ID, "utilityNav-cart") + + def click_cart_icon(self): + self.driver.find_element(*self.cart_icon).click() From edae285a1bfecfe87baf24d0a45571d54953fb4a Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 11:51:24 -0500 Subject: [PATCH 06/17] Create hw7 --- hw7 | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 hw7 diff --git a/hw7 b/hw7 new file mode 100644 index 000000000..3b7632b50 --- /dev/null +++ b/hw7 @@ -0,0 +1,71 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +class SearchResultsPage: + def __init__(self, driver): + self.driver = driver + self.wait = WebDriverWait(driver, 10) + + def click_first_product(self): + first_product = self.wait.until( + EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[data-test="product-title"]')) + )[0] + first_product.click() + +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +class ProductPage: + def __init__(self, driver): + self.driver = driver + self.wait = WebDriverWait(driver, 10) + + def add_to_cart(self): + add_button = self.wait.until( + EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Add to cart')]")) + ) + add_button.click() + +from behave import given, when, then +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.common.keys import Keys +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from pages.search_results_page import SearchResultsPage +from pages.product_page import ProductPage +from pages.cart_page import CartPage +from pages.home_page import HomePage + +@given("I open the Target homepage") +def step_open_home(context): + context.driver = webdriver.Chrome() + context.driver.get("https://www.target.com") + context.driver.maximize_window() + context.wait = WebDriverWait(context.driver, 10) + context.search_results_page = SearchResultsPage(context.driver) + context.product_page = ProductPage(context.driver) + context.cart_page = CartPage(context.driver) + +@when('I search for "headphones"') +def step_search_product(context): + search_box = context.wait.until(EC.presence_of_element_located((By.ID, "search"))) + search_box.send_keys("headphones") + search_box.send_keys(Keys.RETURN) + +@when("I click on the first product") +def step_click_product(context): + context.search_results_page.click_first_product() + +@when("I add the product to the cart") +def step_add_to_cart(context): + context.product_page.add_to_cart() + +@then("I should see the product in the cart") +def step_verify_cart(context): + context.cart_page.open_cart() + assert context.cart_page.is_cart_not_empty() + context.driver.quit + From 6a98eeb146bad0a622c7b981dc8954dad71eb621 Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 11:56:42 -0500 Subject: [PATCH 07/17] Create hw8 --- hw8 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 hw8 diff --git a/hw8 b/hw8 new file mode 100644 index 000000000..e1819f30b --- /dev/null +++ b/hw8 @@ -0,0 +1,55 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +class SignInPage: + def __init__(self, driver): + self.driver = driver + self.wait = WebDriverWait(driver, 10) + self.terms_link = (By.LINK_TEXT, "Target terms & conditions") + + def click_terms_link(self): + self.wait.until(EC.element_to_be_clickable(self.terms_link)).click() + + + from behave import given, when, then +from selenium import webdriver +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from pages.sign_in_page import SignInPage + +@given("I open the Target sign in page") +def step_open_signin(context): + context.driver = webdriver.Chrome() + context.driver.get("https://www.target.com/login") + context.driver.maximize_window() + context.wait = WebDriverWait(context.driver, 10) + context.sign_in_page = SignInPage(context.driver) + +@when("I store the original window") +def step_store_original_window(context): + context.original_window = context.driver.current_window_handle + +@when("I click on the Target Terms and Conditions link") +def step_click_terms_link(context): + context.sign_in_page.click_terms_link() + +@when("I switch to the newly opened window") +def step_switch_to_new_window(context): + context.wait.until(EC.number_of_windows_to_be(2)) + for handle in context.driver.window_handles: + if handle != context.original_window: + context.driver.switch_to.window(handle) + break + +@then("I should see the Terms and Conditions page") +def step_verify_terms_page(context): + context.wait.until(EC.title_contains("Terms")) + assert "terms" in context.driver.title.lower() + +@then("I close the new window and switch back to original") +def step_close_and_return(context): + context.driver.close() + context.driver.switch_to.window(context.original_window) + assert "target.com/login" in context.driver.current_url + context.driver.quit() From b59b715dc256ae4fec7499a3b72a387b013f2959 Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 12:48:48 -0500 Subject: [PATCH 08/17] Create hw11 --- hw11 | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 hw11 diff --git a/hw11 b/hw11 new file mode 100644 index 000000000..1349a4fe4 --- /dev/null +++ b/hw11 @@ -0,0 +1,66 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +driver = webdriver.Chrome() +driver.get("https://help.target.com/help") + +try: + # Assume there's a search bar instead of a traditional dropdown + search_input = WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.ID, "searchTerm")) # Adjust ID if needed + ) + search_input.send_keys("Returns") + + search_button = driver.find_element(By.ID, "searchSubmit") # Adjust if needed + search_button.click() + + WebDriverWait(driver, 10).until( + EC.url_contains("childcat=Returns") + ) + + assert "Returns" in driver.title or "Returns" in driver.page_source + print("Help page search test passed.") + +finally: + driver.quit() + +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +driver = webdriver.Chrome() +driver.get("https://www.target.com/account/signin") + +try: + # Step 1: Enter valid email and continue + email_input = WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.ID, "username")) + ) + email_input.send_keys("your_valid_email@example.com") + + continue_button = driver.find_element(By.ID, "continue") + continue_button.click() + + # Step 2: Enter incorrect password + password_input = WebDriverWait(driver, 10).until( + EC.presence_of_element_located((By.ID, "password")) + ) + password_input.send_keys("wrongpassword123") + + sign_in_button = driver.find_element(By.ID, "login") + sign_in_button.click() + + # Step 3: Check for error message + error_msg = WebDriverWait(driver, 10).until( + EC.visibility_of_element_located((By.CLASS_NAME, "styles__ErrorMessage-sc-1tby3k4-2")) # Update if needed + ) + + assert "incorrect" in error_msg.text.lower() + print("Sign-in error test passed.") + +finally: + driver.quit() + From 6ef9490acec386362bd8ec097dff142b9364b230 Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 17:42:43 -0500 Subject: [PATCH 09/17] hw2 --- hw2 | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 hw2 diff --git a/hw2 b/hw2 new file mode 100644 index 000000000..127a2506c --- /dev/null +++ b/hw2 @@ -0,0 +1,39 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +# Set up Chrome in Incognito mode +chrome_options = Options() +chrome_options.add_argument("--incognito") +driver = webdriver.Chrome(options=chrome_options) + +try: + # Step 1: Open Target homepage + driver.get("https://www.target.com/") + + # Step 2 + sign_in_menu = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.XPATH, "//span[text()='Sign in']")) + ) + sign_in_menu.click() + + # Step 3 + sign_in_link = WebDriverWait(driver, 10).until( + EC.element_to_be_clickable((By.XPATH, "//a[@data-test='accountNav-signIn']")) + ) + sign_in_link.click() + + # Step 4 + WebDriverWait(driver, 10).until( + EC.visibility_of_element_located((By.XPATH, "//h1[contains(text(), 'Sign into your Target account')]")) + ) + print("✅ Sign-in header is visible") + + # Verify SignIn + sign_in_button = driver.find_element(By.ID, "login") + print("✅ Sign-in button is located") + +finally: + driver.quit() From 81d0f4e730731af9e82d9e69d2aa213a31d6c38a Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 17:52:00 -0500 Subject: [PATCH 10/17] Update hw3 --- hw3 | 76 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/hw3 b/hw3 index 12cc2986d..9f80c0996 100644 --- a/hw3 +++ b/hw3 @@ -1,41 +1,55 @@ + + from selenium import webdriver from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC from behave import given, when, then -import time - -@given('I open the Target homepage') -def step_open_target(context): - context.driver = webdriver.Chrome() - context.driver.get("https://www.target.com") - context.driver.maximize_window() - time.sleep(3) - -@when('I click on the Cart icon') -def step_click_cart(context): - cart_icon = context.driver.find_element(By.ID, "utilityNav-cart") - cart_icon.click() - time.sleep(3) +from selenium.webdriver.chrome.options import Options -@then('I should see the message "Your cart is empty"') -def step_verify_empty_cart(context): - empty_text = context.driver.find_element(By.XPATH, "//*[contains(text(), 'Your cart is empty')]") - assert "Your cart is empty" in empty_text.text +def before_all(context): + chrome_options = Options() + chrome_options.add_argument("--incognito") + context.driver = webdriver.Chrome(options=chrome_options) + context.driver.implicitly_wait(10) + +def after_all(context): context.driver.quit() -@when('I click on the Sign In button') -def step_click_signin_button(context): - signin = context.driver.find_element(By.XPATH, "//span[text()='Sign in']") - signin.click() - time.sleep(2) +@given('the user opens the Target homepage') +def step_open_homepage(context): + context.driver.get("https://www.target.com/") + +@when('the user clicks on the Cart icon') +def step_click_cart_icon(context): + cart_icon = context.driver.find_element(By.XPATH, "//a[@data-test='@web/CartLink']") + cart_icon.click() + +@then('the user should see the "Your cart is empty" message') +def step_verify_empty_cart(context): + empty_message = WebDriverWait(context.driver, 10).until( + EC.visibility_of_element_located((By.XPATH, "//div[contains(text(),'Your cart is empty')]")) + ) + assert empty_message.is_displayed() -@when('I click Sign In from the right side menu') +@when('the user clicks on the Sign In menu') def step_click_signin_menu(context): - menu_signin = context.driver.find_element(By.XPATH, "//a[@data-test='accountNav-signIn']") - menu_signin.click() - time.sleep(3) + sign_in_menu = WebDriverWait(context.driver, 10).until( + EC.element_to_be_clickable((By.XPATH, "//span[text()='Sign in']")) + ) + sign_in_menu.click() -@then('I should see the Sign In form') +@when('the user clicks on Sign In from the side navigation') +def step_click_signin_nav(context): + nav_signin = WebDriverWait(context.driver, 10).until( + EC.element_to_be_clickable((By.XPATH, "//a[@data-test='accountNav-signIn']")) + ) + nav_signin.click() + +@then('the Sign In form should be visible') def step_verify_signin_form(context): - form_header = context.driver.find_element(By.XPATH, "//h1[contains(text(),'Sign into your Target account')]") - assert "Sign into your Target account" in form_header.text - context.driver.quit() + signin_header = WebDriverWait(context.driver, 10).until( + EC.visibility_of_element_located((By.XPATH, "//h1[contains(text(), 'Sign into your Target account')]")) + ) + assert signin_header.is_displayed() + From 2d15ad93aa92643fe4fdba9f4a40456f31404e2e Mon Sep 17 00:00:00 2001 From: majesticj Date: Fri, 11 Apr 2025 17:58:54 -0500 Subject: [PATCH 11/17] hw4 updated --- hw4 updated | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 hw4 updated diff --git a/hw4 updated b/hw4 updated new file mode 100644 index 000000000..288490183 --- /dev/null +++ b/hw4 updated @@ -0,0 +1,70 @@ +from behave import given, when, then +from selenium.webdriver.common.by import By + +@given('I open the Target homepage') +def step_impl(context): + context.browser.get("https://www.target.com/") + +@when('I search for the product "{product_name}"') +def step_impl(context, product_name): + search_box = context.browser.find_element(By.ID, 'search') + search_box.send_keys(product_name) + search_box.submit() + +@then('I should see search results for "{product_name}"') +def step_impl(context, product_name): + context.browser.implicitly_wait(10) + assert product_name.lower() in context.browser.page_source.lower() + + + from behave import given, then +from selenium.webdriver.common.by import By + +@given('I open the Target Circle page') +def step_impl(context): + context.browser.get("https://www.target.com/circle") + +@then('I should see at least 10 benefit cells') +def step_impl(context): + context.browser.implicitly_wait(10) + cells = context.browser.find_elements(By.CSS_SELECTOR, '[data-test="circle-benefit-card"]') + assert len(cells) >= 10, f"Only found {len(cells)} benefit cells" + + + from behave import given, when, then +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +@given('I search for a product "{product_name}"') +def step_impl(context, product_name): + context.browser.get("https://www.target.com/") + search_box = context.browser.find_element(By.ID, 'search') + search_box.send_keys(product_name) + search_box.submit() + +@when('I add the first product to the cart') +def step_impl(context): + WebDriverWait(context.browser, 10).until( + EC.presence_of_element_located((By.CSS_SELECTOR, '[data-test="product-title"]')) + ) + first_product = context.browser.find_element(By.CSS_SELECTOR, '[data-test="product-title"]') + first_product.click() + + add_button = WebDriverWait(context.browser, 10).until( + EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test="addToCartButton"]')) + ) + add_button.click() + +@then('I should see the product in my cart') +def step_impl(context): + cart_icon = WebDriverWait(context.browser, 10).until( + EC.element_to_be_clickable((By.ID, 'utilityNav-cart')) + ) + cart_icon.click() + + cart_items = WebDriverWait(context.browser, 10).until( + EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[data-test="cart-item"]')) + ) + assert len(cart_items) > 0 + From b744730286b57b50ae37d4cdddaf9bd53ef2eb90 Mon Sep 17 00:00:00 2001 From: majesticj Date: Tue, 15 Apr 2025 08:04:40 -0500 Subject: [PATCH 12/17] Create redo hw6 redo hw6 --- redo hw6 | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 redo hw6 diff --git a/redo hw6 b/redo hw6 new file mode 100644 index 000000000..372d2a50c --- /dev/null +++ b/redo hw6 @@ -0,0 +1,62 @@ +project/ +├── features/ +│ ├── steps/ +│ ├── pages/ +│ │ ├── home_page.py +│ │ ├── search_results_page.py +│ │ ├── cart_page.py +│ └── ... + + +from selenium.webdriver.common.by import By + +class HomePage: + def __init__(self, driver): + self.driver = driver + self.search_box = (By.ID, 'search') + self.cart_icon = (By.ID, 'utilityNav-cart') + + def open(self): + self.driver.get("https://www.target.com/") + + def search_product(self, product_name): + self.driver.find_element(*self.search_box).send_keys(product_name) + self.driver.find_element(*self.search_box).submit() + + def click_cart_icon(self): + self.driver.find_element(*self.cart_icon).click() + +from selenium.webdriver.common.by import By + +class CartPage: + def __init__(self, driver): + self.driver = driver + self.empty_cart_text = (By.XPATH, "//*[contains(text(), 'Your cart is empty')]") + + def is_cart_empty_message_displayed(self): + return self.driver.find_element(*self.empty_cart_text).is_displayed() + +Feature: Cart page + + Scenario: “Your cart is empty” message is shown for empty cart + Given I open the Target homepage + When I click on the cart icon + Then I should see the “Your cart is empty” message + +from behave import given, when, then +from features.pages.home_page import HomePage +from features.pages.cart_page import CartPage + +@given("I open the Target homepage") +def step_open_homepage(context): + context.home_page = HomePage(context.browser) + context.home_page.open() + +@when("I click on the cart icon") +def step_click_cart(context): + context.home_page.click_cart_icon() + context.cart_page = CartPage(context.browser) + +@then("I should see the “Your cart is empty” message") +def step_verify_empty_cart(context): + assert context.cart_page.is_cart_empty_message_displayed(), "Empty cart message not displayed" From 6c883fc35e2578a9122d7ce7db8d5d9e9b953561 Mon Sep 17 00:00:00 2001 From: majesticj Date: Thu, 17 Apr 2025 08:53:56 -0500 Subject: [PATCH 13/17] Create homework six redo --- homework six | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 homework six diff --git a/homework six b/homework six new file mode 100644 index 000000000..20c1a269f --- /dev/null +++ b/homework six @@ -0,0 +1,118 @@ +project/ +├── features/ +│ ├── steps/ +│ │ ├── cart_steps.py +│ │ ├── search_steps.py +│ ├── pages/ +│ │ ├── base_page.py +│ │ ├── home_page.py +│ │ ├── cart_page.py +│ ├── cart.feature +│ ├── search.feature +├── environment.py + +class Page: + def __init__(self, driver): + self.driver = driver + + def open_url(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.find_element(locator).click() + + def input_text(self, text, locator): + self.find_element(locator).send_keys(text) + +from selenium.webdriver.common.by import By +from features.pages.base_page import Page + +class HomePage(Page): + SEARCH_BAR = (By.ID, "search") + CART_ICON = (By.ID, "utilityNav-cart") + + def open(self): + self.open_url("https://www.target.com/") + + def search_product(self, product): + self.input_text(product, self.SEARCH_BAR) + self.find_element(self.SEARCH_BAR).submit() + + def go_to_cart(self): + self.click(self.CART_ICON) + + +from selenium.webdriver.common.by import By +from features.pages.base_page import Page + +class CartPage(Page): + EMPTY_CART_MESSAGE = (By.XPATH, "//*[contains(text(), 'Your cart is empty')]") + + def is_empty_message_displayed(self): + return self.find_element(self.EMPTY_CART_MESSAGE).is_displayed() + + +Feature: Cart page + + Scenario: “Your cart is empty” message is shown for empty cart + Given I am on the Target homepage + When I go to the cart + Then I should see the empty cart message + + +Feature: Product Search + + Scenario: Search for a product on Target + Given I am on the Target homepage + When I search for "toothbrush" + Then I should see results related to "toothbrush" + +from behave import given, when, then +from features.pages.home_page import HomePage +from features.pages.cart_page import CartPage + +@given("I am on the Target homepage") +def step_impl(context): + context.home = HomePage(context.browser) + context.home.open() + +@when("I go to the cart") +def step_impl(context): + context.home.go_to_cart() + context.cart = CartPage(context.browser) + +@then("I should see the empty cart message") +def step_impl(context): + assert context.cart.is_empty_message_displayed(), "Empty cart message was not displayed" + +from behave import given, when, then +from features.pages.home_page import HomePage + +@given("I am on the Target homepage") +def step_impl(context): + context.home = HomePage(context.browser) + context.home.open() + +@when('I search for "{product}"') +def step_impl(context, product): + context.home.search_product(product) + +@then('I should see results related to "{product}"') +def step_impl(context, product): + assert product.lower() in context.browser.page_source.lower() + + +from selenium import webdriver + +def before_all(context): + context.browser = webdriver.Chrome() + +def after_all(context): + context.browser.quit() + From 90a874b1ff8de9cb9a7ffa03eb3bf7a2b352fdf6 Mon Sep 17 00:00:00 2001 From: majesticj Date: Thu, 17 Apr 2025 17:43:37 -0500 Subject: [PATCH 14/17] homework six 5 homework six 5 --- homework six 5 | 157 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 homework six 5 diff --git a/homework six 5 b/homework six 5 new file mode 100644 index 000000000..6504eedc2 --- /dev/null +++ b/homework six 5 @@ -0,0 +1,157 @@ +#product_search.feature +#header.py +#search_results_page.py +#application.py +#main.page.py +#sample_script.py +#base_page.py +#enviroment.py + +Feature: Test Scenarios for Search functionality + +kk Scenario: User can search for a tes on target + Given Open target main page + When search for tea + And Click on search icon + Then Product results for tea + +from pages base_page import Page + +class MainPage(page): + + def open_main_page (self): + self.open.url('https://www.target.com/') + + +from selenium.import webdriver +from selenium.webdriver.chrome.service import Service +from selenium.webdriver.support.wait import WebDriverWait +from webdriver_manager.chrome import ChromeDriverManager + +from app application import Application + +def browser_init(context): + """ + :param context: Behave context + """ + driver_path = ChromeDriverManager().install() + service = Service(driver_path) + context.driver = webdriver.Chrome(service=service) + + context.driver.maximize_window() + + context.driver.maximize_window() + context.driver.implicitly_wait(4) + context.wait = WebDriverWait(context.driver, 10) + context app = Application(context.driver) + + +def before_scenario(context, scenario): + print('\nStarted scenario: ', scenario.name) + browser_init(context) + + +def before_step(context, step): + print('\nStarted step: ', step) + + +def after_step(context, step): + if step.status == 'failed': + print('\nStep failed: ', step) + + +def after_scenario(context, feature): + context.driver.delete_all_cookies() + context.driver.quit() +from pages base_page Import Page +from pages.header import Header +from pages.main.page import MainPage +from pages.search_results_page import SearchResutsPage + + +class Application: + + def __init__(self, driver): + self.driver = driver + self.main_page = MainPage(driver) + self.header =Header(driver) + self.search_results_page = SearchResutsPage(driver) + + +from selenium.webdriver.common.by import By + +from pages.base_page import Page + + +class SearchResultsPage(Page): + Search_Results_TEXT = By.XPATH, "//div(@data-test=\p=resultsCount"}") + + def verify_search_results(self): + actual_text = self.find_element(self.Search_Results_TEXT).text + assert "tea" in actual_text. f Error. Text tea not in (actual_text) + + +from slenium.webdriver.common.by import By + +from pages.base.page import Page + + +class.Header(page): + +SEARCH_FIELD = (By, ID 'search') +SEARCH_BTN = (By.XPATH, "//button[@data-test= @web/search/button") + + def search(self): + self.input.text(text 'tea, self.SEARCH_FIELD) + self.click(*self.SEARCH_BTN) + +from sample_script import driver + + +class Page: + + def__init (self.driver): + self.driver = driver + + def open.url(self,url) + self.driver.get(url) + + def find_element(selfself. locater): + self.driver.find_element(*locater) + + def click(selfslef, *locator): + self.driver.find_element(*locator) click() + + def input_text(self.text, *locator): + self.driver.find_element(*locator) send_keys(text + +# 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.google.com/') + +# populate search field +search = driver.find_element(By.NAME, 'q') +search.clear() +search.send_keys('Car') + +# wait for 4 sec +sleep(4) + +# click search button +driver.find_element(By.NAME, 'btnK').click() + +# verify search results +assert 'car' in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" +print('Test Passed') + +driver.quit() + From b7a9dfaca1a903c1a77e5aeca69229d319dd31c5 Mon Sep 17 00:00:00 2001 From: majesticj Date: Sat, 19 Apr 2025 03:43:23 -0500 Subject: [PATCH 15/17] homework six again --- homework six 5 | 157 ---------- homework six again | 755 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 755 insertions(+), 157 deletions(-) delete mode 100644 homework six 5 create mode 100644 homework six again diff --git a/homework six 5 b/homework six 5 deleted file mode 100644 index 6504eedc2..000000000 --- a/homework six 5 +++ /dev/null @@ -1,157 +0,0 @@ -#product_search.feature -#header.py -#search_results_page.py -#application.py -#main.page.py -#sample_script.py -#base_page.py -#enviroment.py - -Feature: Test Scenarios for Search functionality - -kk Scenario: User can search for a tes on target - Given Open target main page - When search for tea - And Click on search icon - Then Product results for tea - -from pages base_page import Page - -class MainPage(page): - - def open_main_page (self): - self.open.url('https://www.target.com/') - - -from selenium.import webdriver -from selenium.webdriver.chrome.service import Service -from selenium.webdriver.support.wait import WebDriverWait -from webdriver_manager.chrome import ChromeDriverManager - -from app application import Application - -def browser_init(context): - """ - :param context: Behave context - """ - driver_path = ChromeDriverManager().install() - service = Service(driver_path) - context.driver = webdriver.Chrome(service=service) - - context.driver.maximize_window() - - context.driver.maximize_window() - context.driver.implicitly_wait(4) - context.wait = WebDriverWait(context.driver, 10) - context app = Application(context.driver) - - -def before_scenario(context, scenario): - print('\nStarted scenario: ', scenario.name) - browser_init(context) - - -def before_step(context, step): - print('\nStarted step: ', step) - - -def after_step(context, step): - if step.status == 'failed': - print('\nStep failed: ', step) - - -def after_scenario(context, feature): - context.driver.delete_all_cookies() - context.driver.quit() -from pages base_page Import Page -from pages.header import Header -from pages.main.page import MainPage -from pages.search_results_page import SearchResutsPage - - -class Application: - - def __init__(self, driver): - self.driver = driver - self.main_page = MainPage(driver) - self.header =Header(driver) - self.search_results_page = SearchResutsPage(driver) - - -from selenium.webdriver.common.by import By - -from pages.base_page import Page - - -class SearchResultsPage(Page): - Search_Results_TEXT = By.XPATH, "//div(@data-test=\p=resultsCount"}") - - def verify_search_results(self): - actual_text = self.find_element(self.Search_Results_TEXT).text - assert "tea" in actual_text. f Error. Text tea not in (actual_text) - - -from slenium.webdriver.common.by import By - -from pages.base.page import Page - - -class.Header(page): - -SEARCH_FIELD = (By, ID 'search') -SEARCH_BTN = (By.XPATH, "//button[@data-test= @web/search/button") - - def search(self): - self.input.text(text 'tea, self.SEARCH_FIELD) - self.click(*self.SEARCH_BTN) - -from sample_script import driver - - -class Page: - - def__init (self.driver): - self.driver = driver - - def open.url(self,url) - self.driver.get(url) - - def find_element(selfself. locater): - self.driver.find_element(*locater) - - def click(selfslef, *locator): - self.driver.find_element(*locator) click() - - def input_text(self.text, *locator): - self.driver.find_element(*locator) send_keys(text - -# 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.google.com/') - -# populate search field -search = driver.find_element(By.NAME, 'q') -search.clear() -search.send_keys('Car') - -# wait for 4 sec -sleep(4) - -# click search button -driver.find_element(By.NAME, 'btnK').click() - -# verify search results -assert 'car' in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" -print('Test Passed') - -driver.quit() - diff --git a/homework six again b/homework six again new file mode 100644 index 000000000..fa0f740d3 --- /dev/null +++ b/homework six again @@ -0,0 +1,755 @@ + +##outline +#app +_init_.py +application.py +#features +-steps +_init_.py +cart_steps.py +main_page_steps.py +product_details_steps.py +search_results_steps.py +-tests +_init_.py +cart.feature +main_page_ui-tests.feature +target_search.feature +-init_.py +-enviroment.py +#pages +_init_.py +base_page.py +header.py +main_page.py +search_results_page.py +--gitignore +--readme.md +--css.selectors.py +--locators.py +--playground.py +--requirements.txt +--sample_script.py +--target_search.py +--target_signin.py +## + + +#under app, then click application.py + +from pages base_page Import Page +from pages.header import Header +from pages.main.page import MainPage +from pages.search_results_page import SearchResutsPage + + +class Application: + + def __init__(self, driver): + self.driver = driver + self.main_page = MainPage(driver) + self.header =Header(driver) + self.search_results_page = SearchResutsPage(driver) + + +#under feature, then click steps, then cart_steps.py + +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 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}" + +#under features, steps and main_page_steps.py + +from selenium.webdriver.common.by import By +from behave import given, when, then + +SEARCH_FIELD = (By.ID, 'search') +SEARCH_BTN = (By.XPATH, "//button[@data-test=@web/Search/SearchButton']" +CART_ICON = (By.CSS_SELECTOR, "[id*'utilityNav']") + +@given('Open target main page') +def open_target_main(context): + context.app.main_page.open_main_page() +Context.driver.wait.until( +EC.element_to.be_clickable(SEARCH_FIELD). +message= 'Search field not clickable' + + +@when('Click on cart icon') +def click_cart(context): + context.driver.find_element(By.CSS_SELECTOR, "[data-test='@web/CartLink']").click() + + +@when('Search for {search_word}') +def search_product(context, search_word): + context.app.header.search() + +@then('Verify header has {expected_amount} links') +def verify_header_links(context, expected_amount): + expected_amount = int(expected_amount) # '6' => 6 + links = context.driver.find_elements(By.CSS_SELECTOR, "[data-test*='@web/GlobalHeader/UtilityHeader/']") + assert len(links) == expected_amount, f'Expected {expected_amount} links but got {len(links)}' + + +@then('Verify header is shown') +def verify_header(context): + context.driver.find_element(By.CSS_SELECTOR, "[class*='styles_utilityHeaderContainer']") + + +#under features, stpes and product_details_steps.py + +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(8) + +@then('Verify user can click through colors') +def click_and_verify_colors(context): + expected_colors = ['Blue Tint', 'Denim Blue', 'Marine', 'Raven'] + actual_colors = [] + + colors = context.driver.find_elements(*COLOR_OPTIONS) # [webelement1, webelement2, webelement3] + print(colors) + + for color in colors: + print(color) + color.click() + + selected_color = context.driver.find_element(*SELECTED_COLOR).text # 'Color\nBlack' + print('Current color text', selected_color) + + selected_color = selected_color.split('\n')[1] # remove 'Color\n' part, keep Black' + 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}' + +#features, steps and search_results_steps.py + +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 + +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") +LISTINGS = (By.CSS_SELECTOR, "[data-test='@web/site-top-of-funnel/ProductCardWrapper']") +PRODUCT_TITLE = (By.CSS_SELECTOR, "[data-test='product-title']") +PRODUCT_IMG = (By.CSS_SELECTOR, 'img') + +@when('Click on Add to Cart button') +def click_add_to_cart(context): + context.driver.find_element(*ADD_TO_CART_BTN).click() # always clicks on 1st Add to cart btn + context.driver.wait.until( + EC.visibility_of_element_located(SIDE_NAV_PRODUCT_NAME), + message='Side navigation product name not visible' + ) + +@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) + + +@then('Verify that correct search results shown for {product}') +def verify_results(context, product): + context.app.search_results_page.verify_results(product) + + +@then('Verify that every product has a name and an image') +def verify_products_name_img(context): + # To see ALL listings (comment out if you only check top ones): + context.driver.execute_script("window.scrollBy(0,2000)", "") + sleep(4) + context.driver.execute_script("window.scrollBy(0,2000)", "") + + all_products = context.driver.find_elements(*LISTINGS) # [WebEl1, WebEl2, WebEl3, WebEl4] + + for product in all_products: + title = product.find_element(*PRODUCT_TITLE).text + assert title, 'Product title not shown' + print(title) + product.find_element(*PRODUCT_IMG) + +#tests, cart_feature + +Feature: Cart tests + + Scenario: User can see Cart Empty message + Given Open target main page + When Click on cart icon + Then Verify Cart Empty message shown + +#main_page_ui_tests.feature + +Feature: Tests for main page UI + + Scenario: Verify header in shown + Given Open Target main page + Then Verify header is shown + + Scenario: Verify header has correct amount links + Given Open Target main page + Then Verify header is shown + And Verify header has 6 links + +#product_details.feature + +Feature: Tests for product page + + Scenario: User can select colors + Given Open target product A-54551690 page + Then Verify user can click through colors + +#target_search.feature + +Feature: Tests for Target Search functionality + + Scenario: User can search for coffee + Given Open target main page + When Search for coffee + Then Verify that correct search results shown for coffee + + Scenario: User can search for tea + Given Open target main page + When Search for tea + Then Verify that correct search results shown for tea + + Scenario Outline: User can search for product + Given Open target main page + When Search for + Then Verify that correct search results shown for + Examples: + |search_word |search_result | + |coffee |coffee | + |tea |tea | + |mug |mug | + |sugar |sugar | + + Scenario: User can add a product to cart + Given Open target main page + When Search for mug + 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 + + Scenario: Verify that user can see product names and images + Given Open target main page + When Search for AirPods (3rd Generation) + Then Verify that every product has a name and an image + +#tests and enviroment.py + +from selenium.import webdriver +from selenium.webdriver.chrome.service import Service +from selenium.webdriver.support.wait import WebDriverWait +from webdriver_manager.chrome import ChromeDriverManager + +from app application import Application + +def browser_init(context): + """ + :param context: Behave context + """ + driver_path = ChromeDriverManager().install() + service = Service(driver_path) + context.driver = webdriver.Chrome(service=service) + + context.driver.maximize_window() + + context.driver.maximize_window() + context.driver.implicitly_wait(4) + context.wait = WebDriverWait(context.driver, 10) + context app = Application(context.driver) + + +def before_scenario(context, scenario): + print('\nStarted scenario: ', scenario.name) + browser_init(context) + +def before_step(context, step): + print('\nStarted step: ', step) + + +def after_step(context, step): + if step.status == 'failed': + print('\nStep failed: ', step) + + +def after_scenario(context, feature): + context.driver.delete_all_cookies() + context.driver.quit() + +#pages and base_page.py + +class Page: + + def__init (self.driver): + self.driver = driver + + def open.url(self,url) + self.driver.get(url) + + def find_element(self. *locater): + self.driver.find_element(*locater) + + def click(selfslef, *locator): + self.driver.find_element(*locator) click() + + def input_text(self.text, *locator): + self.driver.find_element(*locator) send_keys(text + + +#header.py + +from slenium.webdriver.common.by import By +from pages.base.page import Page + +class.Header(page): + +SEARCH_FIELD = (By, ID 'search') +SEARCH_BTN = (By.XPATH, "//button[@data-test= @web/search/button") + + def search(self): + self.input.text(text 'tea, self.SEARCH_FIELD) + self.click(*self.SEARCH_BTN) + + +#main_page.py + +from pages.base_page import Page + +class MainPage(Page): + + def open_main_page(self): + self.open('https://www.target.com/') + +#search_results_page.py + +from selenium.webdriver.common.by import By + +from pages.base_page import Page + + +class SearchResultsPage(Page): + Search_Results_TEXT = By.XPATH, "//div(@data-test=\p=resultsCount"}") + + def verify_search_results(self): + actual_text = self.find_element(self.Search_Results_TEXT).text + assert "tea" in actual_text. f Error. Text tea not in (actual_text) + + +#.gitignore +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# PyBuilder +target/ + +# pyenv +.python-version + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# idea +.idea/ +*.iws/ + +# logs +*.log + +# app files +app_binaries/ +*.apk +*.app +*.ipa + +.DS_Store + +/features/test_results/* + +# Screenshots +*.png + +#css_selectors.py + +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/') + +# By CSS, by ID, use #: +driver.find_element(By.CSS_SELECTOR, "#twotabsearchtextbox") +# Same as => +driver.find_element(By.ID, "twotabsearchtextbox") # Note, By.ID is preferred if you only use ID + +# By CSS, by class, use . +driver.find_element(By.CSS_SELECTOR, ".nav-progressive-attribute") +driver.find_element(By.CSS_SELECTOR, ".nav-input.nav-progressive-attribute") +# By CSS, by tag & class(es) +driver.find_element(By.CSS_SELECTOR, "input.nav-input.nav-progressive-attribute") +# By CSS, tag & ID & class +driver.find_element(By.CSS_SELECTOR, "input#twotabsearchtextbox.nav-input.nav-progressive-attribute") + +# By CSS, attributes, use []: +driver.find_element(By.CSS_SELECTOR, "[placeholder='Search Amazon']") +driver.find_element(By.CSS_SELECTOR, "input[placeholder='Search Amazon']") +driver.find_element(By.CSS_SELECTOR, ".nav-input[tabindex='0'][placeholder='Search Amazon']") + +# By CSS, attribute, partial match: +driver.find_element(By.CSS_SELECTOR, "[href*='notification_condition_of_use']") +driver.find_element(By.CSS_SELECTOR, "[class*='search']") +driver.find_element(By.CSS_SELECTOR, "[id*='search']") + +# By CSS, from parent => to child, separate by space: +driver.find_element(By.CSS_SELECTOR, "#legalTextRow [href*='privacy']") +driver.find_element(By.CSS_SELECTOR, ".a-box-inner #legalTextRow [href*='privacy']") + +#locators.py +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/') + +# Locate element: +# driver.find_element() # By. / value +# Locate by ID: +driver.find_element(By.ID, 'twotabsearchtextbox') +driver.find_element(By.ID, 'nav-logo-sprites') + +# By Xpath, using 1 attribute +driver.find_element(By.XPATH, "//img[@alt='Shop Studio Pro headphones']") +driver.find_element(By.XPATH, "//input[@name='field-keywords']") +driver.find_element(By.XPATH, "//input[@placeholder='Search Amazon']") +# By Xpath, multiple attributes +driver.find_element(By.XPATH, "//a[@class='nav-a ' and @href='/gp/bestsellers/?ref_=nav_cs_bestsellers' and @tabindex='0']") + +# By Xpath, text: +driver.find_element(By.XPATH, "//a[text()='Best Sellers']") +driver.find_element(By.XPATH, '//a[text()="Best Sellers"]') +# By Xpath, text and attributes: +driver.find_element(By.XPATH, "//a[text()='Best Sellers' and @class='nav-a ']") + +# By attributes or text only, any tag +driver.find_element(By.XPATH, "//*[@name='field-keywords']") +driver.find_element(By.XPATH, "//*[text()='Best Sellers' and @class='nav-a ']") + +# By attributes, parent node => child +driver.find_element(By.XPATH, "//div[@id='nav-main']//a[text()='Best Sellers']") + +#playgrond.py + +# class Page: +# +# def click(self): +# print('Clicking') +# +# def input_text(self, text): +# print(f'Entering text {text}') +# +# def find_element(self): +# print('Searching for element') +# +# +# class LoginPage(Page): +# +# def login(self): +# self.find_element() +# self.click() +# +# +# # login_page = LoginPage() +# # login_page.login() + + +#requirements.txt + +allure-behave +behave +selenium +webdriver-manager + +#sample_script.py + +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 selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +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.implicitly_wait(4) +wait = WebDriverWait(driver, timeout=10) + +# open the url +driver.get('https://www.google.com/') + +# populate search field +search = driver.find_element(By.NAME, 'q') +search.clear() +search.send_keys('Car') + +# wait for 4 sec +# sleep(4) +search_btn = (By.NAME, 'btnK') + +# click search button +wait.until(EC.element_to_be_clickable(search_btn), message='Search button not clickable').click() # => (By.smth, "value") +driver.find_element(*search_btn).click() # => 2: By..smth / "value" + +# verify search results +assert 'car'.lower() in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" +print('Test Passed') + +driver.quit() + +#sample script + +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 selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +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.google.com/') + +# populate search field +search = driver.find_element(By.NAME, 'q') +search.clear() +search.send_keys('Car') + +# wait for 4 sec +sleep(4) + +# click search button +driver.find_element(By.NAME, 'btnK').click() + +# verify search results +assert 'car' in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" +print('Test Passed') + +driver.quit() + +#target.search.py + +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.google.com/') + +# populate search field +search = driver.find_element(By.NAME, 'q') +search.clear() +search.send_keys('Car') + +# wait for 4 sec +sleep(4) + +# click search button +driver.find_element(By.NAME, 'btnK').click() + +# verify search results +assert 'car' in driver.current_url.lower(), f"Expected query not in {driver.current_url.lower()}" +print('Test Passed') + +driver.quit() + +#target_signin.py + +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 + +# Start Chrome browser: +driver_path = ChromeDriverManager().install() +driver = webdriver.Chrome(service=Service(driver_path)) +driver.maximize_window() +driver.implicitly_wait(5) + +# Open target.com +driver.get('https://www.target.com/') + +driver.find_element(By.XPATH, "//*[@data-test='@web/AccountLink']").click() +driver.find_element(By.XPATH, "//*[@data-test='accountNav-signIn']").click() + +expected = 'Sign into your Target account' +actual = driver.find_element(By.XPATH, "//h1/span").text +assert expected == actual, f'Expected {expected} did not match actual {actual}' + +# OR: +driver.find_element(By.XPATH, "//span[text()='Sign into your Target account']") + +# Make sure login button is shown +driver.find_element(By.ID, 'login') + + From ec5f32bd91de99e70d56982bd043e2081bde1335 Mon Sep 17 00:00:00 2001 From: majesticj Date: Mon, 21 Apr 2025 16:38:42 -0500 Subject: [PATCH 16/17] Update homework six again --- homework six again | 139 ++++++++++++++++++++++++++++++++------------- 1 file changed, 98 insertions(+), 41 deletions(-) diff --git a/homework six again b/homework six again index fa0f740d3..fb759ed35 100644 --- a/homework six again +++ b/homework six again @@ -1,6 +1,6 @@ ##outline -#app +#ap _init_.py application.py #features @@ -14,6 +14,8 @@ search_results_steps.py _init_.py cart.feature main_page_ui-tests.feature +product_details.feature +product_search.feature target_search.feature -init_.py -enviroment.py @@ -22,6 +24,7 @@ _init_.py base_page.py header.py main_page.py +home_page.py search_results_page.py --gitignore --readme.md @@ -37,10 +40,10 @@ search_results_page.py #under app, then click application.py -from pages base_page Import Page +from pages.base_page import Page from pages.header import Header -from pages.main.page import MainPage -from pages.search_results_page import SearchResutsPage +from pages.main_page import MainPage +from pages.search_results_page import SearchResultsPage class Application: @@ -49,18 +52,36 @@ class Application: self.driver = driver self.main_page = MainPage(driver) self.header =Header(driver) - self.search_results_page = SearchResutsPage(driver) + self.search_results_page = SearchResultsPage(driver) #under feature, then click steps, then cart_steps.py +from behave import given, when, then +from pages.home_page import HomePage +from pages.cart_page import CartPage + +@given('Open Target main page') +def step_open_main_page(context): + context.home_page = HomePage(context.driver) + context.home_page.load() + +@when('Click on cart icon') +def step_click_cart(context): + context.home_page.click_cart() + context.cart_page = CartPage(context.driver) + +@then('Verify Cart Empty message shown') +def step_verify_cart_empty_message(context): + assert context.cart_page.is_cart_empty_message_displayed(), "Empty cart message not displayed" + + 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') @@ -85,31 +106,25 @@ 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}" + #under features, steps and main_page_steps.py from selenium.webdriver.common.by import By from behave import given, when, then -SEARCH_FIELD = (By.ID, 'search') -SEARCH_BTN = (By.XPATH, "//button[@data-test=@web/Search/SearchButton']" -CART_ICON = (By.CSS_SELECTOR, "[id*'utilityNav']") - @given('Open target main page') -def open_target_main(context): +def open_main(context): context.app.main_page.open_main_page() -Context.driver.wait.until( -EC.element_to.be_clickable(SEARCH_FIELD). -message= 'Search field not clickable' - @when('Click on cart icon') def click_cart(context): context.driver.find_element(By.CSS_SELECTOR, "[data-test='@web/CartLink']").click() -@when('Search for {search_word}') -def search_product(context, search_word): - context.app.header.search() +@when('Search for {item}') +def search_product(context, item): + context.app.header.search(item) + @then('Verify header has {expected_amount} links') def verify_header_links(context, expected_amount): @@ -117,12 +132,6 @@ def verify_header_links(context, expected_amount): links = context.driver.find_elements(By.CSS_SELECTOR, "[data-test*='@web/GlobalHeader/UtilityHeader/']") assert len(links) == expected_amount, f'Expected {expected_amount} links but got {len(links)}' - -@then('Verify header is shown') -def verify_header(context): - context.driver.find_element(By.CSS_SELECTOR, "[class*='styles_utilityHeaderContainer']") - - #under features, stpes and product_details_steps.py from selenium.webdriver.common.by import By @@ -180,6 +189,7 @@ def click_add_to_cart(context): message='Side navigation product name not visible' ) + @when('Store product name') def store_product_name(context): context.product_name = context.driver.find_element(*SIDE_NAV_PRODUCT_NAME).text @@ -324,39 +334,86 @@ def after_scenario(context, feature): context.driver.delete_all_cookies() context.driver.quit() +from selenium import webdriver +from selenium.webdriver.chrome.service import Service +from selenium.webdriver.chrome.options import Options + +def before_all(context): + options = Options() + options.add_argument("--start-maximized") + service = Service() + context.driver = webdriver.Chrome(service=service, options=options) + +def after_all(context): + context.driver.quit() + + #pages and base_page.py class Page: - def__init (self.driver): - self.driver = driver + def __init__(self, driver): + self.driver = driver + + def open_url(self, url): + self.driver.get(url) + + def find_element(self, *locater): + self.driver.find_element(*locater) + + def click(self, *locator): + self.driver.find_element(*locator).click() + + def input_text(self, text, *locator): + self.driver.find_element(*locator).send_keys(text) + +#home_page.py + +from selenium.webdriver.common.by import By + +class HomePage: + URL = "https://www.target.com" + CART_ICON = (By.ID, "utilityNav-cart") + + def __init__(self, driver): + self.driver = driver + + def load(self): + self.driver.get(self.URL) + + def click_cart(self): + self.driver.find_element(*self.CART_ICON).click() + +#cart_page.py + +from selenium.webdriver.common.by import By - def open.url(self,url) - self.driver.get(url) +class CartPage: + EMPTY_CART_MESSAGE = (By.XPATH, "//*[contains(text(),'Your cart is empty')]") - def find_element(self. *locater): - self.driver.find_element(*locater) + def __init__(self, driver): + self.driver = driver - def click(selfslef, *locator): - self.driver.find_element(*locator) click() + def is_cart_empty_message_displayed(self): + return self.driver.find_element(*self.EMPTY_CART_MESSAGE).is_displayed() - def input_text(self.text, *locator): - self.driver.find_element(*locator) send_keys(text #header.py -from slenium.webdriver.common.by import By -from pages.base.page import Page +from selenium.webdriver.common.by import By +from pages.base_page import Page +from time import sleep -class.Header(page): +class Header(Page): -SEARCH_FIELD = (By, ID 'search') -SEARCH_BTN = (By.XPATH, "//button[@data-test= @web/search/button") + SEARCH_FIELD = (By.ID, 'search') + SEARCH_BTN = (By.CSS_SELECTOR, '[data-test="@web/Search/SearchButton"]') - def search(self): - self.input.text(text 'tea, self.SEARCH_FIELD) + def search(self, text): + self.input_text(text, *self.SEARCH_FIELD) self.click(*self.SEARCH_BTN) + sleep(5) #main_page.py From 5d9557b8f3d720c3142f9ad2f232aae21dac479f Mon Sep 17 00:00:00 2001 From: "jay404077047@gmail.com" Date: Mon, 21 Apr 2025 20:16:15 -0500 Subject: [PATCH 17/17] lesson 6 --- app/__init__.py | 0 app/application.py | 15 +++++++ css.selectors.py | 0 features/environment.py | 18 +++++++- features/steps/cart_steps.py | 48 ++++++++++++++++++++ features/steps/main_page_steps.py | 24 ++++++++++ features/steps/product_details_steps.py | 35 +++++++++++++++ features/steps/search_results_steps.py | 54 +++++++++++++++++++++++ features/tests/cart.feature | 6 +++ features/tests/main_page_ui_tests.feature | 10 +++++ features/tests/product_details.feature | 5 +++ features/tests/product_search.feature | 11 ++--- features/tests/target_search.feature | 37 ++++++++++++++++ locators.py | 0 pages/__init__.py | 0 pages/base_page.py | 17 +++++++ pages/cart_page.py | 10 +++++ pages/header.py | 14 ++++++ pages/home_page.py | 14 ++++++ pages/main_page.py | 7 +++ pages/search_results_page.py | 11 +++++ playground.py | 0 target_search.py | 0 target_signin.py | 0 24 files changed, 328 insertions(+), 8 deletions(-) create mode 100644 app/__init__.py create mode 100644 app/application.py create mode 100644 css.selectors.py create mode 100644 features/steps/cart_steps.py create mode 100644 features/steps/main_page_steps.py create mode 100644 features/steps/product_details_steps.py create mode 100644 features/steps/search_results_steps.py create mode 100644 features/tests/cart.feature create mode 100644 features/tests/main_page_ui_tests.feature create mode 100644 features/tests/product_details.feature create mode 100644 features/tests/target_search.feature create mode 100644 locators.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/home_page.py create mode 100644 pages/main_page.py create mode 100644 pages/search_results_page.py create mode 100644 playground.py create mode 100644 target_search.py create mode 100644 target_signin.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..41ba574e9 --- /dev/null +++ b/app/application.py @@ -0,0 +1,15 @@ +from pages.base_page import Page +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.driver = driver + self.main_page = MainPage(driver) + self.header =Header(driver) + self.search_results_page = SearchResultsPage(driver) + + diff --git a/css.selectors.py b/css.selectors.py new file mode 100644 index 000000000..e69de29bb diff --git a/features/environment.py b/features/environment.py index 0930bc703..185d27e35 100755 --- a/features/environment.py +++ b/features/environment.py @@ -1,7 +1,8 @@ from selenium import webdriver from selenium.webdriver.chrome.service import Service +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,8 @@ def browser_init(context): context.driver.maximize_window() context.driver.implicitly_wait(4) + context.wait = WebDriverWait(context.driver, 10) + context.app = Application(context.driver) def before_scenario(context, scenario): @@ -34,3 +37,16 @@ def after_step(context, step): def after_scenario(context, feature): context.driver.delete_all_cookies() context.driver.quit() + +from selenium import webdriver +from selenium.webdriver.chrome.service import Service +from selenium.webdriver.chrome.options import Options + +def before_all(context): + options = Options() + options.add_argument("--start-maximized") + service = Service() + context.driver = webdriver.Chrome(service=service, options=options) + +def after_all(context): + context.driver.quit() diff --git a/features/steps/cart_steps.py b/features/steps/cart_steps.py new file mode 100644 index 000000000..67817ddd1 --- /dev/null +++ b/features/steps/cart_steps.py @@ -0,0 +1,48 @@ +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 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}" + +from behave import given, when, then +from pages.home_page import HomePage +from pages.cart_page import CartPage + +@given('Open Target main page') +def step_open_main_page(context): + context.home_page = HomePage(context.driver) + context.home_page.load() + +@when('Click on cart icon') +def step_click_cart(context): + context.home_page.click_cart() + context.cart_page = CartPage(context.driver) + +@then('Verify Cart Empty message shown') +def step_verify_cart_empty_message(context): + assert context.cart_page.is_cart_empty_message_displayed(), "Empty cart message not displayed" \ 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..c897cafc3 --- /dev/null +++ b/features/steps/main_page_steps.py @@ -0,0 +1,24 @@ +from selenium.webdriver.common.by import By +from behave import given, when, then + + +@given('Open target main page') +def open_main(context): + context.app.main_page.open_main_page() + + +@when('Click on cart icon') +def click_cart(context): + context.driver.find_element(By.CSS_SELECTOR, "[data-test='@web/CartLink']").click() + + +@when('Search for {item}') +def search_product(context, item): + context.app.header.search(item) + + +@then('Verify header has {expected_amount} links') +def verify_header_links(context, expected_amount): + expected_amount = int(expected_amount) # '6' => 6 + links = context.driver.find_elements(By.CSS_SELECTOR, "[data-test*='@web/GlobalHeader/UtilityHeader/']") + assert len(links) == expected_amount, f'Expected {expected_amount} links but got {len(links)}' \ No newline at end of file diff --git a/features/steps/product_details_steps.py b/features/steps/product_details_steps.py new file mode 100644 index 000000000..ea20e4b16 --- /dev/null +++ b/features/steps/product_details_steps.py @@ -0,0 +1,35 @@ +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(8) + + +@then('Verify user can click through colors') +def click_and_verify_colors(context): + expected_colors = ['Blue Tint', 'Denim Blue', 'Marine', 'Raven'] + actual_colors = [] + + colors = context.driver.find_elements(*COLOR_OPTIONS) # [webelement1, webelement2, webelement3] + print(colors) + + for color in colors: + print(color) + color.click() + + selected_color = context.driver.find_element(*SELECTED_COLOR).text # 'Color\nBlack' + print('Current color text', selected_color) + + selected_color = selected_color.split('\n')[1] # remove 'Color\n' part, keep Black' + 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_steps.py b/features/steps/search_results_steps.py new file mode 100644 index 000000000..3b60e85bc --- /dev/null +++ b/features/steps/search_results_steps.py @@ -0,0 +1,54 @@ +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 + + +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") +LISTINGS = (By.CSS_SELECTOR, "[data-test='@web/site-top-of-funnel/ProductCardWrapper']") +PRODUCT_TITLE = (By.CSS_SELECTOR, "[data-test='product-title']") +PRODUCT_IMG = (By.CSS_SELECTOR, 'img') + + +@when('Click on Add to Cart button') +def click_add_to_cart(context): + context.driver.find_element(*ADD_TO_CART_BTN).click() # always clicks on 1st Add to cart btn + context.driver.wait.until( + EC.visibility_of_element_located(SIDE_NAV_PRODUCT_NAME), + message='Side navigation product name not visible' + ) + + +@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) + + +@then('Verify that correct search results shown for {product}') +def verify_results(context, product): + context.app.search_results_page.verify_results(product) + + +@then('Verify that every product has a name and an image') +def verify_products_name_img(context): + # To see ALL listings (comment out if you only check top ones): + context.driver.execute_script("window.scrollBy(0,2000)", "") + sleep(4) + context.driver.execute_script("window.scrollBy(0,2000)", "") + + all_products = context.driver.find_elements(*LISTINGS) # [WebEl1, WebEl2, WebEl3, WebEl4] + + for product in all_products: + title = product.find_element(*PRODUCT_TITLE).text + assert title, 'Product title not shown' + print(title) + product.find_element(*PRODUCT_IMG) diff --git a/features/tests/cart.feature b/features/tests/cart.feature new file mode 100644 index 000000000..ebfe919da --- /dev/null +++ b/features/tests/cart.feature @@ -0,0 +1,6 @@ +Feature: Cart tests + + Scenario: User can see Cart Empty message + Given Open target main page + When Click on cart icon + Then Verify Cart Empty message shown \ No newline at end of file diff --git a/features/tests/main_page_ui_tests.feature b/features/tests/main_page_ui_tests.feature new file mode 100644 index 000000000..9458ce3a6 --- /dev/null +++ b/features/tests/main_page_ui_tests.feature @@ -0,0 +1,10 @@ +Feature: Tests for main page UI + + Scenario: Verify header in shown + Given Open Target main page + Then Verify header is shown + + Scenario: Verify header has correct amount links + Given Open Target main page + Then Verify header is shown + And Verify header has 6 links \ 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..708c7a0e4 --- /dev/null +++ b/features/tests/product_details.feature @@ -0,0 +1,5 @@ +Feature: Tests for product page + + Scenario: User can select colors + Given Open target product A-54551690 page + Then Verify user can click through colors \ No newline at end of file diff --git a/features/tests/product_search.feature b/features/tests/product_search.feature index f072ebd7d..8161b5911 100755 --- a/features/tests/product_search.feature +++ b/features/tests/product_search.feature @@ -1,9 +1,6 @@ Feature: Test Scenarios for Search functionality - - -kk Scenario: User can search for a product - Given Open Google page - When Input Car into search field - And Click on search icon - Then Product results for Car are shown \ No newline at end of file + Scenario: User can search for a tes on target + Given Open target main page + When search for tea + #Then Product results for tea \ 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..09f100d95 --- /dev/null +++ b/features/tests/target_search.feature @@ -0,0 +1,37 @@ +Feature: Tests for Target Search functionality + + Scenario: User can search for coffee + Given Open target main page + When Search for coffee + Then Verify that correct search results shown for coffee + + Scenario: User can search for tea + Given Open target main page + When Search for tea + Then Verify that correct search results shown for tea + + Scenario Outline: User can search for product + Given Open target main page + When Search for + Then Verify that correct search results shown for + Examples: + |search_word |search_result | + |coffee |coffee | + |tea |tea | + |mug |mug | + |sugar |sugar | + + Scenario: User can add a product to cart + Given Open target main page + When Search for mug + 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 + + Scenario: Verify that user can see product names and images + Given Open target main page + When Search for AirPods (3rd Generation) + Then Verify that every product has a name and an image \ No newline at end of file diff --git a/locators.py b/locators.py new file mode 100644 index 000000000..e69de29bb 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..c8e1befcd --- /dev/null +++ b/pages/base_page.py @@ -0,0 +1,17 @@ + +class Page: + + def __init__(self, driver): + self.driver = driver + + def open_url(self, url): + self.driver.get(url) + + def find_element(self, *locater): + self.driver.find_element(*locater) + + 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..eb29a61d7 --- /dev/null +++ b/pages/cart_page.py @@ -0,0 +1,10 @@ +from selenium.webdriver.common.by import By + +class CartPage: + EMPTY_CART_MESSAGE = (By.XPATH, "//*[contains(text(),'Your cart is empty')]") + + def __init__(self, driver): + self.driver = driver + + def is_cart_empty_message_displayed(self): + return self.driver.find_element(*self.EMPTY_CART_MESSAGE).is_displayed() diff --git a/pages/header.py b/pages/header.py new file mode 100644 index 000000000..24ee04daf --- /dev/null +++ b/pages/header.py @@ -0,0 +1,14 @@ +from selenium.webdriver.common.by import By +from pages.base_page import Page +from time import sleep + + +class Header(Page): + + SEARCH_FIELD = (By.ID, 'search') + SEARCH_BTN = (By.CSS_SELECTOR, '[data-test="@web/Search/SearchButton"]') + + def search(self, text): + self.input_text(text, *self.SEARCH_FIELD) + self.click(*self.SEARCH_BTN) + sleep(5) \ No newline at end of file diff --git a/pages/home_page.py b/pages/home_page.py new file mode 100644 index 000000000..e4fe419ae --- /dev/null +++ b/pages/home_page.py @@ -0,0 +1,14 @@ +from selenium.webdriver.common.by import By + +class HomePage: + URL = "https://www.target.com" + CART_ICON = (By.ID, "utilityNav-cart") + + def __init__(self, driver): + self.driver = driver + + def load(self): + self.driver.get(self.URL) + + def click_cart(self): + self.driver.find_element(*self.CART_ICON).click() diff --git a/pages/main_page.py b/pages/main_page.py new file mode 100644 index 000000000..8df1d6dbd --- /dev/null +++ b/pages/main_page.py @@ -0,0 +1,7 @@ +from pages.base_page import Page + +class MainPage(Page): + + def open_main_page(self): + self.open_url('https://www.target.com/') + diff --git a/pages/search_results_page.py b/pages/search_results_page.py new file mode 100644 index 000000000..fa5940762 --- /dev/null +++ b/pages/search_results_page.py @@ -0,0 +1,11 @@ +from selenium.webdriver.common.by import By +from pages.base_page import Page + + +class SearchResultsPage(Page): + SEARCH_RESULTS_TEXT = (By.XPATH, '//div(@data-test="p=resultsCount"}') + + def verify_search_results(self): + actual_text = self.find_element(*self.SEARCH_RESULTS_TEXT).text + assert "tea" in actual_text, f'Error. Text tea not in {actual_text}' + diff --git a/playground.py b/playground.py new file mode 100644 index 000000000..e69de29bb diff --git a/target_search.py b/target_search.py new file mode 100644 index 000000000..e69de29bb diff --git a/target_signin.py b/target_signin.py new file mode 100644 index 000000000..e69de29bb