Skip to content

L6 #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

L6 #51

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Target_signin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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()

# Verification
expected = 'Sign into your Target account'
actual = driver.find_element(By.XPATH, "//h1[contains(@class, 'styles_ndsHeading')]").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')
65 changes: 65 additions & 0 deletions Week 2 HW.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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()
driver.maximize_window()

# Open Amazon Sign-In page
driver.get("https://www.amazon.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_ya_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&")


#Amazon logo
driver.find_element(By.XPATH, "//a[@class='a-logo']")

#Email field
driver.find_element(By.XPATH, "//input[@type='email' or @name='email']")

#Continue button
driver.find_element(By.XPATH, "//input[@id='continue']")

#Conditions of use link
driver.find_element(By.XPATH, "//a[contains(@href, 'condition_of_use')]")

#Privacy Notice link
driver.find_element(By.XPATH, "//a[contains(@href, 'privacy_notice')]")

#Need help link
driver.find_element(By.XPATH, "//span[@class='a-expander-prompt']")

#Forgot your password link
driver.find_element(By.XPATH, "//a[contains(@href, 'forgotpassword')]")

#Other issues with Sign-In link
driver.find_element(By.XPATH, "//a[contains(@href, 'ap_signin_notification_popup')]")

#Create your Amazon account button
driver.find_element(By.XPATH, "//a[@id='createAccountSubmit']")

#Create a test case for the SignIn page using python & selenium script.
# Open Target
driver.get("https://www.target.com/")

#Click SignIn button
driver.find_element(By.XPATH, "//a[@id='account']")

#Click SignIn from side navigation
driver.find_element(By.XPATH, "//a[contains(text(),'Sign in')]")

#Verify SignIn page opened:
driver.find_element(By.XPATH, "//h1[text()='Sign into your Target account']")

#SignIn button is shown (you can just use driver.find_element() to check for element’s presence, no need to assert here)
driver.find_element(By.XPATH,"//button[contains(@type,'submit') and contains(text(),'Sign in')]")





98 changes: 98 additions & 0 deletions Week 3 HW.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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)

# 1. Find the most optimal locators for Create Account on amazon.com (Registration) page elements:
#Amazon log
$$("#a-page [href*='frn_logo']")
#Create account
$$("#ap_register_form [href*='ap_register_form']")
#Your name
$$("#ap_customer_name_context_message_section")
#Email
$$("#ap_email")
#Password
$$("#ap_password")
#Passwords must be at least 6 characters
$$("#auth-password-requirement-info")
#Re-enter password
$$("#ap_password_check")

#Condition of use
$$("#legalTextRow [href*='condition_of_use']")
#Privacy notice
$$("#legalTextRow [href*='<a privacy_notice']")
#Sing in
$$("#a-row [href*='Sing in']")

#2.Create a test case using BDD that opens target.com, clicks on the cart icon and verifies that “Your cart is empty” message is shown:
#Target_search_featuer
Scenario: 'Your cart is empty' search is shown for empty cart
Given Open target main page
When Click on Cart icon
Then Verify 'Your cart is empty' message is shown

from selenium.webdriver.common.by import By
from behave import given, when, then
from time import sleep


@given('Open target main page')
def open_target_main(context):
context.driver.get('https://www.target.com/')

@when('Click on Cart icon')
def click_cart(context):
context.driver.find_element(By.CSS_SELECTOR, "[data-test='@web/CartLink']").click()

@then("Verify 'Your cart is empty' message is shown")
def virify_cart_is_empty(context):
expected_text = 'Your cart is empty'
actual_results = context.driver.find_element(By.CSS_SELECTOR, "[data-test='boxEmptyMsg']").text
assert expected_text == actual_results, f'Expected {expected_text} did not match actual {actual_results}'
print("Test passed")


#3. Create a test case using BDD to verify that a logged out user can navigate to Sign In:
Scenario: 'Sign into your Target account' search is shown for sing in
Given Open target main page
When Click on sing in icon
Then Verify 'Sign into your Target account'


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

# Verification
expected = 'Sign into your Target account'
actual = driver.find_element(By.XPATH, "//h1[contains(@class, 'styles_ndsHeading')]").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')
Empty file added app/__init__.py
Empty file.
Empty file added app/application.py
Empty file.
23 changes: 23 additions & 0 deletions cart.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from selenium.webdriver.common.by import By
from behave import given, when, then
from time import sleep


@given('Open target main page')
def open_target_main(context):
context.driver.get('https://www.target.com/')


@when('Search for tea')
def search_product(context):
context.driver.find_element(By.ID, 'search').send_keys('tea')
context.driver.find_element(By.XPATH, "//button[@data-test='@web/Search/SearchButton']").click()
sleep(6)


