What is JavascriptExecutor?
JavascriptExecutor is an interface provided by Selenium which was implemented by all known WebDriver classes ( AndroidDriver, AndroidWebDriver, ChromeDriver, EventFiringWebDriver, FirefoxDriver, HtmlUnitDriver, InternetExplorerDriver, IPhoneDriver, IPhoneSimulatorDriver, RemoteWebDriver, SafariDriver). Using JavascriptExecutor we can inject java script into browsers i.e we can execute the javascript.
What's the use of it?
Sometimes we encounter with scenarios(listed below) where the existing selenium api will not do the job as expected. To handle those scenarios selenium is giving us option to write down our java scripts to perform browser events. Using JavascriptExecutor we can execute java script.
Syntax:
Sample Scenarios:
To get domain name
Click on a webelement
Refresh browser
Get title of the page
Perform scroll
To scroll to end of the page
Mousehover
Navigate to a link
To set JavaScript variable value
Working Exmaple
JavascriptExecutor is an interface provided by Selenium which was implemented by all known WebDriver classes ( AndroidDriver, AndroidWebDriver, ChromeDriver, EventFiringWebDriver, FirefoxDriver, HtmlUnitDriver, InternetExplorerDriver, IPhoneDriver, IPhoneSimulatorDriver, RemoteWebDriver, SafariDriver). Using JavascriptExecutor we can inject java script into browsers i.e we can execute the javascript.
What's the use of it?
Sometimes we encounter with scenarios(listed below) where the existing selenium api will not do the job as expected. To handle those scenarios selenium is giving us option to write down our java scripts to perform browser events. Using JavascriptExecutor we can execute java script.
Syntax:
JavascriptExecutor javaScript = (JavascriptExecutor) driver; javaScript.executeScript(Script,Arguments);
Sample Scenarios:
To get domain name
JavascriptExecutor javaScript=(JavascriptExecutor) driver; String domainName =(String) javaScript.executeScript("return document.domain");To generate an alert
JavascriptExecutor javaScript= (JavascriptExecutor)driver; JavaScript.executeScript("alert('hello world');");
Click on a webelement
JavascriptExecutor javaScript= (JavascriptExecutor)driver; javaScript.executeScript("arguments[0].click();", element);
Refresh browser
JavascriptExecutor javaScript= (JavascriptExecutor)driver; driver.executeScript("history.go(0)");
Get title of the page
JavascriptExecutor javaScript= (JavascriptExecutor)driver; string sText = javaScript.executeScript("return document.title;").toString();
Perform scroll
JavascriptExecutor javaScript =(JavascriptExecutor)driver; //Verticalscroll - down by 250 pixels javaScript.executeScript("window.scrollBy(0,250)");
To scroll to end of the page
javaScript.executeScript("window.scrollBy(0,document.body.scrollHeight)");
Mousehover
JavascriptExecutor javaScript =(JavascriptExecutor)driver; //Hover on element whose id is id javaScript.executeScript("$(‘#id').hover()");
Navigate to a link
JavascriptExecutor javaScript= (JavascriptExecutor)driver; //Navigate to a link javaScript.executeScript("window.location = 'https://www.flipkart.com");
To set JavaScript variable value
JavascriptExecutor javaScript = (JavascriptExecutor) driver; javaScript .executeScript("window.myVar = arguments[0];", "practiceQa"); String myvar = (String) javaScript.executeScript("return window.myVar;");
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.Test; public class JavascriptExecutorExample { @Test public void testScroll() throws InterruptedException { WebDriver driver = new FirefoxDriver(); //Launch flipkart driver.get("http://www.flipkart.com"); //Search for TV WebElement searchContainer = driver.findElement(By.id("fk-top-search-box")); searchContainer.sendKeys("TV"); //Click on searchButton driver.findElement(By.className("search-bar-submit")).click(); //Scroll down the webpage by 2500 pixels JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("scrollBy(0, 2000)"); boolean noMoreResultsFound=false; while(!noMoreResultsFound){ try{ (new WebDriverWait(driver, 3)).until (ExpectedConditions.presenceOfElementLocated(By.id("no-more-results"))); noMoreResultsFound = true; }catch(Exception e){ WebElement viewMore = driver.findElement(By.id("show-more-results")); if(!viewMore.isDisplayed()){ js.executeScript("scrollBy(0, 2000)"); }else{ ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", viewMore); Thread.sleep(1000); viewMore.click(); } } } } }