Switching to Node.js binding to WebDriverIO and integrating with Sauce Labs.
This article is part of a series on exploring end-to-end testing with Selenium and Node.js; starting with Late to the Party; End-to-End Testing: Part 1.
Switching to WebdriverIO
- A colleague (thanks Felipe) of mine mentioned that he was using WebdriverIO
- A quick search on the Internet comparing the libraries brings up a highlighed article, Better Selenium Tests, with WebdriverIO, strongly recommending WebdriverIO
Not sufficiently convinced, I continued on with selenium-webdriver and discovered:
- The selenium-webdriver documentation is an API reference while WebdriverIO documentation consist of both both a developer’s guide and an API reference
- After a few minutes of poking, I found WebdriverIO’s API reference significantly better than selenium-webdriver’s
- Following the documentation on Sauce Labs, Instant Selenium Node.js Tests, it indicates “You should install Selenium WebDriver 2.47.0 or lower”. This version is over three-years old with the current version being 4.0.0-alpha.1. Related to this, I could not find the documentation for this older version
- In that same Sauce Labs document, they provide two Node.js examples; the first using WebdriverIO instead of selenium-webdriver
- WebdriverIO provides a deep integration to Sauce Labs
Needless to say, I was convinced.
(As before) In a new folder, initialize a new Node.js project by entering (accepting all defaults):
Then in the project’s root folder install the WebdriverIO library with:
npm install webdriverio
Create a file in the project’s root folder (found this example from WebdriverIO):
and run it with
Refactoring with Promise Interface
One interesting slant in WebdriverIO’s documentation (and other similar testing library documentation) is that make efforts to hide that this style of testing is inherently asynchronous.
The good news is that the WebdriverIO library supports promises and coupled with async / await you have readable code that celebrates the library’s asynchronous nature.
Unfamiliar with async / await? Read My Journey to Using Async / Await.
Integration with Sauce Labs
- It is not a good practice to hard-code in your credentials into your code (did it in this example to keep it simple); there are other ways of injecting them, e.g., environment variables or the config library
- It appears that their endpoint uses http vs. the secured https. If you have concerns about securing the connection, Sauce Labs offers Sauce Connect Proxy.
Sauce Labs provides details on the test, including the commands and a video showing screenshots along the way.
Deeper Integration with Sauce Labs
Sauce Labs provides a npm library, saucelabs, that we can use to report the status of the job.
npm install saucelabs
With this in place, the job is named and is reported as passing:
So far, we have been running commands against a Selenium server; we really have not been testing. In the next article, Late to the Party; End-to-End Testing: Part 3, we will write out first test.