Lets understand how we can do different types of navigation in a browser using the Browser Navigation Commands of IWebDriver implementation. IWebDriver interface is implemented by all the supported browser classes, hence you will find these navigation commands in all browsers. We are going to discuss about various navigation commands that we would be using in our day to day automation testing. The INavigation interface exposes the ability to move backwards and forwards in the browser’s history.
To access the navigation’s method, just type IWebDriver.navigate().. If you are using Visual studio or any IDE which supports IntelliSense, you will see the list of available navigation commands that you can use. Below is the image showing all the commands
We have four Navigation commands
- GoToUrl
- Back
- Forward
- Refresh
GoToUrl Command
This command is used to navigate to a particular page. Signature of this command is
void INavigation.GoToUrl(String url) : This command takes a String value of the URL as input parameter and returns void. Once this statement is executed browser navigates to the new page specified by the URL parameter.
Command - driver.Navigate().GoToUrl(appUrl);
Where appUrl is the website address to load. It is best to use a fully qualified URL.
Usage:
driver.Navigate().GoToUrl("https://toolsqa.com");
Back Command
Back command is used to navigate to the previous page in the browser history. This is exactly similar to what happens when you click on the back button in your browser.
void INavigation.Back() : This command does not take any parameter.
Takes you back by one page on the browser’s history.
Usage:
driver.Navigate().Back();
Forward Command
Forward command is used to navigate to the next page in the browser history. This exactly similar to what happens when you click on the forward button in your browser, if you have some history in forward direction
void INavigation.Forward() : This command does not take any parameter.
Takes you forward by one page on the browser’s history.
Usage:
driver.Navigate().Forward();
Refresh Command
This command refreshes the page as if you have pressed F5 on the browser. void INavigation.Refresh() : This command does not take any parameter
Usage:
driver.Navigate().Refresh();
Practice Exercise
- Launch new Browser
- Open DemoQA.com website
- Click on Registration link using “driver.findElement(By.xpath(“.//[@id=’menu-item-374′]/a”)).click();“*
- Come back to Home page (Use ‘Back’ command)
- Again go back to Registration page (This time use ‘Forward’ command)
- Again come back to Home page (This time use ‘To’ command)
- Refresh the Browser (Use ‘Refresh’ command)
- Close the Browser
Solution
static void Main(string[] args)
{
IWebDriver driver = new InternetExplorerDriver(@"C:\Users\abc\Desktop\Server");
driver.Navigate().GoToUrl("https://demoqa.com");
driver.FindElement(By.XPath(".//*[@id='menu-item-374']/a")).Click();
driver.Navigate().Back();
driver.Navigate().Forward();
driver.Navigate().Refresh();
driver.Close();
}