Wednesday, 6 July 2016

What is javascript executor?



Javascript Executor:
=============
javascriptexecutor is an interface. and which is available in org.openqa.selenium.JavascriptExecutor;
which provides mechanism to execute Javascript through selenium driver. It provides “executescript” & "executeAsyncScript" methods, to run JavaScript in the context of the currently selected frame or window. 
We can use Javascript to perform actions Using selenium Webdriver. By using ‘JavascriptExecutor’ we can perform these actions


Why we use it?
==========
To enhance the capabilities of the existing scripts by performing javascript injection into our application under test.


In simple words  “Javascript can be executed within the browser with the help of JavaScript Executor.”

Mousehover:
=========
WebElement element = driver.findElement(By.xpath("//a"));
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].onmouseover()", element);

Click():
=====
WebElement element = driver.findElement(By.id(“Email”));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(“arguments[0].click();”, element);

WebElement element = driver.findElement(By.className(“”));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(“arguments[0].click();”, element);


OR:
===
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“window.document.getElementById(‘Email).click()”);


How to refresh the browser window using Javascript:
===================================
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.executeScript("history.go(0)");

Get Title:
=======
driver.get("https://www.google.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript(“return document.title”);
System.out.println(“title : ” + title);
OUTPUT | title : Google

or
JavascriptExecutor js = (JavascriptExecutor)driver;
string sText =  js.executeScript("return document.title;").toString();
Scroll bar operations:
  JavascriptExecutor js = (JavascriptExecutor)driver;
  //Vertical scroll - down by 50  pixels
  js.executeScript("window.scrollBy(0,50)");

Get Domain name:
============
driver.get(“https://www.google.com);
JavascriptExecutor js = (JavascriptExecutor) driver;
String domain = (String) js.executeScript(“return document.domain”);
System.out.println(“domain : ” + domain);
OUTPUT | domain :
http://www.google.co.in

Get URL:
======
driver.get(“http://google.co.in/”);
JavascriptExecutor js = (JavascriptExecutor) driver;
String URL = (String) js.executeScript(“return document.URL”);
System.out.println(“Full URL : ” + URL);
OUTPUT | Full URL :
https://www.google.co.in/

Get Attribute | Text:
==============
driver.get(“http://google.co.in/”);
Object exampleDiv = ((JavascriptExecutor) driver).executeScript(“return document.getElementById(‘main’);”);
String name = ((WebElement) exampleDiv).getAttribute(“class”);
System.out.println(name);
OUTPUT | content


Last Modified:
=========
driver.get(“http://google.co.in/”);
JavascriptExecutor js = (JavascriptExecutor) driver;
String lastModified = (String) js.executeScript(“return document.lastModified”);
System.out.println(“lastModified : ” + lastModified);
OUTPUT | lastModified : 01/29/2014 14:56:46





No comments:

Post a Comment