How to Run a Test on Multiple Browsers, In Parallel
Intro
In the previous tip I covered how to run a single test against multiple browsers. While this could come in handy, it is still a bit lacking.
Right now, the test is only being run one at a time (a.k.a. in series) for each browser in our list. If this were a longer running test and/or if there were more browsers we cared about, then this could quickly stack up to longer test run times.
A Solution
Let's remedy this by taking the previous tip's example and parallelize it.
Example
NOTE: You'll need a Sauce Labs account, with your username and access key exported as the environment variables
SAUCE_USERNAME and SAUCE_ACCESS_KEY. See How to Run Your Tests on Any Browser & Operating System
for the details. If they are not set, the tests are skipped.
Our setup stays mostly the same as in the previous tip: a collection of browsers (Chrome, Firefox, and Edge), and a test that runs once for each of them in Sauce Labs. The only change is that we now run the browsers at the same time, with the parallel features of each test framework (or with threads).
When running things in parallel, each test needs its own browser, so they don't step on each other. When we run it, we should see three tests running simultaneously in Sauce Labs, and the whole run takes about as long as the slowest browser, instead of the sum of all of them.
NOTE: The number of parallel sessions allotted to your Sauce Labs account will dictate how quickly your tests will finish. For instance, if you add more browsers than the sessions you have, Sauce Labs queues the extra ones until some of the original sessions finish.
Expected Behavior
- Simultaneously loads each browser in a Sauce Labs job
- Each test loads the page and asserts the title is correct
- Each session closes when it is complete
Summary
For a full write-up on running a whole test suite in parallel, check out tip 27.
Happy Testing!