Want better speed for Selenium tests? Run them in parallel.
A few years ago, if anyone asked me how Selenium tests can become faster, my answer would be about:
- using fast selectors
- using fewer locators
- creating atomic tests
- not testing the same functionality twice
- writing good tests
- using only explicit waits
- using the chrome driver
- using drivers for headless browsers
- re-using the browser instance
- running scripts in parallel
- using HTTP parse libraries
- pre-populating cookies
- not loading images in the web page
────────────────────────────────────
Did anything change in the last few years?
- using fast selectors (minimal impact)
- using fewer locators (minimal impact)
- creating atomic tests (useful)
- not testing the same functionality twice (useful)
- writing good tests (useful)
- using only explicit waits (minimal impact but useful)
- using the chrome driver (useful)
- using drivers for headless browsers (useless; I don't remember the last time I used headless browsers)
- re-using the browser instance (useless; for tests to be truly independent, they should start in clean browsers)
- running scripts in parallel (essential)
- using HTTP parse libraries (useless; I don't remember the last time I used http parsing libraries)
- pre-populating cookies (useless; too much work for limited benefit)
- not loading images in the web page (useless)
────────────────────────────────────
Today, this list would be much shorter:
- running scripts in parallel in docker containers
- using the chrome driver
- creating atomic tests
- not testing the same functionality twice
- writing good tests
────────────────────────────────────
By default, Selenium tests are very slow because they
- run on a site in a browser; the browser performance depends on the computer/container it is launched on
- the site's performance depends on number of concurrent users, servers, bandwidth, etc
So, what can make a difference for the speed of Selenium tests?
Run them in parallel on as many docker containers as possible with pipelines like this:
pipeline {
agent any
stages {
stage ('Run All Jobs') {
parallel {
stage("Login") { steps { script {
runTestsFor("Login")
} } }
stage('My Account') { steps { script {
runTestsFor("My Account")
} } }
stage('Results') { steps { script {
runTestsFor("Results")
} } }
stage('Cart') { steps { script {
runTestsFor("Cart")
} } }
stage('User Registration') { steps { script {
runTestsFor("User Registration")
} } }
}//parallel
}//Run All Jobs
}
}
The pipeline has 5 stages with tests for different site features.
If all tests for each stage runs in a different docker container, you run all 5 stages in parallel.
This will not lead to a 5 times reduction in execution time.
But you will probably get the execution time reduced with 50%, maybe even more.
So, the only solution that really works for faster Selenium tests is running them in parallel in many, many docker containers.