Introduction
Test automation is a game-changer for software testing, allowing you to run repetitive tests quickly and efficiently. In this guide, we’ll walk you through the basics of test automation using two powerful tools: Selenium with Python and Playwright with TypeScript. By the end of this post, you'll have written your first automated test script for a real-life example—a login page.
We’ll cover:
- Setting up your environment.
- Writing your first test script.
- Running the test and interpreting the results.
Let’s get started!
Step 1: Setting Up Your Environment
For Selenium/Python:
- Install Python: Download Python from python.org and ensure it’s added to your PATH during installation.
- Install Selenium: Use pip to install Selenium:
pip install selenium
- Download WebDriver: Download the appropriate WebDriver for your browser (e.g., ChromeDriver for Chrome) and add it to your system PATH.
For Playwright/TypeScript:
- Install Node.js: Download Node.js from nodejs.org and install it.
- Install Playwright: Use npm to install Playwright:
npm init playwright@latest
This command will create a new Playwright project and install all necessary dependencies.
- Set Up TypeScript: If you’re new to TypeScript, don’t worry—Playwright handles most of the setup for you.
Step 2: Writing Your First Test Script
Example Scenario: Automating Login on a Sample Website
We’ll automate the login process for a sample website like https://the-internet.herokuapp.com/login. The goal is to:
- Navigate to the login page.
- Enter valid credentials.
- Submit the form.
- Verify successful login.
Selenium/Python Example
Here’s how to write the test script using Selenium and Python:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Step 1: Initialize WebDriver
driver = webdriver.Chrome()
try:
# Step 2: Navigate to the login page
driver.get("https://the-internet.herokuapp.com/login")
# Step 3: Locate username and password fields
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.ID, "password")
# Step 4: Enter credentials
username_field.send_keys("tomsmith")
password_field.send_keys("SuperSecretPassword!")
# Step 5: Click the login button
login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
login_button.click()
# Step 6: Verify successful login
success_message = driver.find_element(By.ID, "flash")
assert "You logged into a secure area!" in success_message.text
print("Login successful!")
finally:
# Step 7: Close the browser
time.sleep(3) # Wait to see the result
driver.quit()
Playwright/TypeScript Example
Here’s how to write the same test script using Playwright and TypeScript:
import { test, expect } from '@playwright/test';
test('Login Test', async ({ page }) => {
// Step 1: Navigate to the login page
await page.goto('https://the-internet.herokuapp.com/login');
// Step 2: Fill in the username and password fields
await page.fill('#username', 'tomsmith');
await page.fill('#password', 'SuperSecretPassword!');
// Step 3: Click the login button
await page.click('button[type="submit"]');
// Step 4: Verify successful login
const successMessage = await page.textContent('#flash');
expect(successMessage).toContain('You logged into a secure area!');
console.log('Login successful!');
});
Step 3: Running the Test
For Selenium/Python:
Run the Python script using the following command:
python your_script_name.py
If everything is set up correctly, the browser will open, perform the login steps, and close automatically.
For Playwright/TypeScript:
Run the Playwright test using the following command:
npx playwright test
Playwright will execute the test and generate a detailed report.
Step 4: Interpreting the Results
Both scripts will verify that the login was successful by checking for a specific success message. If the message is found, the test passes; otherwise, it fails.
-
Selenium Output: A message like
Login successful!
will be printed to the console. - Playwright Output: Playwright provides a detailed HTML report. You can view it by running:
npx playwright show-report
Real-Life Example: Why Automate Login Tests?
Imagine you’re working on an e-commerce platform where users log in frequently. Manually testing the login functionality after every code change is time-consuming and error-prone. Automating this process ensures that:
- The login feature works as expected.
- Any regression issues are caught early.
- You save time for more complex testing tasks.
Conclusion
Congratulations! You’ve just written your first automated test scripts using Selenium/Python and Playwright/TypeScript. These tools are powerful and versatile, making them ideal for automating web applications.
Next steps:
- Explore advanced features like handling dynamic elements, managing waits, and integrating tests into CI/CD pipelines.
- Experiment with other tools and frameworks to find what works best for your team.
Visit us at Testamplify | X | Instagram | LinkedIn
Top comments (0)