With colleagues we had discussion about the performance of different WebDriver locators. According some sources especially the XPATH locator can have slow performance compared to CSS and other locators. To get rough idea about the performance of different locators I decided to make a quick test on Firefox, Chrome and Edge.
Test case
Test measures time used to find an element from Twitter front page using different selectors. The finding of element is repeated 100 times to get more reproducible and statistically reliable results. The locators used in test were:
- By.name(“session[username_or_email]”)
- By.className(“email-input”)
- By.id(“signin-email”)
- By.linkText(“Forgot password?”)
- By.xpath(“//*[@id="signin-email"]”)
- By.cssSelector(“input.text-input.email-input”)
Test Environment
- Windows 10
- Firefox 45.0.1
- Chrome 49.0.2623.110
- Edge 25.10586.0.0
- Java 8 (64bit, version 1.8.0_31)
- WebDriver 2.53.0
- FirefoxDriver 2.53.0
- ChromeDriver 2.21.371459
- MicrosoftWebDriver 3.14316
Results
Here are the measurement results. The time is total time of 100 findElement() method calls using the given locator. The slowests execution times per each browser are bolded.
Locator | Firefox | Chrome | Edge |
name | 1377 ms | 929 ms | 204 ms |
className | 1795 ms | 902 ms | 199 ms |
id | 1600 ms | 851 ms | 262 ms |
linkText | 9056 ms | 1522 ms | 238 ms |
xpath | 2229 ms | 919 ms | 247 ms |
cssSelector | 1280 ms | 809 ms | 219 ms |
Summary
The executed test is just a quick measurement to get rough idea about the locator performance. The performance is naturally affected by context (e.g. page content, locator queries and test environment), and therefore the results should not be used as conclusive results but rather as starting point for additional analysis.
However, what can be seen from the results is that the performance is highly affected by the used browser. In fact, the used browser seems to be more dominant factor for the performance than the used locator type. Supprisingly the Edge seems to beat Firefox and Chrome in performance by far. Can Edge really be this fast?
Another finding was very poor performance of linkText -locator in Firefox, whereas is Chrome and Firefox no similar performance drop was observable. Also bit supprisingly, the id -locator was not always the fastest locator (as we initially expected).
As conclusion, the performance of locators in most cases has only minimal (or none) effect on the test execution time and result varies on each browser. In most normal test cases the test execution times are affected on ten- to hundredfolds by the wait strategies used in tests.
Test code
If interested to carry out your own additional measurements, below is the test case used for the measurements.