9. Real-World Examples and Demo Scripts in Selenium Automation
9.1. Launch Selenium Practice Form Script
from selenium import webdriver
browser = webdriver.Chrome(“chromedriver.exe”)
browser.get(‘https://www.techlistic.com/p/selenium-practice-form.html’)
9.2. Search the keyword “Selenium” in the pypi.org search box
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Launch Chrome browser
browser = webdriver.Chrome(“chromedriver.exe”)
# Open pypi.org
browser.get(‘https://pypi.org/project/selenium/’)
# Maximize Browser Window
browser.maximize_window()
# Set wait time of 30 secs
browser.implicitly_wait(30)
# Type “Selenium” in search bar
browser.find_element(By.ID, ‘search’).send_keys(“Selenium”)
# Click Enter
browser.find_element(By.ID, ‘search’).send_keys(Keys.RETURN)
9.3. Automate Techlistic Demo Sign-up form
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
# Variables
driver_path=‘chromedriver.exe’
navigate_url = “https://www.techlistic.com/p/selenium-practice-form.html”
class DemoqaTesting(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(driver_path)
self.driver.maximize_window()
self.driver.get(navigate_url)
self.driver.implicitly_wait(30)
def tearDown(self):
self.driver.quit()
def test_automate_form(self):
# Enter First Name
first_name = self.driver.find_element(By.NAME,‘firstname’)
first_name.send_keys(‘Jassi’)
# Enter Last Name
last_name = self.driver.find_element(By.NAME,‘lastname’)
last_name.send_keys(‘Maurya’)
# Click on Gender Radio button
gender = self.driver.find_element(By.ID, ‘sex-1’)
print(gender.is_selected())
gender.click()
# Enter DOB
dob = self.driver.find_element(By.ID,‘datepicker’)
dob.clear()
dob.send_keys(’19 Mar 1990′)
# Select Profession checkbox
profession = self.driver.find_element(By.ID,‘profession-1’)
profession.click()
# Select Automation tool checkbox
automation_tool = self.driver.find_element(By.ID,‘tool-2’)
automation_tool.click()
# Click Submit button
submit_button = self.driver.find_element(By.ID,‘submit’)
submit_button.click()
time.sleep(5)
if __name__==‘__main__’:
unittest.main()
9.4. Automate Example Scenarios for Dummy e-Comm Website Functionalities:
To illustrate the automation of common web application functionalities, let’s consider the following scenarios:
Scenario 1: Login
Navigate to the login pageFill in the required fields with valid dataSubmit the login formVerify the successful login message or the user’s profile page
Scenario 2: Search Functionality
Open the home pageEnter a search query in the search fieldClick on the search buttonVerify that the search results are displayed correctly
Scenario 3: Adding Items to Cart
Browse to a product category pageSelect a specific productAdd the product to the cartVerify that the item appears in the cart
Here’s the code that includes the checkout process after adding an item to the cart:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC # Set up the driver
driver = webdriver.Chrome() # Change this to the appropriate web driver if needed
driver.maximize_window() # Navigate to the login page
driver.get(“https://www.saucedemo.com/”) # Perform login
username = “standard_user”
password = “secret_sauce”
username_field = WebDriverWait(driver,
10).until(EC.visibility_of_element_located((By.ID, “user-name”))
)
username_field.send_keys(username)
password_field = driver.find_element(By.ID,
“password”)password_field.send_keys(password)
login_button = driver.find_element(By.ID,
“login-button”)login_button.click() # Verify successful login
inventory_page_title = WebDriverWait(driver, 10).until(
EC.title_contains(“Swag Labs”)
) if “Swag Labs” in driver.title:
print(“Login successful!”)
else:
print(“Login failed.”) # Perform search functionality
search_field = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, “search-box”))
)
search_field.send_keys(“backpack”)
search_button = driver.find_element(By.ID,
“search-button”)search_button.click() # Verify search results
search_results = driver.find_elements(By.XPATH, “//div[@class=’inventory_item_name’]”)
if len(search_results) > 0:
print(“Search results found.”)
else:
print(“No search results found.”) # Add item to cart
add_to_cart_button = driver.find_element(By.XPATH, “//button[text()=’Add to cart’]”)
add_to_cart_button.click() # Verify item added to cart
cart_badge = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, “//span[@class=’shopping_cart_badge’]”))
)
cart_items_count = int(cart_badge.text) if cart_items_count > 0:
print(f”Item added to cart. Cart count: {cart_items_count}“)
else:
print(“Item not added to cart.”) # Close the browser
driver.quit()
1. Importing the necessary modules:
from selenium import webdriver: Importing the Selenium WebDriver module, which provides the necessary tools for browser automation.
driver = webdriver.Chrome(): Creating an instance of the Chrome WebDriver. This sets up the Chrome browser for automation. You can change the web driver to the appropriate one based on your browser choice.
driver.maximize_window(): Maximizing the browser window to ensure the content is fully visible.
driver.get(“https://www.saucedemo.com/”): Opening the specified URL in the browser.
Providing the login credentials by finding the username and password fields using WebDriverWait and locating the elements by their IDs.username_field.send_keys(username): Entering the value of the username variable into the username field.password_field.send_keys(password): Entering the value of the password variable into the password field.login_button.click(): Click the login button to submit the form.
Using WebDriverWait to wait for the title of the page to contain “Swag Labs” (the expected title after successful login).Checking if the page title contains “Swag Labs” to determine if the login was successful.
Finding the search field using WebDriverWait and locating the element by its ID.
Entering the search keyword “backpack” into the search field.Finding the search button and clicking it to initiate the search.
Using WebDriverWait to wait for the presence of search results.Checking if the search results are found by verifying if the length of the search results is greater than zero.
8. Adding an item to the cart:
Finding the “Add to cart” button using an XPath expression and clicking it.
Using WebDriverWait to wait for the visibility of the cart badge (indicating the number of items in the cart).Retrieving the text from the cart badge and converting it to an integer.Checking if the cart items count is greater than zero to confirm that the item was added to the cart.
driver.quit(): Quitting the browser and ending the WebDriver session.This code demonstrates a typical Selenium automation workflow for logging in, performing a search, adding an item to the cart, and verifying the results. You can modify and expand upon this code to suit your specific needs.
Source: Read More