Navigation Commands In Selenium WebDriver: In our previous posts we have discussed so many articles on different topics like Different locators in Selenium WebDriver, How to setup Eclipse project, and How to Run your first Selenium program like this. In this post, we are going to discuss the different navigation commands.
After successfully run the first selenium program, now we can proceed to learn about the various navigational commands. So in this post, we have the plan to share the different navigation common which we are using a regular basis in our automation testing script. A browser has various options for navigation, like:
- Open a Web Page URL
- Navigate to another web page by Clicking any element
- Go Back
- Go forward
- Refresh a web page
Let us go through each of the commands one by one and learn how to do with the help of using Selenium WebDriver.
Different Navigation Commands In Selenium
Navigate.To(URL):
In our previous posts, we have seen that with the help of using the get() method, we can able to open a web page URL. But in the Navigation interface also providing one method and by using that method also we able to open a web page. To open a web page using the Navigation method you have to use to() method.
package com.selenium.practice.basic; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Navigate_To_PGM { public static void main(String[] args) { WebDriver driver; System.setProperty("webdriver.gecko.driver", "Path Of the Borwser driver file"); driver = new FirefoxDriver(); driver.navigate().to("https://www.google.com"); driver.close(); } }
Navigate.To(String):
This method will load a new web page on the current browser window. All the process is done with the help of the Get operation, and this method will block all other option until the load process is complete.
Note: The URL should be a full URL, for example, if you are trying to open Google Home page then you have to mention the full URL.
- Valid: https://www.google.com
- Invalid: www.google.com
package com.selenium.practice.basic; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Navigate_To_String_PGM { public static void main(String[] args) { WebDriver driver; String url="https://www.google.com"; System.setProperty("webdriver.gecko.driver", "Path of Browser Driver File"); driver = new FirefoxDriver(); driver.navigate().to(url); driver.close(); } }
Navigate.Back():
If you have redirected to other pages (Inner page) from a Home page and you have completed your required operation on the redirected page (Inner page), and again you want to go back to the home page. Then, in that case, you can use the back() method of the Navigate interface.
This method moves back a single page ion the web history, and if you are on the home page, then this method will not perform any action.
Navigate.Forward():
Like the back() method we can use the forward() method when our requirement is to move one page forward in the web history. It will not perform any operation if you are in the last or latest page of web history.
Navigate.Refresh():
If your requirement is reloading the complete page then in navigate interface we have a method called refresh(). You can use this method to refresh the complete web page.
package com.selenium.practice.basic; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Navigate_Back_Forward_Pgm { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.gecko.driver", "F:\\Software_Testingo_Practice\\Selenium_Practice\\Selenium_Practice_PGM\\Browser_Driver\\geckodriver.exe"); driver = new FirefoxDriver(); driver.navigate().to("https://www.google.com"); driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS); driver.findElement(By.linkText("Gmail")).click(); Thread.sleep(5000); System.out.println("Gmail Page Sucessfully open"); driver.navigate().back(); Thread.sleep(5000); System.out.println("Back to Google Home Page"); driver.navigate().forward(); Thread.sleep(5000); System.out.println("Gmail Page Sucessfully open By Forward"); driver.navigate().refresh(); Thread.sleep(5000); driver.close(); } }
Add a Link of Difference between Get() and TO() method
Ref: Article
Leave a Reply