WebdriverIO - Data Driven Testing



We can achieve data driven testing with WebdriverIO. Data driven testing is required when we need to execute the same test case multiple times with different combinations of data. Here, we shall see how to use an external JSON file to hold data.

In the WebdriverIO project all the test files are created within the specs folder. The specs folder resides within the test folder. We shall create another folder, say testData within the test folder.

The testData folder shall contain the JSON files which hold the different sets of data in key-value pairs. Also, if we have three test files within the spec folder and we want to have data driven testing for all these files, we need to create three JSON files.

Each of these JSON files should be used dedicatedly for each test file within the spec folder. We shall create a JSON file, say test1.json within the testData folder.

Now, add the below data within this file −

[
   {
      "email":"test@gmail.com",
      "password":"12"
   },
   {
      "email":"test12@gmail.com",
      "password":"34"
   }
]

The following screen will appear on your computer −

Driven Testing

We shall parse this JSON file and convert it in string format. This is done by adding the below library −

const s =require('fs')

Then to parse the JSON file, we shall use the readFileSync method and pass the relative path of the JSON file file as a parameter to this method. Finally, store this in an object, say c. This object shall contain all the data.

let c = JSON.parse(s.readFileSync('test/testData/test1.json'))

Then, we shall iterate the same test case over the two sets of data with the help of the loop. This loop has to be implemented just before the block and it should pass the data keys as declared in the JSON file.

With the above set of data, we shall validate the login page of the LinkedIn application. On clicking on the Sign in button after entering an email and password of less than 6 characters, an error message - The password you provided must have at least 6 characters should be thrown.

The following screen will appear on your computer −

Case Over

To begin, follow Steps 1 to 5 from the Chapter titled Happy path flow with WebdriverIO which are as follows −

Step 1 − Install NodeJS. The details on how to perform this installation are given in detail in the Chapter titled Getting Started with NodeJS.

Step 2 − Install NPM. The details on how to perform this installation are given in detail in the Chapter titled Installation of NPM.

Step 3 − Install VS Code. The details on how to perform this installation are given in detail in the Chapter titled VS Code Installation.

Step 4 − Create the Configuration file. The details on how to perform this installation are given in detail in the Chapter titled Configuration File generation.

Step 5 − Create a spec file. The details on how to perform this installation are given in the Chapter titled Mocha Installation.

Step 6 − Add the below code within the Mocha spec file created.

//import chai library
const c = require('chai').expect
//library for parsing JSON file
const s =require('fs')
let h = JSON.parse(s.readFileSync('test/testData/test1.json'))
// test suite name
describe('Tutorialspoint application', function(){
   //iterate the test case
   h.forEach(  ({email,password})  =>{
      //test case
      it('Data Driven testing', function(){    
         // launch url
         browser.url('https://www.linkedin.com/login')  
         //identify the email field then enter key - email
         $("#username").setValue(email)
         //identify password field then enter key - password
         $("#password").setValue(password)
         //identify Sign in button then click
         $("button[type='submit']").click() 
         //verify error message
         const e = $('#error-for-password')
         console.log(e.getText() + ' - Error Text') 
         //verify Alert text with Chai assertion
         c(e.getText()).to.equal("The password you provided must have at least 6     characters.")
      });
   });
});  

Run the Configuration file - wdio.conf.js file with the following command −

npx wdio run wdio.conf.js 

The details on how to create a Configuration file are discussed in detail in the Chapter titled Wdio.conf.js file and Chapter titled Configuration File generation.

The following screen will appear on your computer −

Case Over Screen

After the command has been executed successfully, the error text - The password you provided must have at least 6 characters gets printed in the console twice.

Also, it shows the message 2 passing as the same test case defined in one block has executed two times with two different sets of data.

Advertisements