Sameera De Silva
2 min readOct 10, 2019

Using Webdriver JavascriptExecutor navigate to a URL and navigate forward and backward and refresh the page .

What is JavascriptExecutor?

It’s a Selenium Interface which directly lets you Interact with HTML DOM of the web page.It is being implemented by all the following classes:

FirefoxDriver

ChromeDriver

InternetExplorerDriver

EdgeDriver

OperaDriver

SafariDriver

RemoteWebDriver

EventFiringWebDriver

it does so by executing JavaScript expressions using Following Syntax :

executeScript():

This method executes JavaScript in the context of the currently selected frame or window.

JavascriptExecutor provides a way to automate a user interaction even when page is not essentially loaded completely or elements are placed in a way that the direct interaction is blocked.

While you execute your Selenium script at times because of cross domain policies browsers enforce your script execution may fail unexpectedly

This however is also the disadvantage too, if you want to automate a web page as if a real user experience.

That said, although it is a really powerful option,

but we should try not to use JavaScript Executor unless there is no standard way of doing it via Selenium.

let’s see it in action , so here I use Webddriver JavascriptExecutor navigate to a URL and navigate forward and backward and refresh the page .

package com.webdriver.blogs;import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class JavaScriptSamplesInWebdriver {

WebDriver driver;
@BeforeClass()
public void setUp() {
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\Jar_files\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().window().maximize();

}
@Test(priority=1,enabled=true,description="Navigate to website using java script and go back and forward")
public void GoToUrlAndNavigateBackAndForward() throws InterruptedException {
String url="https://bitbucket.org/";
//Define a js object with type of JavascriptExecutor class and here the TypeCasting is applied
JavascriptExecutor js = (JavascriptExecutor) driver;
//The url is parameterized and \ \ signs are optional and This is similar to driver.get("https://www.bbc.com/");
js.executeScript("window.location = \'"+url+"\'");
//Without parameterizing the url
js.executeScript("window.location = 'https://www.bbc.com/'");
//To navigate back (-1)--> back This is similar to driver.navigate().back();
js.executeScript("window.history.go(-1)");
Thread.sleep(6000);
//To go forward Forward(-1)--> front. driver.navigate().forward();
js.executeScript("window.history.forward(-1)");
Thread.sleep(6000);
//Refresh the page This is similar to driver.navigate().refresh();
js.executeScript("history.go(0);");
}
}

Please see the free Video URL — https://youtu.be/H1INiSQlnhM

Please follow the Author — https://www.linkedin.com/in/sameera-de-silva-11247922/

No responses yet