Selenium test coverage
Selenium test coverage: see the backend code every test really runs
Selenium does not measure code coverage; it drives a browser. To see what a Selenium suite covers, measure the application while the suite runs. CoverOps does it with an agent inside each backend service and a covctx cookie that names the running test, so you get the lines, methods and services every test reached.
By the CoverOps team · Last updated
Why teams choose CoverOps for Selenium test coverage
Every Selenium test, mapped to the code it runs
Line-level coverage of the running services, per test and across every service a test reaches, so your slow end-to-end suite finally counts toward coverage, together with your API and manual tests.
Skip tests safely, with a reason for every skip
A test is skipped only when its recorded footprint misses every changed line, and the report says why. Observation mode shows what would have been skipped before anything is, and when the change cannot be read in full, everything runs.
A merge gate on the code that changed
Block or flag a pull request when its changed lines were not executed by the tests of its build, with the verdict on the commit status and, on GitHub, a comment listing the lines no test ran.
Flaky tests caught, not hidden by retries
Passes on retry, different results on one commit and flips on a branch are all counted, flips per branch, and a confirmed flake can be quarantined so it stops blocking builds.
Cloud, self-hosted or air-gapped
Use the hosted service in AWS Frankfurt, or run CoverOps in your private cloud, on your own servers or fully offline.
ISO/IEC 27001, 27701 and ISO 9001
Certified to ISO/IEC 27001:2022, ISO/IEC 27701 and ISO 9001. SOC 2 Type 2 is coming soon.
Why a Selenium run has no coverage report
A Selenium test runs in one process and the code it exercises runs in others: the web server, the APIs behind it, the services those call. A coverage tool attached to the test process sees only the test code. The usual workarounds each answer part of the question: instrumenting the front-end JavaScript shows what ran in the browser, and a coverage agent on one server, dumped after the suite, gives one number for the whole run on that server.
What those do not tell you is which test reached which code. That is what you need to decide which tests a change affects, to find the code no test touches, and to see what a failed test actually did on the server. It needs two things: measurement inside every service, and a label on each test's traffic that survives the hop from one service to the next.
Test coverage in the other sense, whether every requirement has a test, is a different question that test management tools answer. This page is about code: what your Selenium tests execute.
How it works with Selenium
- Start each service under test with the CoverOps agent: one
-javaagentflag on the JVM, a--requireentry for Node.js, or the agent for .NET, Python, Go or PHP. Nothing changes in your tests. - Add the runner integration. For Java it ships in the agent jar: a JUnit 5 extension and a TestNG listener. Python has a pytest plugin, JavaScript a WebdriverIO service, and Robot Framework a listener that labels SeleniumLibrary's browser for you.
- Label the browser. A browser cannot add custom headers, but it sends cookies on every page load and XHR, so one
covctxcookie per test, set after the first navigation to your app's domain, names the test, its id in your test tool and the run. - The integration opens the run before the first test and posts every verdict at the end. Services forward the test context on their outbound calls (automatically for Java and Node.js HTTP, with a helper elsewhere), so each test's code is attributed on every service it reached.
java -javaagent:coverageops-agent.jar \
-Dcoverageops.includes=com.yourcompany \
-Dcoverageops.trace=true \
-jar your-app.jar
# trace=true: the JVM agent attributes code to tests only with tracing on
# COVERAGEOPS_ENDPOINT and COVERAGEOPS_TOKEN from the environment@Test
@CoverageOpsTestId("AUT-45") // optional: the id in TestRail / Jira
void buysAKeyboard() {
driver.get(url); // the cookie needs the app's domain first
driver.manage().addCookie(new Cookie("covctx",
CoverageOpsExtension.currentContextCookie()));
…
}@pytest.fixture
def driver(coverops):
d = EventFiringWebDriver(webdriver.Chrome(), coverops.listener())
yield d
d.quit()The JUnit 5 extension is turned on for the whole project with junit.jupiter.extensions.autodetection.enabled=true, or per class with @ExtendWith(CoverageOpsExtension.class). The agent jar comes from your CoverOps address (/v1/maven), not Maven Central. Selenium in C#, Ruby or another language without a plugin sets the same cookie itself and reports verdicts through the Results API.
What you get
What each Selenium test did on the server
Every test gets a link in its own report (JUnit report entry, TestNG Reporter.log, pytest JUnit XML, Robot log) to its backend footprint: endpoints called, services crossed, methods run and lines executed, in request order.
Selenium tests that run only when a change reaches them
With COVERAGEOPS_SELECT=true the JUnit 5 extension skips the tests whose recorded footprint misses the change, with the reason in the report; pytest does the same with --coverops-select. New tests, tests without a footprint and recently failed tests always run.
A changed-code gate
Of the lines a pull request changed, how many did the tests of its build execute? coverageops gate answers in CI and can post the verdict as a commit status on GitHub, GitLab, Azure DevOps or Bitbucket.
Retries counted as evidence
Surefire's rerunFailingTestsCount, Gradle's test-retry and JUnit Pioneer's @RetryingTest are posted as attempts of one test. A pass on retry still passes the build, and the Flaky tests page counts it as flaky.
What is measured, and what is not
Measured
- Lines, methods and (on Java and Node.js) branches of your server-side code that each Selenium test executed, on every service with an agent.
- The requests each test sent, in order, and the services each one crossed.
- The suite as a whole: one run, even when it is split across Selenium Grid nodes or CI jobs that send the same run key.
Not measured
- JavaScript that runs in the browser. CoverOps has no browser agent; use your front-end coverage tool for that half.
- A test's very first page load: the cookie can only be set once the browser is on your domain. Everything after it is labelled.
- Exact lines per test when tests run in parallel against the same service process: each test then gets the lines of the shared time span, and the run page says how many tests overlapped. Requests, services and methods stay exact.
- Services without an agent, and languages outside Java, .NET, Node.js, Python, Go and PHP.
Frequently asked questions
Can Selenium measure code coverage by itself?
No. Selenium automates a browser and knows nothing about the code behind the pages. Coverage has to be measured in the application: in the browser for front-end JavaScript, and in each server process for the backend, which is what CoverOps does.
How does CoverOps know which Selenium test sent a request?
From the covctx cookie. The runner integration sets it once per test after the first navigation, the browser sends it with every page and XHR, and the agent reads it and passes the test context on to the next service as X-CoverageOps-* headers.
Does it work with Selenium Grid and parallel tests?
Yes. Where the browser runs does not matter, because the label travels in the cookie. When parallel tests hit the same service process at the same moment, their lines cannot be told apart, so each test keeps every line of the shared span and the overlap is shown. Selection may then run more tests, never fewer.
Which languages and runners are supported?
Plugins exist for JUnit 5 and TestNG (Java), pytest (Python), WebdriverIO (JavaScript) and Robot Framework. Selenium in any other language sets the same cookie by hand. The services under test can be Java, .NET, Node.js, Python, Go or PHP.
Do I have to change my tests?
Only to set the cookie, and not even that with the pytest listener, the WebdriverIO service or Robot Framework's SeleniumLibrary, which set it for you. JUnit and TestNG tests add one addCookie line after the first navigation.
Keep reading
Related pages
In the documentation
- How CoverOps measures: runs, test context, parallel tests
- Run detail: each test's footprint
- Test selection
- Integrations: runner setup snippets
The documentation is in English.
See your Selenium suite's backend coverage
Bring one service and a few Selenium tests to a demo; we show what each test reaches on a running system.