Execute Tests with Mocha Options



A test file within the specs folder consists of the describe and it blocks. A describe block refers to the test suite and the it block refers to the test case. A describe block can have multiple blocks.

The details on how to create describe and it blocks are discussed in detail in the Chapter titled Happy path flow with Webdriverio.

To verify if a new build obtained from the development team is a healthy one, we need not execute all the test cases within a suite. A few test cases are identified for smoke/sanity testing and they are executed once we have a new build.

We can use the Mocha option called Grep to group test cases and run them together. For this, we have to add a keyword, say Smoke within the it description. Then at the runtime, we can instruct the WebdriverIO test to only trigger the it blocks which have Smoke in its description.

Let us take a test file having four it blocks. Out of the four it blocks, there are two it blocks having the keyword Smoke in description.

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 SSign 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 must be provided.")
      });
   });
   // it is blocked with Smoke keyword
   it('Identify element with Id - Smoke', function(){
      // launch url
      browser.url('https://the-internet.herokuapp.com/redirector')
      //identify element with id then click
      $("#redirect").click()
      //obtain page title
      console.log('Page title after click: ' + browser.getTitle())
   });
   // it block with Smoke keyword
   it('Identify element with Tagname - Smoke', function(){    
      // launch url
      browser.url('https://www.tutorialspoint.com/about/about_careers.htm')
      //identify element with tagname then obtain text
      console.log($("<h1>").getText() + " - is the text.")
   });
   //test case
   it('Identify element with Class Name', function(){        
      // launch url
      browser.url('https://www.tutorialspoint.com/about/about_careers.htm')
      //identify element with Class Name then obtain text
      console.log($(".heading").getText() + " - is the text.")
   });
});

To trigger only the it blocks connected with Smoke, run the Configuration file - wdio.conf.js file with the following command −

npx wdio run wdio.conf.js --mochaOpts.grep Smoke

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 −

Execute Tests Mocha Options

After the command has been executed successfully, we find out of the four it blocks, only two it blocks (having Smoke tag in description) have been executed.

Advertisements