WebDriver vs Chrome DevTools  (Speed Test)
Published: 2020-06-20 20:18:44

Today I got inspired by @FedericoToledo to write a test script that can automate a simple clicking game, but with a focus on executing as fast as possible. 

<blockquote class="twitter-tweet" data-lang="en" data-theme="dark"><p lang="en" dir="ltr">Test automation challenge: Can you prepare a test script to win the game 1to50 automatically as fast as possible <a href="https://t.co/H6mwYxeKko">https://t.co/H6mwYxeKko</a>???<br><br>My solution with Ruby and Selenium took 4.9secs to win the game and I know it can be faster <a href="https://t.co/HadbaCzien">https://t.co/HadbaCzien</a> <a href="https://t.co/UrPDh2bfGX">pic.twitter.com/UrPDh2bfGX</a></p>&mdash; Federico Toledo (@fltoledo) <a href="https://twitter.com/fltoledo/status/1248789014632476672?ref_src=twsrc%5Etfw">April 11, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

This challenge inspired me to use my favorite Web Testing Framework [webdriverIO](https://webdriver.io/) which support devtools and webdriver protocols out of the box with minor setup. So I decided to try and see which protocol executes faster.


##Let's explain the details first.
In order to make your tests scripts execute actions against a website your test framework has to use an interface which is actually capable of controlling the browser.

There are 2 very popular interfaces that allow this to happen. Selenium uses **WebDriver** (w3c protocol) which is today the most popular one, but there is also **Chrome DevTools** which is native to chromium browsers and has some benefits and disadvantages over its competitor. In this post we are focusing on speed, and _spoiler alert_ our winner is devTools.


###Here is why!
WebDriver sits in the middle between the test scripts and the browser, and uses http traffic to communicate back and forth which can cause a small latency between the execution and the response from the browser.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z9vv3vqm71k2mlqb766l.png)

On the other hand Chrome DevTools allows direct control over the browser by using the [Puppeteer library](https://developers.google.com/web/tools/puppeteer) which is finally used by WebDriverio. This means direct execution and less latency on the results.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wq817yh0nrgu8m38gjdb.png)


##Let's do the test

The idea of the game is to click the numbers from 1 to 50 as fast as possible, but only the next 25 are displayed. So if you click 1 then the 26 appear. [Game](https://zzzscore.com/1to50/en/?ts=1592666149743)


I created a simple script using WebdriverIO on standalone mode so that it runs fast without all the test framework setup.

```js
const { remote } = require('webdriverio');
(async () => {
    const browser = await remote({
        logLevel: 'trace', capabilities: { browserName: 'chrome'}
    })
    await browser.url('https://zzzscore.com/1to50/en/?ts=1592666149743') // navigate
    for(i = 1; i<51; i++){
        var element = await browser.$('//div[text()="@id"]'.replace('@id',i))
        await element.click(); //click each number
    }
    var result = await browser.$('.level');
    console.log(`The result is ${await result.getText()}`);
    // await browser.deleteSession()
})().catch((e) => console.error(e))
```

By default webdriverIO v6 will try to connect using devtools protocol unless a explicit WebDriver path is specified.

To run the script just type 
```bash
node <scriptname>.js
``` 


####DevTools Result
The first thing you will notice when executing the script is this line of log: `INFO webdriverio: Initiate new session using the devtools protocol` which indicates the used protocol.

And you get this result: _**The result is 0.85**_. Which is really fast, it meant at least 50 browser interactions in less than a second.

####WebDriver Result
In order to execute the same script on WebDriver protocol, we need to specify the `hostname` and `path` when creating the remote object. We also require a running webdriver server. I recommend installing the [webdriver-manager](https://www.npmjs.com/package/webdriver-manager) package which simplifies the process a lot.

Once the webdriver is running (by default it runs on localhost:444/wd/hub), then just update the code.

```js
...
 const browser = await remote({
        logLevel: 'trace', capabilities: { browserName: 'chrome'},
        hostname: 'localhost', path:'/wd/hub'
    })
...
```

Again you will notice that the webdriver protocol is specified: `INFO webdriverio: Initiate new session using the webdriver protocol`.

And the result is: _**The result is 3.801**_ which is impressing but it is 4 times slower than the first execution and places webdriver in the second place.




#Final Thoughts
We can easily conclude that devtools protocol execute scripts way faster which makes me consider it as a scripting and scraping tool. But there is no free lunch, webdriver is now days the most used interface and the one that has support for cross browser testing, devices and even desktop apps.

I will also mention the great capability of webdriverIO to support both protocols out of the box. It really matters a lot when a tool allows you the power of choice.




_Credits for the protocols images to webdriverio website_