JavaScriptExecutor is an interface which provides mechanism to execute Javascript through selenium driver.
It provides two methods to run JavaScript in the context of the currently selected frame or window:
1) “executescript” &
2) "executeAsyncScript"
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.”
Package:-
import org.openqa.selenium.JavascriptExecutor;
Syntax:-
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);
Handling Alert Pop window in selenium:
JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("alert('alertwinname');");
Clicking a button in Selenium WebDriver using JavaScript
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
Refreshing browser window using Javascript.
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.executeScript("history.go(0)");
To get innertext of the entire webpage in Selenium
JavascriptExecutor js = (JavascriptExecutor)driver;
string iText = js.executeScript("return document.documentElement.innerText;").toString();
To get the Title of our webpage
JavascriptExecutor js = (JavascriptExecutor)driver;
string Title = js.executeScript("return document.title;").toString();
Performing Scroll:
JavascriptExecutor js = (JavascriptExecutor)driver;
//Vertical scroll - down by 250 pixels
js.executeScript("window.scrollBy(0,250)");
Note:- for scrolling till the bottom of the page use:
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
Clicking on a SubMenu which is only visible after mouse hover on Menu
JavascriptExecutor js = (JavascriptExecutor)driver;
//Hover on Test Menu on the MenuBar
js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");
Navigate to different page using Javascript
JavascriptExecutor js = (JavascriptExecutor)driver;
//Navigate to new Page
js.executeScript("window.location = 'www.google.com'");
It provides two methods to run JavaScript in the context of the currently selected frame or window:
1) “executescript” &
2) "executeAsyncScript"
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.”
Package:-
import org.openqa.selenium.JavascriptExecutor;
Syntax:-
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);
Handling Alert Pop window in selenium:
JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("alert('alertwinname');");
Clicking a button in Selenium WebDriver using JavaScript
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
Refreshing browser window using Javascript.
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.executeScript("history.go(0)");
To get innertext of the entire webpage in Selenium
JavascriptExecutor js = (JavascriptExecutor)driver;
string iText = js.executeScript("return document.documentElement.innerText;").toString();
To get the Title of our webpage
JavascriptExecutor js = (JavascriptExecutor)driver;
string Title = js.executeScript("return document.title;").toString();
Performing Scroll:
JavascriptExecutor js = (JavascriptExecutor)driver;
//Vertical scroll - down by 250 pixels
js.executeScript("window.scrollBy(0,250)");
Note:- for scrolling till the bottom of the page use:
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
Clicking on a SubMenu which is only visible after mouse hover on Menu
JavascriptExecutor js = (JavascriptExecutor)driver;
//Hover on Test Menu on the MenuBar
js.executeScript("$('ul.menus.menu-secondary.sf-js-enabled.sub-menu li').hover()");
Navigate to different page using Javascript
JavascriptExecutor js = (JavascriptExecutor)driver;
//Navigate to new Page
js.executeScript("window.location = 'www.google.com'");