WebdriverIO - Waits



The waitUnit method in WebdriverIO is a standard method to wait for an action /element on the page. It waits for a criterion to be met (a true value).

For example, we often wait for a text to appear on the page.

Syntax

The syntax for waitUnit method is as follows −

browser.waitUntil(condition, { timeout, timeoutMsg, interval })

Here,

  • condition = condition for waiting on.

  • The timeout is in milliseconds. The default value is 5000 and is an optional parameter.

  • The timeoutMsg is the error message thrown when there is a timeout and it is an optional parameter.

  • The interval is the interval in between verification. The default value is 500 and it is also an optional parameter.

In the below image, let us click on the link - Team and wait for the text - Team @ Tutorials Point to appear on the page.

WaitUnit Method

On clicking the link Team, the highlighted message is displayed on the page.

Tutorialpoint

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.

// test suite name
describe('Tutorialspoint Application', function(){
   //test case
   it('Waits', function(){    
      // launch url
      browser.url('https://www.tutorialspoint.com/about/about_careers.htm')  
      //identify then click link - Team
      const p = $('=Team')
      p.click()
      //wait for text
      browser.waitUntil(
         () => $('<h1>').getText() === 'Team @ Tutorials Point', {
            timeout: 6000,
            timeoutMsg: 'expected text did not match'
         }
      );
      //identify required text
      const m = $('<h1>')
      console.log(m.getText())   
   });
});

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 titledWdio.conf.js file and Chapter titled Configuration File generation. The following screen will appear on your computer −

Wait Screen

After the command has been executed successfully, the text generated on clicking the Team link - Team @ Tutorials Point gets printed in the console.

Advertisements