How to work with id locator in WebdriverIO?

Rest AssuredDynamic ProgrammingProgramming

We can work with the id locator in WebdriverIO. Once we navigate to a webpage, we have to interact with the web elements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.

For this, our first job is to identify the element. We can use the id attribute for an element for its identification. It is a very useful locator and speeds up the execution of automation tests in comparison to all the locators.

In the WebdriverIO code, we have the option to specify the value of the id attribute of an element in the below format −

$('=#value of id attribute')
Or, we can store this expression in a variable:
const p = $('=#value of id attribute')

Let us identify the element highlighted in the below image and click on it −

The link highlighted in the above image has a tagname - a and the id attribute value - redirect.

Example

Code Implementation

// test suite name
describe('Tutorialspoint application', function(){

   //test case
   it('Identify element with Id', 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())
   });
});

Output

After the command has been executed successfully, the title of the page after clicking - The Internet is printed in the console.

raja
Updated on 19-Nov-2021 10:51:12

Advertisements