Let's have a look at few of the most common scenarios for JavascriptExecutor. All of the reusable methods are included in TestBase.java:
- The following code waits for the page or document to load completely:
public static boolean docState() {
docReady = ((JavascriptExecutor) driver)
.executeScript("return document.readyState").toString()
.equals("complete");
return docReady;
}
- The following code waits for an AJAX call to complete:
public static boolean ajaxState() {
int counter = 0;
while (counter < 10) {
ajaxIsComplete = (Boolean) ((JavascriptExecutor) driver)
.executeScript("return jQuery.active == 0");
if (ajaxIsComplete) {
break;
} else {
counter = counter + 1;
}
}
return ajaxIsComplete;
}
- The following code sets the focus and scrolls into view:
public static void executeFocus(String elementID) {
jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('" + elementID
+ "').focus();");
}
public static void scrollintoviewJS(WebElement element) {
jsscript1 = "arguments[0].scrollIntoView(false);";
js.executeScript(jsscript1, element);
}
- The following code sets the style visibility by id:
public static void setStyleVisibility(String id) {
jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('" + id
+ "').style.visibility = 'visible';");
}
- The following code sets the style block by id and name:
public static void setStyleVisibilityByidBlock(String id) {
jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('" + id
+ "').style.display='block';");
}
public static void setStyleVisibilityBynameBlock(String name) {
jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementsByName('"+name+"')
[0].style.display='block';");
}
Let's see the scroll in action with an example.
The following code opens the JQueryUI URL, scrolls down to the Easing hyperlink, and clicks it:
public class ScrollintoView {
public static void main(String[] args) {
String userDirectory = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver",
userDirectory+"\src\main\resources\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(15,
TimeUnit.SECONDS);
driver.navigate().to("https://jqueryui.com");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement txtEasing = driver.findElement(By
.xpath("//a[text()='Easing']"));
js.executeScript("arguments[0].scrollIntoView(true);",
txtEasing);
txtEasing.click();
}
}
The following piece of code will use the same HTML (DoubleClick.html) and, without double clicking the h2 header, it will display the paragraph by altering the CSS appropriately:
public class GetHiddenText {
public static void main(String[] args) {
String userDirectory = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver",
userDirectory+"\src\main\resources\chromedriver.exe");
RemoteWebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(15,
TimeUnit.SECONDS);
driver.navigate().to(
"C:\Users\Bhagyashree\Documents\DoubleClick.html");
driver.manage().window().maximize();
String txtPara = driver.findElement(By.id("txtPara"))
.getAttribute("id");
driver.executeScript("document.getElementById('" + txtPara
+ "').style.display='block';");
}
}
Next, we will move on to EventFiringWebDriver.