URL Loading in Selenium Webdriver: All about get() and navigate() | Make Selenium Easy
Hello Folks,
We have learnt how to open Chrome, Firefox, IE and Edge browser. It is very first step before we start automating test cases through Selenium Webdriver.
Second step is to load URL of AUT(Application Under Test). So in this post we will learn below topics:
- Opening a URL through get() method.
- Opening a URL using to() method.
- Difference between get() and navigate()
Opening a URL through get() method:
Most common way of loading a URL in browser in Selenium is using get() method.
Syntax: void get(java.lang.String url)
Points to know about get():
- get() method is declared in WebDriver interface.
- get() method is defined in RemoteWebDriver class.
- get() method returns void.
- get() method takes an argument of type String which is your AUT URL.
- It loads a new web page in the current browser window.
- It is done using an HTTP GET operation.
- It blocks the execution of next statement till web page is loaded completely.
- If you try to open a URL without http or https( e.g. driver.get(“www.facebook.com”);) , get() method will throw “org.openqa.selenium.WebDriverException“. It will also give error message as “Cannot navigate to invalid URL”.
- You can load a URL as http://facebook.com. It will not give any exception. Normally also it will load Facebook page. This was asked to me in interview.
- If you pass a wrong URL e.g. https://faceok.com , it will not give any page load time exception. Browser will display “ERR_CONNECTION_REFUSED” and program will be terminated. It was asked to me in interview.
- There is default page load time which is 300 seconds. It will throw org.openqa.selenium.TimeoutException once default page load time reaches and page is still loading.
- If a URL is being loaded and you stop manually loading of URL by clicking on “Stop Loading Page” icon what will happen?? Nothing will happen and get() method will hold the control till default page load timeout reaches. You can’t make get() method fool. It is also an interview question.
- We can set page load time using pageLoadTimeout(long arg0, TimeUnit arg2) method. Syntax is as below:
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
14. You can not go back and forward using get() method. You can use get() multiple times in a program.
Complete JAVA Program:
package MakeSeleniumEasy;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoadURL {
public static void main(String[] args) {
System.out.println("Execution Starts");
// Setting chrome driver property and opening chrome browser
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
WebDriver driver= new ChromeDriver();
System.out.println("Browser opened.");
// loading URL
driver.get("http://www.makeseleniumeasy.com");
System.out.println("makeseleniumeasy loaded");
driver.get("http://www.facebook.com");
System.out.println("Facebook loaded.");
// Setting default page load time. It will be applicable from next line not for above code
driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.SECONDS);
driver.get("http://www.learn-automation.com");
}
}
Output:
In above program, I am setting page load time once Facebook is loaded. New page load time will be applicable from next statement. Remember execution of JAVA programs goes line by line. I am setting less page load time to get exception.
- We can load a URL using to() method of Navigation inner interface of WebDriver interface.
- It is similar to get() method and to() method calls get() method internally.
public void to(String url)
{
RemoteWebDriver.this.get(url);
}
- Syntax: driver.navigate().to(“http://www.makeseleniumeasy.com”);
- navigate() is abstract method which allows the driver to access the browser’s history and to navigate to a given URL. It returns a WebDriver.Navigation that allows the selection of what to do next.
- It behaves in same way as get() method does.
- Using navigate() method we can go back and forward using back() and forward() methods respectively if any session presents.
- forward() method moves a single “item” forward in the browser’s history. Does nothing if we are on the latest page viewed.
- back() method moves back a single “item” in the browser’s history.
- Using navigate() method we can refresh the page by calling refresh() method on it.
- We can pass URL class object as well to to() method to open a URL. to() method is overloaded with arguments type of String and URL(java.net.URL).
- Syntax: driver.navigate().to(new URL(“http://www.google.com”));
Complete JAVA Program:
package MakeSeleniumEasy;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoadURLUsingTo {
public static void main(String[] args) {
System.out.println("Execution Starts");
// Setting chrome driver property and opening chrome browser
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
WebDriver driver= new ChromeDriver();
System.out.println("Browser opened.");
// loading URL
driver.navigate().to("http://www.makeseleniumeasy.com");
System.out.println("makeseleniumeasy loaded");
driver.get("http://www.facebook.com");
System.out.println("Facebook loaded.");
driver.navigate().back();
System.out.println("Back: "+driver.getCurrentUrl());
driver.navigate().forward();
System.out.println("Forward :"+driver.getCurrentUrl());
driver.navigate().refresh();
System.out.println("Refresh: "+driver.getCurrentUrl());
}
}
Output:
Difference between get() and navigate():
- get() method does not allow driver to access the browser’s history while navigate() allows the driver to access the browser’s history and to navigate to a given URL.
- You can not navigate back and forward using get() while same can be achieved through navigate().
- There is no specific method for refresh a web page in get() while navigate() provides a method named refresh(). You can refresh a page using get() also by calling get() method again. It was interview question. How you will refresh a page using get() method.
That’s it guys. I hope you must have learnt some new things. If you have any doubt or suggestions, feel free to contact me.
Thank you.
#HappyLearning
My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning