- executeScript(String, Object...)
- executeAsyncScript(String, Object...)
Here is a simple test,
@Test
public void testJavascript() throws InterruptedException {
// wait 10 seconds if web element is not present
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("alert('hello world');");
TimeUnit.SECONDS.sleep(4);
driver.switchTo().alert().accept();
WebElement continents = (WebElement) jsExecutor
.executeScript("return document.getElementById('continents')");
assertNotNull(continents);
String selectedContinent = jsExecutor
.executeScript("return document.getElementById('continents').value")
.toString();
assertEquals("Asia", selectedContinent);
}
public void testJavascript() throws InterruptedException {
// wait 10 seconds if web element is not present
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("alert('hello world');");
TimeUnit.SECONDS.sleep(4);
driver.switchTo().alert().accept();
WebElement continents = (WebElement) jsExecutor
.executeScript("return document.getElementById('continents')");
assertNotNull(continents);
String selectedContinent = jsExecutor
.executeScript("return document.getElementById('continents').value")
.toString();
assertEquals("Asia", selectedContinent);
}
No comments:
Post a Comment