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 36d6913cf..8161b5911 100755 --- a/features/tests/product_search.feature +++ b/features/tests/product_search.feature @@ -1,7 +1,6 @@ Feature: Test Scenarios for Search functionality - 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/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() + diff --git a/homework six again b/homework six again new file mode 100644 index 000000000..fb759ed35 --- /dev/null +++ b/homework six again @@ -0,0 +1,812 @@ + +##outline +#ap +_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 +product_details.feature +product_search.feature +target_search.feature +-init_.py +-enviroment.py +#pages +_init_.py +base_page.py +header.py +main_page.py +home_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 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) + + +#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') + + +@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 + +@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)}' + +#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() + +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 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 + +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() + + + +#header.py + +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) + + +#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') + + 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() + 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() diff --git a/hw3 b/hw3 new file mode 100644 index 000000000..9f80c0996 --- /dev/null +++ b/hw3 @@ -0,0 +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 +from selenium.webdriver.chrome.options import Options + +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() + +@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('the user clicks on the Sign In menu') +def step_click_signin_menu(context): + sign_in_menu = WebDriverWait(context.driver, 10).until( + EC.element_to_be_clickable((By.XPATH, "//span[text()='Sign in']")) + ) + sign_in_menu.click() + +@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): + 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() + 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() 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 + 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() 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() 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 + 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() 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/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" 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) 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