Selenium is widely loved by web testers around the world thanks to its versatility and simplicity. Testing with Selenium is quite straightforward, which is why it is commonly used for people who want to go from manual to automation testing.
In this article, we’ll show you how to do Selenium testing in depth.
Selenium started in 2004 when Jason Huggins, an engineer at ThoughtWorks, needed to test a web application frequently. To avoid the hassle of manual testing, he built a JavaScript tool called JavaScriptTestRunner to automate user actions like clicking and typing.
Later, this tool was renamed Selenium Core, and it became popular within ThoughtWorks. However, it had a major limitation: it couldn’t bypass a browser’s same-origin policy, which blocked interactions with different domains.
In 2005, Paul Hammant created Selenium Remote Control (RC) to solve this issue. It allowed tests to be written in various programming languages and run across different browsers using a server to inject JavaScript. This made Selenium more flexible and widely adopted.
In 2006, Simon Stewart from Google developed Selenium WebDriver, which directly controlled browsers using their native APIs, making automation faster and more reliable.
By 2024, Selenium 4 is the latest version. It offers a simpler API, better browser support, and native WebDriver protocol, making web automation easier and more efficient.
Selenium is an open-source suite of tools designed for automating web browsers. It provides a way to interact with web pages through a programmatic interface, making it possible to perform tasks such as form submission, navigation, and data extraction automatically.
You can write Selenium test scripts in Java, C#, Python, Ruby, JavaScript, and more.
Selenium has 3 core components:
This is the core of the Selenium suite. It provides a programming interface to create and execute browser-based test scripts.
Simply put, Selenium WebDriver allows you to communicate directly with the browser you want to test on using a dedicated driver for that browser. It controls the browser by sending it commands (like clicking a button or filling out a form) and receiving responses (like checking if a button is displayed or if a form was submitted successfully).
There are usually 4 steps in the process:
Each browser has a different driver, which is:
Selenium WebDriver offers you a rich set of commands to perform on-web interactions with its elements. You can locate those elements using several strategies:
Selenium Grid is a tool designed to facilitate the parallel execution of test cases across multiple machines and browsers simultaneously. Running tests in parallel significantly reduces the overall time required to complete a test suite. Instead of waiting for one test to finish before starting another, multiple tests run at the same time.
Selenium Grid has 2 core components:
Here’s how Selenium Grid works:
A typical test execution flow in Selenium happens in these steps:
Communication between the Hub and Nodes is usually done in JSON format, where test requests, details, and results are sent as JSON data.
The primary purpose of Selenium IDE is to make the process of creating and executing automated tests more accessible and less time-consuming, especially for users who may not have extensive programming experience.
Selenium IDE allows users to record their interactions with a web application, such as clicking buttons, entering text, and navigating through pages. During the recording session, every action is captured and converted into a test script.
How It Works: Users start the recording session by clicking a button in the IDE, then perform actions in the web application. Selenium IDE automatically generates a sequence of commands based on these actions. This eliminates the need for manual coding, making Selenium more accessible to non-programmers and speeding up test case creation.
After recording, users can run the test script, and Selenium IDE will replay the recorded actions in the browser. The IDE displays the results of the test, including any errors or issues encountered.
Selenium IDE allows users to set breakpoints in their test scripts. When a breakpoint is reached during execution, the IDE pauses the test, enabling users to inspect the state of the application and script.
To effectively use Selenium, you should have a basic understanding of at least one programming language. Java, Python, C#, and Ruby is the most popular choices. Knowledge of programming concepts such as variables, loops, conditionals, and functions is also essential.
After that, make sure you understand the basics of web technologies:
Get your IDE ready. Popular IDEs for Selenium are:
2.1. Java
Step 1: Download Selenium Java bindings from the Selenium website.
Step 2: Add the downloaded JAR files to your project’s build path in your IDE.
Step 3: Set up a Maven or Gradle project to manage dependencies (if preferred).
Step 4: Add Selenium dependencies in pom.xml (for Maven) or build.gradle (for Gradle).
2.2. Python
Step 1: Install Python from the official website.
Step 2: Use pip (Python package manager) to install Selenium
pip install selenium
2.3. C#
Step 1: Install Visual Studio IDE
Step 2: Create a new project and add Selenium WebDriver using NuGet package
Install-Package Selenium.WebDriver
Step 1: Visit the ChromeDriver download page and download the version of ChromeDriver that matches your installed version of Google Chrome.
Step 2: Extract the downloaded ZIP file to a directory of your choice. Note the path to the chromedriver executable.
Step 3: Add the ChromeDriver executable to your system PATH, or specify the path in your code:
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
Step 1: Visit the GeckoDriver releases page and download the appropriate version for your operating system.
Step 2: Extract the downloaded ZIP file to a directory of your choice and note the path to the geckodriver executable.
Step 3: Add the GeckoDriver executable to your system PATH, or specify the path in your code:
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
WebDriver driver = new FirefoxDriver();
Step 1: Visit the Microsoft Edge WebDriver download page and download the version of EdgeDriver that matches your installed version of Microsoft Edge.
Step 2: Extract the downloaded ZIP file to a directory of your choice and note the path to the msedgedriver executable.
Step 3: Configure EdgeDriver in Your Code
Add the EdgeDriver executable to your system PATH, or specify the path in your code:
System.setProperty("webdriver.edge.driver", "path/to/msedgedriver");
WebDriver driver = new EdgeDriver();
Step 1: Enable Remote Automation
Step 2: Install SafariDriver
Step 3: Configure SafariDriver in Your Code
WebDriver driver = new SafariDriver();
A Selenium test is composed of several key components:
Read More: What is a Test Report? How To Build Good Test Reports?
Now let’s write a Selenium script for this test case:
Component |
Details |
Test Case ID |
TC001 |
Description |
Verify Login with Valid Credentials on Etsy |
Preconditions |
User is on the Etsy login popup |
Test Steps |
1. Enter a valid email address. 2. Enter the corresponding valid password. 3. Click the "Sign In" button. |
Test Data |
Email: validuser@example.com Password: validpassword123 |
Expected Result |
Users should be successfully logged in and redirected to the homepage or the previously intended page. |
Actual Result |
(To be filled in after execution) |
Postconditions |
User is logged in and the session is active |
Pass/Fail Criteria |
Pass: Test passes if the user is logged in and redirected correctly. Fail: Test fails if an error message is displayed or the user is not logged in. |
Comments |
Ensure the test environment has network access and the server is operational. |
Let’s write a Selenium script in Python for those steps:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Test Case Details
test_case_id = "TC001"
description = "Verify Login with Valid Credentials on Etsy"
email = "validuser@example.com"
password = “validpassword123”
# Set up WebDriver
chrome_options = Options()
chrome_options.add_argument("--headless") # Run in headless mode (optional)
chrome_service = Service("path/to/chromedriver") # Replace with the path to your chromedriver
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
try:
# Navigate to Etsy login page
driver.get("https://www.etsy.com/signin")
# Wait until the login fields are available
wait = WebDriverWait(driver, 10)
email_field = wait.until(EC.visibility_of_element_located((By.ID, "join_neu_email_field")))
password_field = wait.until(EC.visibility_of_element_located((By.ID, "join_neu_password_field")))
sign_in_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-id='signin-btn']")))
# Enter email and password
email_field.send_keys(email)
password_field.send_keys(password)
# Click the Sign In button
sign_in_button.click()
# Wait for the page to load and verify successful login
WebDriverWait(driver, 10).until(EC.url_changes("https://www.etsy.com/signin"))
# Check if redirected to the homepage
if "etsy.com" in driver.current_url:
print(f"Test Case {test_case_id}: Pass - User is successfully logged in.")
else:
print(f"Test Case {test_case_id}: Fail - User is not redirected to the homepage.")
except Exception as e:
print(f"Test Case {test_case_id}: Fail - {str(e)}")
finally:
# Cleanup
driver.quit()