0% found this document useful (0 votes)
3 views

SauceDemo Script Python

The document provides a series of Selenium WebDriver scripts to automate testing on the Sauce Demo website. It includes steps for logging in with both locked-out and valid users, validating error messages, asserting product presence, and adding products to the cart. The scripts demonstrate how to interact with web elements and perform assertions to ensure the correct functionality of the application.

Uploaded by

waseemimran401
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SauceDemo Script Python

The document provides a series of Selenium WebDriver scripts to automate testing on the Sauce Demo website. It includes steps for logging in with both locked-out and valid users, validating error messages, asserting product presence, and adding products to the cart. The scripts demonstrate how to interact with web elements and perform assertions to ensure the correct functionality of the application.

Uploaded by

waseemimran401
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. Navigate to https://www.saucedemo.

com/

import time

from selenium import webdriver

driver= webdriver.Chrome()
driver.maximize_window()

driver.get("https://www.saucedemo.com/")
time.sleep(5000)

driver.quit()

2. a. Try to login with a locked-out user [locked_out_user/


secret_sauce].
import time

from selenium import webdriver


from selenium.webdriver.common.by import By

driver= webdriver.Chrome()
driver.maximize_window()

driver.get("https://www.saucedemo.com/")

driver.find_element(By.ID, 'user-name').send_keys('locked_out_user')
driver.find_element(By.ID, 'password').send_keys('secret_sauce')

time.sleep(5000)

b. Validate the error message on the screen.


import time

from selenium import webdriver


from selenium.webdriver.common.by import By

driver= webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.saucedemo.com/")

driver.find_element(By.ID, 'user-name').send_keys('locked_out_user')
driver.find_element(By.ID, 'password').send_keys('secret_sauce')

driver.find_element(By.ID, 'login-button').click()

time.sleep(5000)

3. a.Login with a valid username and password [standard_user/


secret_sauce].

import time

from selenium import webdriver


from selenium.webdriver.common.by import By

driver= webdriver.Chrome()
driver.maximize_window()

driver.get("https://www.saucedemo.com/")
driver.find_element(By.ID, 'user-name').send_keys('standard_user')
driver.find_element(By.ID, 'password').send_keys('secret_sauce')
driver.find_element(By.ID, 'login-button').click()

time.sleep(500)
driver.quit()

b.Validate login is successful.


import time

from selenium import webdriver


from selenium.webdriver.common.by import By

driver= webdriver.Chrome()
driver.maximize_window()

driver.get("https://www.saucedemo.com/")

driver.find_element(By.ID, 'user-name').send_keys('standard_user')
driver.find_element(By.ID, 'password').send_keys('secret_sauce')
driver.find_element(By.ID, 'login-button').click()

driver.find_element(By.ID, 'inventory_container')
print("login successful")

time.sleep(500)
driver.quit()

1. Assert that the product- “Dummy Product” is not present in


the product list.
import time

from selenium import webdriver


from selenium.webdriver.common.by import By

driver= webdriver.Chrome()
driver.maximize_window()

driver.get("https://www.saucedemo.com/")
driver.find_element(By.ID, 'user-name').send_keys('standard_user')
driver.find_element(By.ID, 'password').send_keys('secret_sauce')
driver.find_element(By.ID, 'login-button').click()

driver.find_element(By.ID, 'inventory_container')
print("login successful")
products = driver.find_elements(By.CLASS_NAME, 'inventory_item_name')
product_names = [product.text for product in products]

assert "Dummy Product" not in product_names, "Dummy Product should not be in the product
list."
print("Assertion passed: 'Dummy Product' is not present in the product list.")

time.sleep(5000)

driver.quit()

5. a. Assert that the product “Sauce Labs Bolt T-Shirt" is present


in the product list.

import time

from selenium import webdriver


from selenium.webdriver.common.by import By

driver= webdriver.Chrome()
driver.maximize_window()

driver.get("https://www.saucedemo.com/")

driver.find_element(By.ID, 'user-name').send_keys('standard_user')
driver.find_element(By.ID, 'password').send_keys('secret_sauce')
driver.find_element(By.ID, 'login-button').click()

driver.find_element(By.ID, 'inventory_container')
print("login successful")
products = driver.find_elements(By.CLASS_NAME, 'inventory_item_name')
product_names = [product.text for product in products]

assert "Sauce Labs Bolt T-Shirt" in product_names, "Sauce Labs Bolt T-Shirt is not present in the
product list."
print("Assertion passed: 'Sauce Labs Bolt T-Shirt' is present in the product list.")

time.sleep(5000)

driver.quit()

5. b. If the product is found, grab the price of the product and print
it in the console.
import time

from selenium import webdriver

from selenium.webdriver.common.by import By

driver= webdriver.Chrome()

driver.maximize_window()

driver.get("https://www.saucedemo.com/")

driver.find_element(By.ID, 'user-name').send_keys('standard_user')

driver.find_element(By.ID, 'password').send_keys('secret_sauce')

driver.find_element(By.ID, 'login-button').click()

driver.find_element(By.ID, 'inventory_container')

print("login successful")

products = driver.find_elements(By.CLASS_NAME, 'inventory_item')

for product in products:

product_name = product.find_element(By.CLASS_NAME, 'inventory_item_name').text

if product_name == "Sauce Labs Bolt T-Shirt":


price = product.find_element(By.CLASS_NAME, 'inventory_item_price').text

print(f"Price of '{product_name}': {price}")

time.sleep(5000)

driver.quit()

6. a. Add this product and any other product to the cart and
navigate to the cart.
import time

from selenium import webdriver

from selenium.webdriver.common.by import By

driver= webdriver.Chrome()

driver.maximize_window()

driver.get("https://www.saucedemo.com/")

driver.find_element(By.ID, 'user-name').send_keys('standard_user')

driver.find_element(By.ID, 'password').send_keys('secret_sauce')

driver.find_element(By.ID, 'login-button').click()

driver.find_element(By.ID, 'inventory_container')

print("login successful")

products = driver.find_elements(By.CLASS_NAME, 'inventory_item')


sauce_labs_bolt_tshirt = None

other_product = None

for product in products:

product_name = product.find_element(By.CLASS_NAME, 'inventory_item_name').text

if product_name == "Sauce Labs Bolt T-Shirt":

sauce_labs_bolt_tshirt = product

elif not other_product:

other_product = product

if sauce_labs_bolt_tshirt:

sauce_labs_bolt_tshirt.find_element(By.CLASS_NAME, 'btn_inventory').click()

print("Added 'Sauce Labs Bolt T-Shirt' to the cart.")

else:

print("'Sauce Labs Bolt T-Shirt' not found.")

if other_product:

other_product.find_element(By.CLASS_NAME, 'btn_inventory').click()

print("Added another product to the cart.")

else:

print("No other products found to add to the cart.")

# Navigate to the cart

driver.find_element(By.ID, "add-to-cart-test.allthethings()-t-shirt-(red)").click()

print("Navigated to the cart page.")

time.sleep(5000)

driver.quit()

You might also like