Selenium
- Pronunciation
- suh-LEE-nee-um
In short
Selenium is an open-source suite of tools for automating web browsers, used mainly for end-to-end testing, with its WebDriver API available in many languages.
What is Selenium?
Selenium began in 2004 at ThoughtWorks, where Jason Huggins wrote a tool to automate repetitive testing of a web app. Its core today is WebDriver, an API that controls real browsers, including Chrome, Firefox, Safari and Edge, through each browser's own driver. The WebDriver protocol became a W3C standard in 2018, so browser makers implement it directly.
Tests can be written in Java, Python, C#, JavaScript, Ruby and other languages, which makes Selenium popular in teams whose main language isn't JavaScript. A test opens a page, finds elements by ID, CSS selector or XPath, clicks, types and asserts what appears. Selenium Grid runs tests in parallel across many machines and browser versions, and Selenium IDE records and replays browser actions.
Its long history means huge numbers of existing test suites, integrations with every CI system and cloud testing services, and plenty of documentation. Many commercial testing platforms are built on Selenium and WebDriver underneath.
A common misconception is that Selenium tests are inherently flaky. Flakiness usually comes from tests that don't wait properly for the page, using fixed sleeps instead of explicit waits for elements to appear. Newer tools such as Playwright and Cypress wait automatically, which is a big reason many new projects choose them, but well-written Selenium suites can be just as reliable.
Key takeaways
- Selenium automates real web browsers, mainly for end-to-end tests.
- It started in 2004; WebDriver became a W3C standard in 2018.
- Tests can be written in Java, Python, C#, JavaScript, Ruby and more.
- Selenium Grid runs tests in parallel across browsers and machines.
- Explicit waits, not fixed sleeps, keep Selenium tests reliable.
Example
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
try:
driver.get("https://staging.example.com/login")
driver.find_element(By.ID, "email").send_keys("ada@example.com")
driver.find_element(By.ID, "password").send_keys("correct horse")
driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
# Explicit wait instead of time.sleep(): wait until the heading appears
heading = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.TAG_NAME, "h1")))
assert "Dashboard" in heading.text
finally:
driver.quit()Readers ask
What is the difference between Selenium and Playwright?
Selenium is older, supports many languages and uses the standard WebDriver protocol. Playwright is newer, controls browsers through their debugging protocols, waits automatically and includes tracing and parallel runs out of the box. Both test real browsers.
What is Selenium WebDriver?
The main part of Selenium: an API and protocol for controlling a browser programmatically, opening pages, finding elements and simulating user actions such as clicks and typing.
What is Selenium Grid?
A way to run Selenium tests on many machines and browsers in parallel, by sending commands from the tests to a hub that distributes them to browser nodes.
See also
- End-to-End TestTesting & Quality, p. 8An end-to-end test is an automated test that runs a complete user journey through the whole application, from the user interface to the database and back.
- PlaywrightTesting & Quality, p. 19Playwright is Microsoft's open-source framework for end-to-end testing and browser automation, driving Chromium, Firefox and WebKit through one API.
- CypressTesting & Quality, p. 7Cypress is a JavaScript framework that runs end-to-end and component tests for web apps inside the browser, with automatic waiting and an interactive runner.
- Test AutomationTesting & Quality, p. 28Test automation is using code to run tests and check their results, so the same checks repeat quickly and consistently on every change instead of by hand.
- Flaky TestTesting & Quality, p. 10A flaky test is an automated test that sometimes passes and sometimes fails without any change to the code, which makes its results hard to trust.
- Web BrowserWeb Development, p. 57A web browser is an application that fetches web pages from servers and turns their HTML, CSS, and JavaScript into the interactive pages you see on screen.
Spotted a mistake or something missing on this page?Suggest an edit