@then('Verify correct search results show')
def verify_search_results(context):
actual_text = context.driver.find_element(By.XPATH, "//div[@data-test='lp-resultsCount']").text
expected_text = 'tea'
assert expected_text in actual_text, f'Error. Text {expected_text} not in {actual_text}'

8 changes: 3 additions & 5 deletions features/environment.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager


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.implicitly_wait(4)

context.driver.wait = WebDriverWait(context.driver, timeout=10)

def before_scenario(context, scenario):
print('\nStarted scenario: ', scenario.name)
Expand Down
31 changes: 31 additions & 0 deletions features/steps/cart_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from selenium.webdriver.common.by import By
from behave import given, when, then


CART_SUMMARY = (By.XPATH, "//div[./span[contains(text(), 'subtotal')]]")
CART_ITEM_TITLE = (By.CSS_SELECTOR, "[data-test='cartItem-title']")


@when('Open cart page')
def open_cart(context):
context.driver.get('https://www.target.com/cart')

@then('Verify cart has correct product')
def verify_product_name(context):
# context.product_name => stored before
product_name_in_cart = context.driver.find_element(*CART_ITEM_TITLE).text
print('Name in cart: ', product_name_in_cart)
assert context.product_name[:20] == product_name_in_cart[:20], \
f'Expected {context.product_name[:20]} did not match {product_name_in_cart[:20]}'

@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}"


@then("Verify 'Your cart is empty' message is shown")
def verify_cart_empty(context):
expected_result = 'Your cart is empty'
actual_result = context.driver.find_element(By.CSS_SELECTOR, "[data-test='boxEmptyMsg']").text
assert expected_result == actual_result, f'Expected {expected_result} did not match actual {actual_result}'
70 changes: 70 additions & 0 deletions features/steps/circle_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from behave import given, when, then
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


@given('Open the Target Circle page')
def open_target_circle_page(context):
context.driver.get("https://www.target.com/circle")


@then('Verify there are at least 10 benefit cells')
def verify_benefit_cells(context):
benefits = WebDriverWait(context.driver, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".cell-item-content"))
)
assert len(benefits) >= 10, f"Expected at least 10 benefit cells, got {len(benefits)}"


@given('Open the Target homepage')
def open_target_homepage(context):
# Replace with the actual URL of Target's homepage
context.driver.get("https://www.target.com")
# Verify homepage has loaded by checking the title
assert "Target" in context.driver.title, "Target homepage did not load properly."


@when('Search for a product "{product_name}"')
def search_for_product(context, product_name):
# Find the search input field
search_box = WebDriverWait(context.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "input#search"))
)

# Enter the product name and press ENTER
search_box.send_keys(product_name + Keys.RETURN)


@when('Add the first product to the cart')
def add_first_product_to_cart(context):
# Wait for the search results to load and select the first product
first_product = WebDriverWait(context.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div[data-test='product-card']"))
)
first_product.click()

# Wait for the "Add to Cart" button to be clickable
add_to_cart_button = WebDriverWait(context.driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-test='addToCartButton']"))
)
add_to_cart_button.click()


@then('Verify the product is in the cart')
def verify_product_in_cart(context):
# Open the cart page
cart_icon = WebDriverWait(context.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "a[data-test='cart']"))
)
cart_icon.click()

# Check if the cart contains at least one cart item
cart_items = WebDriverWait(context.driver, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div[data-test='cart-item']"))
)

# Assert there is at least one item in the cart
assert len(cart_items) > 0, "No items were found in the cart!"
print(f"Cart contains {len(cart_items)} item(s).")
46 changes: 46 additions & 0 deletions features/steps/main_page_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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


SEARCH_FIELD = (By.ID, 'search')
SEARCH_BTN = (By.XPATH, "//button[@data-test='@web/Search/SearchButton']")
CART_ICON = (By.CSS_SELECTOR, "[data-test='@web/CartLink']")
HEADER_LINKS = (By.CSS_SELECTOR, "[id*='utilityNav']")


@given('Open target main page')
def open_target_main(context):
context.driver.get('https://www.target.com/')

context.driver.wait.until(
EC.element_to_be_clickable(SEARCH_FIELD),
message='Search field not clickable'
)


@when('Search for {search_word}')
def search_product(context, search_word):
context.driver.find_element(*SEARCH_FIELD).send_keys(search_word)
context.driver.find_element(*SEARCH_BTN).click()
sleep(7)


@when('Click on Cart icon')
def click_cart(context):
context.driver.find_element(*CART_ICON).click()


@then('Verify at least 1 link shown')
def verify_1_header_link_shown(context):
link = context.driver.find_element(*HEADER_LINKS)
print(link)


@then('Verify {link_amount} links shown')
def verify_all_header_links_shown(context, link_amount):
link_amount = int(link_amount) # "6" => int 6
links = context.driver.find_elements(*HEADER_LINKS)
print(links)
assert len(links) == link_amount, f'Expected {link_amount} links, but got {len(links)}'
Loading