JavaScriptExecutor : Part-1

JavaScriptExecutor

Before we discuss on JavaScriptExecutor, We will discuss what is JavaScript actually?

So,

What is JavaScript ?
  • Java Script is an object-oriented programming language commonly used to create interactive effects within the web browser to Handle WebElements within Web Page.
  • It is used by Selenium WebDriver to do handle WebElements.
Now lets us discuss,

What is JavaScriptExecutor?
  • The JavaScriptExecutor is an interface that provides mechanisms to execute javascript through the Selenium Driver.
  • To run JavaScript in the context of the currently selected frame or window, it provides two methods i.e. "executescript" and "executeAsyncScript"
  • The basic advantage of JavaScriptExecutor is that Selenium provides a way to run Java Script in webdriver.
  • Sometimes the locator can not work, in that case the JavaScriptExecutor will help interact with web elements on a particular webpage.
  • The reason behind this is, even Selenium webdriver convert the bindings of the language internally to the equivalent of javascript and inject them into the related browser.
Components of JavascriptExecutor in Selenium:
JavascriptExecutor consists of two methods that handle all essential interactions using JavaScript in Selenium. executeScript method:
  • This method executes the test script in the context of the currently selected window or frame. The script in the method runs as an anonymous function.
  • If the script has a return statement, the following values are returned:
    • For an HTML element, the method returns a WebElements.
    • For a decimal, the method returns Long.
    • For a non-decimal number, the method returns Long.
    • For a Boolean, the method returns Boolean.
    • For other cases, the method returns a String.
executeAsyncScript method: 
  • This method executes the asynchronous piece of JavaScript on the current window or frame. An asynchronous script will be executed while the rest of the page continues parsing, which enhances responsiveness and application performance.
The basic syntax for JavascriptExecutor is given below:

Syntax:

    JavascriptExecutor js = (JavascriptExecutor) driver;  
    js.executeScript(Script, Arguments);

Script – This is the JavaScript that needs to execute.
Arguments – It is the arguments to the script. It's optional.

Generate Alert Pop Window:
    JavascriptExecutor js = (JavascriptExecutor)driver;
    Js.executeScript("alert('VDS TECH LABS');");

Click Action:
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].click();", element);

Refresh Browser:
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("history.go(0)");

Get Inner Text of a Webpage:
    JavascriptExecutor js = (JavascriptExecutor)driver;
    String sText =  js.executeScript("return
    document.documentElement.innerText;").toString();

Enter text by JS: 
     JavascriptExecutor js = (JavascriptExecutor)driver;
     js.executeScript("document.getElementByName('userName').value='admin'");      -->1st Way
     js.executeScript("arguments[0].value=arguments[1]",uName,"admin"); 
  -->2nd Way
     js.executeScript("arguments[0].value='admin'", password);
  --> 3rd Way

Get Title of a WebPage:
JavascriptExecutor js = (JavascriptExecutor)driver;
String sText =  js.executeScript("return document.title;").toString();

Scroll Page:
  JavascriptExecutor js = (JavascriptExecutor)driver;
  //Vertical scroll - down by 150  pixels
  js.executeScript("window.scrollBy(0,150)");

Finally, we should know:

Why use JavaScriptExecutor in Selenium?
  • Sometimes, Selenium WebDriver alone will not be able to perform certain operations or interact with web elements. In that case, JavaScript is needed to make sure those actions are being performed accurately.
  • JavaScriptExecutor is also very useful for identifying hidden elements and interacting with them on the webpage.
  • To understand its importance, let’s consider an example, suppose a tester has written an automation script to click a few buttons, but there seems to be an issue due to which the script fails every time. To resolve this, the tester uses JavascriptExecutor.

THANKS FOR READING!!!

Comments

Post a Comment

Popular posts from this blog

Python namespaces & LEGB rule in Python