Tue Jan 07 - Written by: Samridha
Test Automation
Learn about test automation frameworks, best practices for scripting, and modern tools like Selenium, Cypress, and Playwright to streamline your QA process.
Test Automation
Automation Frameworks
Selenium WebDriver
- Open-source browser automation tool.
- Simulates user actions like clicking, navigating, and entering text.
- Locators include:
ID
,name
,class_name
,xpath
,CSS_selector
.
Example Code (Python):
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# Step 1: Set up WebDriver
driver = webdriver.Chrome() # Replace 'Chrome' with the browser of your choice
driver.get("https://example.com/login") # Navigate to the login page
# Step 2: Locate elements and interact with them
try:
# Enter username
username_field = driver.find_element(By.ID, "username") # Replace with actual ID
username_field.send_keys("testuser")
# Enter password
password_field = driver.find_element(By.ID, "password") # Replace with actual ID
password_field.send_keys("password123")
# Click the login button
login_button = driver.find_element(By.ID, "loginButton") # Replace with actual ID
login_button.click()
# Step 3: Validate login success
time.sleep(3) # Wait for the page to load
assert "Dashboard" in driver.title, "Login failed: Dashboard title not found"
print("Test Passed: Login successful!")
except Exception as e:
print(f"Test Failed: {e}")
finally:
driver.quit() # Clean up
Cypress
- Modern testing framework for frontend and single-page web applications.
- Features:
- Runs directly in the browser.
- Automatic waiting.
- Built with Node.js.
Example Code (Javascrip):
describe('Login Test', () => {
it('should log in with valid credentials', () => {
// Step 1: Visit the login page
cy.visit('https://example.com/login'); // Replace with the actual login page URL
// Step 2: Interact with the login form
cy.get('#username') // Replace '#username' with the actual ID of the username field
.type('testuser'); // Enter the username
cy.get('#password') // Replace '#password' with the actual ID of the password field
.type('password123'); // Enter the password
cy.get('#loginButton') // Replace '#loginButton' with the actual ID of the login button
.click(); // Click the login button
// Step 3: Validate successful login
cy.url().should('include', '/dashboard'); // Verify the URL includes '/dashboard'
cy.contains('Welcome').should('be.visible'); // Verify the 'Welcome' message is displayed
});
});
Playwright
- Open-source framework for modern browser automation.
- Supports multiple languages and cross-browser testing.
- Features:
- Auto-waiting.
- Cross-browser compatibility.
- Node.js-based.
Example Code (Javascrip):
// Import Playwright Test
const { test, expect } = require('@playwright/test');
// Define the test
test.describe('Login Test', () => {
test('should log in with valid credentials', async ({ page }) => {
// Step 1: Navigate to the login page
await page.goto('https://example.com/login'); // Replace with the actual login page URL
// Step 2: Interact with the login form
await page.fill('#username', 'testuser'); // Replace '#username' with the actual ID of the username field
await page.fill('#password', 'password123'); // Replace '#password' with the actual ID of the password field
await page.click('#loginButton'); // Replace '#loginButton' with the actual ID of the login button
// Step 3: Validate successful login
await expect(page).toHaveURL(/dashboard/); // Verify the URL contains '/dashboard'
await expect(page.locator('text=Welcome')).toBeVisible(); // Verify the 'Welcome' message is displayed
});
});
Test Scripting
Writing and Maintaining Automation Scripts
- Writing Test Cases:
- Follow a structured approach: Setup → Test Steps → Assertion → Breakdown.
- Maintaining Test Cases:
- Modularize code into reusable functions and modules.
- Use the Page Object Model (POM) to separate object locators from testing logic.
- Employ dynamic waits.
- Use version control and regularly update scripts.
Data-Driven, Keyword-Driven, and Hybrid Frameworks
- Data-Driven Framework:
- Test data is stored externally (e.g., in Excel or JSON) and fetched by the script for execution.
- Keyword-Driven Framework:
- Allows engineers with limited coding knowledge to write test cases by creating reusable functions for actions.
- Hybrid Framework:
- Combines the benefits of data-driven and keyword-driven frameworks.
Page Object Model (POM) and TestNG/JUnit Structure
- Page Object Model (POM):
- Creates an abstraction layer between UI elements and test scripts.
- TestNG/JUnit:
- Frameworks used for organizing and executing test cases.