Late to the Party; End-to-End Testing: Part 2

July 19, 2018 0 Comments

Late to the Party; End-to-End Testing: Part 2

 

 

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

The rationale for using the Node.js selenium-webdriver library in the previous article was because it is Selenium’s officially supported JavaScript language binding.

The rationale for switching to using WebdriverIO (another JavaScript language binding) started with:

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

WebdriverIO

(As before) In a new folder, initialize a new Node.js project by entering (accepting all defaults):

npm init

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):

hello.js

Observation:

and run it with

node hello.js

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.

hello.js

Integration with Sauce Labs

The WebdriverIO library can be easily configured to use Sauce Labs to run your tests; requires a Sauce Labs account (they have a free trial).

hello.js

Observations:

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

As indicated by the question mark below, Sauce Labs does not report if the test was successful because the business logic (if the test was successful) resides in the JavaScript code.

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

And update:

hello.js

With this in place, the job is named and is reported as passing:

Next Steps

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.


Tag cloud