Browser and navigation commands in selenium web driver are the most common and useful commands at the initial stage of automation. In this selenium webdriver tutorial, I am going to cover all the important browser and navigation commands in this selenium webdriver tutorial.
What is browser command in selenium webdriver?
Browser commands are used to perform browser operations like open browser, close browser. Webdriver interface provides methods to perform these operations in automation.
If you don’t know about Webdriver Interface read about the Webdriver interface. Webdriver interface provides lots of methods to perform the operation if you want to see all the methods just create an object on any browser(firefox or chrome),
Example to create webdriver object for chrome browser.
Webdriver driver = new ChromeDriver();
Now enter the driver and a dot(.) you can see all the methods.
Browser commands in selenium webdriver?
The followings are important browser commands in the selenium webdriver.
- get(String args0): void
- getTitle():string
- getCurrentUrl() :String
- getPageSourceCode():String
- getWindowHandle():String
- getWindowHandles():Set<String>
- getClass():Class<? extends WebDriver>
How to load URL in selenium webdriver?
get(String args0): void
method will open reload the URL in the current browser. It will take the String as a parameter and return nothing. We can pass the weburl as a string into get(“url”) method.
Example to load URL in selenium webdriver
driver.get("codebun.com");
This statement will load the URL “codebun.com” on the browser.
How to Get Title of a page in selenium webdriver?
getTitle():string
It will return the title of the current page as a string. by default it will check the <title></title> tag over the current page and return the text of the title tag.
String tittle = driver.getTitle();
Above statement will return a String(title of the current page).
How to Get Current URL in selenium webdriver?
getCurrentUrl():String
method returns the string as the current URL which is opened in the browser and returns a String value. If you have multiple windows opened during the automation it will return only the current page URL.
Example get current URL in Selenium
String currentURL = driver.getCurrentUrl();
How to Get Page Source Code in selenium webdriver?
getPageSourceCode():String
method will return the source code of the current page and return a string value. It will return the complete source code of the page. it will be used to verify and specific text or elements over the page.
Example to get source code of the current page in selenium
String pageSourceCode = driver.getPageSource();
How to handle windows in selenium webdriver?
getWindowHandle():String
Method will return a string of alphanumeric window handle from the focused browser.
Example to handle browser windows in selenium
String windows = driver.getWindowHandle();
Get set of Windows Handles in selenium webdriver?
getWindowHandles():Set<String>
This method will return a set of windows handle from the focused browser.
Example to get set of windows in selenium
Set<String>s1 = driver.getWindowHandles();
How to Get Class of current object in selenium webdriver?
getClass():Class<? extends WebDriver>
This method will return the class of the current object. For example, If you are using the object of chrome driver(driver) then it will return “class org.openqa.selenium.chrome.ChromeDriver”
Example to Get Class of current object in selenium
Class<? extends WebDriver>classOfTheObject = driver.getClass();
Browser Commands in selenium webdriver with example
In this below automation example, we will use all the browser commands in the selenium webdriver that we have discussed above. this automation testcase will follow the following steps.
- Load URL in the current browser.
- Get the title of the current page.
- Get current URL.
- Get the Source code of the current page.
- Get focused window.
- Get a set of focused windows.
- Get class of the current object.
import java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Main { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe"); // Open Browser WebDriver driver = new ChromeDriver(); // Load URL in the current browser driver.get("http://www.seleniumeasy.com/test/"); // Get Tittle of the current page. String tittle = driver.getTitle(); System.out.println("Page tittle: "+tittle); // Get current URL.... String currentURL = driver.getCurrentUrl(); System.out.println("Current URL :"+currentURL); //Get Source code of the current page String pageSourceCode = driver.getPageSource(); //System.out.println(pageSourceCode); //get focused window String window = driver.getWindowHandle(); System.out.println("Window : "+window); //get set of focused windows Set<String> windows = driver.getWindowHandles(); System.out.println("Set of windows : "+windows); //get class of the current object... Class<? extends WebDriver> classOfTheObject = driver.getClass(); System.out.println("class of the current object :"+classOfTheObject); } }
Navigation Commands in selenium webdriver?
Navigation commands are used to perform browser navigation operations like back, forward, refresh, etc. Below is the list of Navigation commands in the selenium webdriver.
To find the list of all methods you just need to write code in your eclipse driver.navigate(). it will display all the methods of navigation interface.
How to Navigate URL in selenium webdriver?
to(String args0): void
This method loads the Application URL in the current windows of the browser. It takes a String parameter as a URL(complete URL with HTTP protocol) for example we need to load a URL in the current window.
driver.navigate().to("https://codebun.com/");
Or
String URL = "https://codebun.com/"; driver.navigate().to(URL);
How to perform back operation is selenium webdriver?
back():void
This method performs the back operation at the current browser window It takes a step back. Like you press back button in the browser.
Syntax :
driver.navigate().back();
Example:
driver.navigate().to("https://codebun.com/category/java/"); driver.navigate().back();
How to perform Forward operation in selenium webdriver?
Forward():void
This method performs a forward operation at the current browser window It takes a step forward. Like you press the forward button in the browser.
Syntax:
driver.navigate().forward();
Example:
driver.navigate().to("https://codebun.com/category/java/"); driver.navigate().back(); driver.navigate().forward();
How to Refresh the current page in the selenium webdriver?
refresh():void
This method refreshes the current URL of the current window of the browser.
Syntax:
driver.navigate().refresh();
Example:
driver.navigate().to("https://codebun.com/category/java/"); driver.navigate().refresh();
Navigation commands in selenium webdriver with example
In the below automation example, Is using all the important navigation commands in the selenium webdriver. following are the steps to automate this scenario.
- Open Browser.
- Navigate to the application URL of the home page.
- Click at any link.
- Come to again on home page by back().
- Come to link URL by forward().
- Refresh current URL.
import java.util.Set; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Main { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe"); // Open Browser WebDriver driver = new ChromeDriver(); // Load URL in the current browser driver.navigate().to("https://codebun.com/category/java/"); driver.navigate().back(); driver.navigate().forward(); driver.navigate().refresh(); } }