Open URL through Selenium without using get() and navigate() functions - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Friday, 30 March 2018

Open URL through Selenium without using get() and navigate() functions

package mypackage;



import java.io.IOException;

import java.util.concurrent.TimeUnit;



import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.FluentWait;



import com.google.common.base.Function;



public class myclass {

 

 public static void main(String[] args) {

  System.out.println("Execution Starts");

  WebDriver driver= new FirefoxDriver();

  System.out.println("Browser opened.");

  // We need to downcast WebDriver reference varaible to use JavascriptExecutor methods

  JavascriptExecutor js= (JavascriptExecutor) driver;

  String url = "https://www.google.com";

  String script = "window.location = \'"+url+"\'";

  js.executeScript(script);

  System.out.println(driver.getCurrentUrl());

  driver.quit();

  }

 }





Note:



It also keeps current browser session history and you can navigate back and forward

No comments:

Post a Comment