Home Guide How to make your Selenium test cases run faster

How to make your Selenium test cases run faster

Shreya Bose, Technical Content Writer at BrowserStack -

The entire point of automated Selenium testing is to introduce speed into the software testing lifecycle. To this end, it is important to write and execute Selenium test cases for maximum speed. This article highlights a few methods that can accelerate Selenium test cases, thus giving faster results and allowing faster time to market for new software.

  • Parallel Testing

This is the easiest way to speed up Selenium test cases. Simply run automated tests on different device-browser-OS combination simultaneously, so that the entire test suite can be completed in much lesser time.

Essentially, if there are ten tests to be run, execute each one on a different device at the same time. If each test takes ten seconds to run, the entire test suite will be completed in ten seconds. If the suite had been run on a single device, it would have taken 100 seconds.

BrowserStack offers parallel testing on Automate, enabling testers to run automated tests on 2000+ real devices and browsers. Each parallel test comes with a user account, which gives the user access to unlimited Live and Screenshots, apart from Automate.

Try Parallel Test Automation for Free

  • Write shorter tests

Ensure that each Selenium test is meant to test a single functionality. A helpful guide would be to keep the number of steps in each test under 20. While parallel testing can certainly speed up the process, keep in mind that a single test must run on a single device, not multiple ones. Therefore, it’s a good practice to keep tests short.

Additionally, short tests have another advantage. If the test fails, it is easier to pinpoint the problem to a specific line of code, if that is necessary.

  • Refine Test Scripts

To write Selenium tests that run faster, write independent test scripts that do not duplicate steps. Ensure that there are no steps or methods that are not directly related to the final outcome.

  • Use only Explicit Waits

By using Explicit Wait commands, Selenium WebDriver is directed to wait until a certain condition occurs before proceeding with executing the test script.

Setting Explicit Wait is important in cases where there are certain elements on a website that naturally take more time to load. If one sets an Implicit Wait command, then the browser will wait for the same time frame before loading every web element. This causes an unnecessary delay in executing the test script.

As far as possible, using Explicit Wait commands will eliminate delays in running Selenium tests.

  • Use fast selectors in test scripts

Among the many locators that can be used in Selenium Webdriver, it is best to use the fast ones. A few of them are:

1. search by ID

This locator functions if the html element is carrying the id attribute. It works the fastest since it uses the document.getElementById() javascript command. This command is optimized for all browsers.

2. NAME selector

This locator functions if the element being located has a name attribute.

3. XPath selector

XPath selector is known for providing flexibility, but are the slowest of all selectors. This is because the browser DOM of the webpage needs to be traversed in order to find the specific element. When writing scripts, use XPath selectors as rarely as possible.

4. CSS selector

CSS Selector is faster than XPATH but definitely lacking in terms of flexibility.

As far as possible, incorporate the strategies discussed above into the creation of Selenium test scripts. By shaping test cases in a way that makes them run faster, it reduces test times. This means bugs are identified and resolved faster. Overall, it reduces the time to push new code to production.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free