WebdriverIO - JavaScript Executor



Inside the WebdriverIO, the JavaScript Executor is bundled and called executeScript. The JavaScript Executor is capable of performing all the tasks on a page whenever normal WebdriverIO methods are not working as expected.

Syntax

The syntax for the JavaScript executor is as follows −

browser.executeScript("JavaScript command")

Actions with JavaScript Executor

Some actions performed with JavaScript Executor are as follows −

To enter a text - AB into an edit box having id as txt, use the command given below −

browser.executeScript("document.getElementById('txt').value='AB'")

To click a link, use the command given below −

browser.executeScript("document.querySelector('.lnk').click()") 

The command given below is used for refreshing windows −

browser.executeScript("history.go(0)")
var t = js.executeScript("return document.getElementById('bln').innerHTML").toString()

The command to scroll down a page by 350 pixels is as follows −

browser.executeScript("window.scrollBy(0,350)")
browser.executeScript("window.scrollTo(0, document.body.scrollHeight)")

The command given below is used to scroll down upto an element having class as tcl.

browser.executeScript("document.querySelector('.tcl').scrollIntoView()")
browser.executeScript("window.history.back()")

Following command is used to go forward in browser history −

browser.executeScript("window.history.forward()")
browser.executeScript("return document.title")
Advertisements