Below code will illustrate how we can execute java script in selenium webdriver.
Please note that we need to import below interface
import org.openqa.selenium.JavascriptExecutor;
String x = ((JavascriptExecutor)
driver).executeScript("return
document.documentElement.innerText;").toString();
Returning the document node of the webpage
Finding the browser name using JavascriptExecutor
public boolean getBrowserName(){ String agent = (String) ((JavascriptExecutor) driver)
.executeScript("return navigator.userAgent"); if (agent.toLowerCase().contains("chrome")){ return "Chrome"; }else if (agent.toLowerCase().contains("firefox")){ return "Firefox"; }else if (agent.toLowerCase().contains(".net")){ return "IE"; }else if (agent.toLowerCase().contains("macintosh") ||
agent.toLowerCase().contains("safari")){ return "Safari"; } }
Finding the screen width using JavascriptExecutorboolean isScreenWidthSmall() { Long width = (Long)(((JavascriptExecutor) driver).executeScript
("return window.screen.width;")); if (width < 767) return true; else return false; }
Scrolling using JavascriptExecutor
public void scrollToElement(WebElement element){ ((JavascriptExecutor) driver).
executeScript("arguments[0].scrollIntoView();",element); ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,-111)"); } public void scrollToTop(){ ((JavascriptExecutor) driver).executeScript("window.scrollTo(200,200)"); }
Get document state using JavascriptExecutor
public String getDocumentState(){ String state = ((JavascriptExecutor)driver).executeScript("return document.readyState;"); return state; }
public String getInnerHtml(WebElement element){ ((JavascriptExecutor)driver).
executeScript("return arguments[0].innerHTML;",element); }
Clicking on an element using JavascriptExecutor
protected void clickByJavaScript(WebElement element) { ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element); }
Complete example to execute javascript is given below in Java.
package abc;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
public class ExecuteJavaScript {
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
try{
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.google.com");
String x = ((JavascriptExecutor) driver).executeScript("return document.documentElement.innerText;").toString();
//sometimes we just want to click on the elements so we can use below code
//below statement will click on webelement e.
//(JavascriptExecutor) driver).executeScript("arguments[0].click();",e);
//print the contents of the open page
System.out.println(x);
Thread.sleep(2000);
}
catch(Exception ex){
System.out.println(ex.toString());
}
finally{
driver.close();
driver.quit();
}
}
}
Javascript plays very important role when we are not able to perform complex operation on the element on the webpage. We can use all HTML DOM methods and properties when using javascript.
We can pass the parameters to the execute the script as shown below.
((JavascriptExecutor) TestExecutor.wb).executeScript("arguments[0].click()",e);
In above code I have passed the webelement - e to executescript method. I can access the passed argument using arguments[] array.
No comments:
Post a Comment