Selenium Testing: A Complete Guide
Learn Selenium testing with this detailed step-by-step tutorial. Understand Selenium's anatomy and how to write a test case.
Join the DZone community and get the full member experience.
Join For FreeSelenium is widely loved by web testers worldwide thanks to its versatility and simplicity. Testing with Selenium is relatively straightforward, which is why it is commonly used by developers looking to move from manual to automation testing. In this article, we’ll show you how to do Selenium testing in depth.
History of Selenium
Selenium began in 2004, when Jason Huggins, an engineer at ThoughtWorks, needed to frequently test a web application. 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 domains other than the one it was on.
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 by injecting JavaScript via a server. 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 more straightforward API, better browser support, and native WebDriver protocol, making web automation easier and more efficient.
Understanding Selenium
Selenium's open-source suite of tools is designed for automating web browsers. It provides a programmatic interface for interacting with web pages, enabling tasks such as form submission, navigation, and data extraction to be performed automatically.
You can write Selenium test scripts in Java, C#, Python, Ruby, JavaScript, and more. Let's look at Selenium's three core components.
Selenium WebDriver
This is the core of the Selenium suite. It provides a programming interface for creating and executing 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 5 steps in the process:
- A WebDriver object is created in the desired programming language. This object corresponds to a specific browser (e.g., ChromeDriver for Google Chrome).
- The WebDriver establishes a session with the browser, initiating a controlled instance of the browser.
- WebDriver sends commands to the browser (e.g., open a URL, click a button, enter text in a form field). These commands are executed in the browser just as if a human user were performing the actions.
- The browser sends responses back to the WebDriver. These responses contain information about the success or failure of the commands, as well as any data requested (e.g., the text of a web element, the status of a form submission).
- Once the test script has completed its tasks, the WebDriver terminates the session, closing the browser.
Each browser has a different driver:
ChromeDriverfor Google ChromeGeckoDriverfor Mozilla FirefoxEdgeDriverfor Microsoft EdgeSafariDriverfor Safari
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:
- By ID
- By Name
- By Class Name
- By Tag Name
- By CSS Selector
- By XPath
Selenium Grid
Selenium Grid facilitates 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:
- Hub: a server that receives test requests from clients (test scripts) and routes them to the appropriate nodes. It is responsible for managing the distribution of test requests to nodes based on the test requirements and available resources.
- Node: the machines that actually execute the test cases. Each node is connected to the hub and can run tests on a specific browser or operating system. Each node can be configured with various options like the type of browsers it supports, the version of the browser, and the operating system. Nodes can be dynamically added or removed from the grid based on the testing needs.
A typical test execution flow in Selenium happens in these steps:
- A test script is written in a supported programming language using Selenium WebDriver.
- The test script specifies details such as the browser type, version, and operating system, and then sends a test request to the Hub.
- The Hub receives the request and checks the specified details.
- The Hub looks through its list of available Nodes to find one that matches the request.
- The Hub picks a suitable Node and assigns the test to it.
- The Hub forwards the test request to the selected Node.
- The Node gets the test request and starts running the test.
- The Node opens the browser, performs the actions from the test script, and interacts with the web application.
- After the test finishes, the Node sends the results (pass/fail status, logs, screenshots) back to the Hub.
- The Hub combines the results from all Nodes and shares the final outcome with the test script or reporting tool.
Communication between the Hub and Nodes is usually in JSON format, with test requests, details, and results sent as JSON data.
Selenium IDE
Selenium IDE makes 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 test results, 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.
Steps To Start Selenium Testing
1. Prepare your skills
To effectively use Selenium, you should have a basic understanding of at least one programming language. Java, Python, C#, and Ruby are 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:
- HTML
- CSS
- JavaScript
- Web element mechanism (forms, buttons, links, etc.)
Get your IDE ready. Popular IDEs for Selenium include:
- Eclipse: An open-source IDE that supports Java and other programming languages. It offers powerful features such as code completion, debugging, and plugin support.
- IntelliJ IDEA: A versatile IDE with both community (free) and commercial editions. It is known for its advanced code analysis and user-friendly interface.
- VSCode: This lightweight, open-source code editor was developed by Microsoft to support numerous programming languages through extensions. VSCode offers features such as IntelliSense (smart code completion), debugging, an integrated terminal, and version control integration.
2. Installing Selenium
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
2.3. C#
Step 1: Install Visual Studio IDE
Step 2: Create a new project and add Selenium WebDriver using NuGet package
PowerShell
Install-Package Selenium.WebDriver
3. Setting Up WebDriver For Your Browsers
3.1. Google Chrome
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();
3.2. Mozilla Firefox
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();
3.3. Microsoft Edge
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();
3.4. Safari
Step 1: Enable Remote Automation
- Open Safari and go to Preferences.
- Navigate to the Advanced tab.
- Check the box next to "Show Develop menu in menu bar."
- In the Develop menu, select “Allow Remote Automation.”
Step 2: Install SafariDriver
- SafariDriver comes pre-installed with Safari on macOS. No additional download is required.
Step 3: Configure SafariDriver in Your Code
- You can directly use SafariDriver without setting any system property:
WebDriver driver = new SafariDriver();
Writing Your First Selenium Tests
A Selenium test is composed of several key components:
- WebDriver initialization: The first step is setting up the WebDriver instance. It is a bridge between your test script and the browser.
- Navigate to the web page: You can use methods get() or navigate().to() to open the URL of the specific web page you want to test on.
- Locate web elements: To identify and interact with on-web elements, use locators (e.g. ID, name, CSS selectors, XPath).
- Perform actions: These actions are performed using WebDriver's methods:
sendKeys(): Sends input to an input field.click(): Clicks on a web element.getText(): Retrieves the text of a web element.selectByVisibleText(): Selects an option from a dropdown by visible text.get(): Navigates to a specified URL.findElement(): Locates a single web element.findElements(): Locates multiple web elements.clear(): Clears the content of an input field.isDisplayed(): Checks if a web element is visible.isEnabled(): Checks if a web element is enabled.isSelected(): Checks if a web element (e.g., checkbox) is selected.submit(): Submits a form.getTitle(): Retrieves the title of the current page.getCurrentUrl(): Retrieves the URL of the current page.navigate().to(): Navigates to a specified URL using the browser's navigation.navigate().back(): Navigates back to the previous page.navigate().forward(): Navigates forward to the next page.navigate().refresh(): Refreshes the current page.switchTo().frame(): Switches to a specific iframe.switchTo().defaultContent(): Switches back to the main content from an iframe.switchTo().alert(): Switches to an alert box.accept(): Accepts an alert.dismiss(): Dismisses an alert.
- Assertions: Assertions verify that expected conditions or values match actual outcomes during test execution.
- Synchronization Handling: Not all web pages load their structure and content in the same way. There are many variables coming into play
implicitlyWait(): Sets a default wait time for all element searches.explicitlyWait (WebDriverWait): Waits for a specific condition to be true before proceeding.pageLoadTimeout(): Sets the maximum time to wait for a page to load.setScriptTimeout(): Sets the maximum time to wait for an asynchronous script to finish.
- Test Data Management: Test data is a big part of any test case, especially for data-driven testing. Test data can be hard-coded within the test script, read from external files (e.g., CSV, Excel), or generated dynamically.
- Reporting and Logging: logging test steps, actions, and results helps in troubleshooting and debugging. Frameworks or libraries like Log4j, SLF4J, or built-in logging capabilities of programming languages are valuable for this.
- Cleanup: After the individual test is completed, it is important to reset the environment to a clean slate to ensure that this test does not affect the results of other tests.
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: 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:
- Import libraries and define test case details (ID, description, login credentials)
- Setup WebDriver for Chrome and specify the path to the ChromeDriver executable
- Navigate to the Etsy login page
- Leverage WebDriverWait to wait until elements are visible and clickable.
- Enter the email and password using send_keys
- Check if user is redirected to the homepage
- Cleanup by quitting the driver
Pythonfrom 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 = "[email protected]" 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()
Published at DZone with permission of Oliver Howard. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments