Testing code might not sound like the most fun experience you can imagine as a programmer, good news that you can automate it. This post will go through how to automatically launch your browser using Selenium and programmatically act as a user to login in to a web service. We will use Netflix as an example for demo purposes only, but please note that we are not in any way affiliated with Netflix’ services or the trademark. The script is partly similar to that of our previous post using Protractor for testing an Angular web app, however this automated test is disconnected from Angular and based on Node.js.

What components do you need?

  • Node.js installed (here is a link for download)
  • Selenium installed (you can do a ‘npm install selenium-webdriver’ if you have Node.js installed)
  • WebdriverIO installed (‘npm install webdriverio’)

That’s it! We’re ready to go!

Creating the test

We start by creating an empty folder with two files: conf.js and autologin.js. The file ‘conf.js’ is just a configuration file to tell Selenium which browser to use, test frameworks etc. You could either create your config file through the command line wizard by typing wdio config in the Terminal window or copy ours from gitlab.com/yellington/WebdriverIO. If you use the command line wizard, note that we have manually fine tuned two entries below. Since we put the conf.js and autologin.js files in the same folder, the specs path in the conf.js file has to point to autologin.js residing in the same folder.

specs: [

We also extend the timeout for the tests to ‘99999999’ to make sure we have plenty of time to manipulate the login sequence.

mochaOpts: {
        ui: 'bdd',
        timeout: 99999999

The conf.js file can specify a lot more parameters such as which web browser you prefer to run the tests on.

Programming the login sequence

Downloading the HTML page and traversing trough it is a quite dirty way of interfacing a web service. However, it is a good demo of how you can interact with a web service that does not provide an official web API. The login sequence in the autologin.js file is test runner based and use synchronous calls. You can download it from gitlab.com/yellington/WebdriverIO and it looks like:

var loginData = require('./login.json');

describe('Automatical login to Netflix', function() {
    it('should step through to the main movie page', function () {

        //Open the login page for Netflix

        //Identify the input fields for email and password by finding the first <form> 
        var loginForm = browser.element('<form>'); //<form class="login-form" ...
        var inputFields = browser.elementIdElements(loginForm.value.ELEMENT, '.ui-text-input'); // use the class 'ui-text-input' as selector for the input fields
        //Enter the value for the email and password...
        browser.elementIdValue(inputFields.value[0].ELEMENT, loginData.email);
        browser.elementIdValue(inputFields.value[1].ELEMENT, loginData.password);

        //...and submit the form.

        //Select which user you want to be logged in as. In this case it's user number 4 (i.e. index 3)
        var profileLink = browser.elements('.profile-link');

        //Paus for 5 seconds to admire the beautiful login

Now, this code is created based on the current DOM structure Netflix happens to have at the time of coding. If they decide to change it, this code has to change as well. Let’s explain the code step by step:

  • browser.url opens the login page.
  • Next we use the <form> tag as a selector to retrieve the first form (the login page has several) as a web element.
  • Then we use the class tag ‘.ui-text-input’ that returns an array with the two input fields for email and password.
  • Enter the login credentials in the input fields. In our case we have stored them in a separate file in JSON format. You can simply replace ‘loginData.xxx’ with a text string with the email and subsequently the password.
  • Submit the form.
  • Next, you get a splash screen with a number of user profiles (yours, your girlfriend/boyfriend/wife/kids etc). We select user number 4 (i.e. index 3).
  • Paus for 5 seconds to observe the movie page load.

You find files at gitlab.com/Yellington/WebdriverIO.

Automated web testing using WebdriverIO


E-postadressen publiceras inte. Obligatoriska fält är märkta *