App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
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 -

Table of Contents

    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 with Selenium on Automate, enabling testers to run automated tests on 3000+ real devices and browsers using its Cloud Selenium Grid. 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 Selenium 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.

    Talk to a Solutions Expert

    Optimizely Logo

    Did you know: Optimizely runs 15,000+ tests in 45 minutes

    Optimizely scaled its automation testing by moving from an on-premise grid to our cloud infrastructure.

    • 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.

    Automation Testing Selenium Selenium Webdriver

    Featured Articles

    5 Selenium tricks to make your life easier

    Locators in Selenium: A Detailed Guide

    App & Browser Testing Made Easy

    Seamlessly test across 20,000+ real devices with BrowserStack