Page Navigation in Selenium C#

In previous tutorial we have learned about Creating Automation Base Class in Selenium C# , Today in this tutorial we will discuss about Page Navigation and use of Close and Quit methods in Selenium C#.

Page Navigation in Selenium C#

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, you will find these navigation commands in all browsers.  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 you will see the list of available navigation commands that you can use. Their are mainly four Navigation commands.

  • GoToUrl
  • Back
  • Forward
  • Refresh

GoToUrl() in Selenium C#

It is used for navigate to any particular page or website, it takes url in the form of string as parameter. We will use GoToUrl navigation. First of all let me tell you what is meant by this statement-

IWebDriver driver = new ChromeDriver ()

When we type this statement then mainly two events occur.

  • It is going to create the instance of that driver.
  • It is going to launch the corresponding browser.

So in the above statement or example first it will create the instance for “Chrome Driver” and second it will launch the “Chrome Browser” for us.

So once our browser is here we might open the website in it, then in order to open the website inside the browser we will use the “INavigation” interface. Inside this interface we have method called “GoToUrl()” that will open the required website inside the browser.

Step 1:

In Visual studio specify the configuration for the website first.

Syntax:

<add Key="Website" Value="url of the website"/>

In above syntax in the place of “url of the website” we have to give the url of the particular website what we want to open in our browser.

Example:

<add Key="Website" Value="http://localhost:5001/"/>

In the above example the given url is of bugzilla website so it will open the bugzilla website. We will add the same key inside our AppConfigKeys class.

public class AppConfigKeys
{
public const string Browser="Browser";
public const string Username="Username";
public const string Password="Password";
public const string Website="Website";
}

We are going to add one more method inside our interface which is going to give us the website url.

public interface IConfig 
{ 
string GetUsername();
string GetPassword();
string GetWebsite();
}

This is going to read the website url from the App.Config file, now we have to implement this method inside AppConfigReader.

public string GetWebsite () 
{
return ConfigurationManager.AppSettings.Get(AppConfigKeys.Website);
}

So here we will use the same statement that is return ConfigurationManager.AppSettings.Get and the key will be website. So once when it’s done we will add a directory inside the Test Script and name it as PageNavigation, inside this we will add a class called TestPageNavigation.

[TestClass]
public class TestPageNavigation
{ 
[TestMethod]
public void OpenPage ()
{ 
INavigation page = ObjectRepository.Driver.Navigate();
Page.GoToUrl(Repository.Config.GetWebsite());
}
}

We will make this class public and also use the attribute called [TestClass], inside this we create a method public void OpenPage () and the attribute which we need to use is [TestMethod], inside this method we will use our ObjectRepository.Driver which is a property and this property will automatically Initialize as soon as the base class will called.

If we look at the IWebDriver Interface so we have a method called Navigate() which is going to return us the interface type of INavigation so here we will use Driver.Navigate as soon as we  type dot (.) it will list down all the methods present inside the IWebDriver Interface,

and as we know that the Navigate method is going to return us the variable of type INavigation interface so here we will use INavigation page=  and inside this interface there is a method called GoToUrl () here we have two overloaded method one will take uri as parameter and the other one will take string as a parameter, so we are going to use string one because we are reading in the form of our App.Config file.

Step 2:

Write Page.GoToUrl(ObjectRepository.Config.GetWebsite()); put the break point on this line and build the solution and run the script in debug mode so it will launch the Chrome browser for us. If we do a step over and run this again it will launch the Chrome browser and open the website of bugzilla inside our browser. We will now click on Continue.

Now these two statements can be written in the form of single statement this will also do the same thing.

ObjectRepository.Driver.Navigate().GoToUrl(ObjectRepository.Config.GetWebsite());

This is the shorthand notation of calling that two statements.

Step 3:

Once this is done inside our ComponentHelper directory we will add a wrapper class called NavigationHelper.

This class will take care of any operations related to the navigation. Here we will have different methods to handle the different types of navigation.

public class NavigationHelper 
{
public static void NavigateToUrl(string Url)
{
ObjectRepository.Driver.Navigate().GoToUrl(Url);
}
}

Here we will create public static void NavigateToUrl and specify the Url in the form of string in parameter. Inside this we are calling ObjectRepository just copy paste the single statement code that we have discussed about and the (ObjectRepository.Config.GetWebsite()) will be replaced by the parameter which we are supplying inside this method.

[TestClass]
public class TestPageNavigation
{
[TestMethod]
public void OpenPage()
 {
NavigationHelper.NavigateToUrl(ObjectRepository.Config.GetWebsite());
 }
}

So here  we are doing the same work with wrapper class what we were doing with our other two statements. With the help of this ComponentHelper we will group our all actions inside the class so this NavigationHelper will contain all the operations related to the Navigation.

Once this is done we will see here that all the browser sessions are still active even though our Testcase is pass, The reason behind why this is happening is that we are not stopping the browser through our script so that’s why our session are left as it is.

Back Browser in selenium C#

It is used to navigate us on the previous page of the browser history. Just like pressing the back button of the browser, it does not take any parameter.

For example: if we are on 4th page of any website and we want to go back to the previous page that is page 3 so we will use this command. it will redirect us to the previous page.

driver.Navigate().Back();

Forward Browser in Selenium C#

It takes you forward by one page means if you have any history in forward direction it will redirect us to that page, just like clicking on the forward button of the browser. this command also does not take any parameter.

Example:

driver.Navigate().Forward();

Refresh Browser in Selenium C#

It is used to refresh the page of the browser, just like we click on the refresh button of browser or press F5. this command also does not take any parameter.

Example:

driver.Navigate().Refresh();

Close and Quit methods in Selenium C#

So moving on we are going to discuss about close() and Quit(). These are the two methods which are used to close the browser and stop the driver.

If we go inside the IWebDriver Interface so their we can see the two methods close() and Quit(), these two methods will going to help us to stop our browser sessions.

Inside our BaseClass again we will create one more method.

[AssemblyCleanup]
public static void TearDown()
{
if(ObjectRepository.Driver != null)
 {
 ObjectRepository.Driver.Close();
 ObjectRepository.Driver.Quit();
 }
}

As we wanted to execute this method in the last so we are using attribute [AssemblyCleanup], now here we will check for the null. Here in the if condition with ObjectRepository we are putting the null check if it is not and condition is true then the ObjectRepository.Driver.Close(); and similarly ObjectRepository.Driver.Quit(); methods will be execute.

Now if we run the same script again so it will launch the Chrome browser first for us and open the webpage after that ideally it should close and Quit the driver for us.

If we comment out the close method statement and again run the script so the Quit method will also do the same work, so how can we understand what the actual difference between the Close() and Quit () methods.

Difference between Close() and Quit () methods

Suppose if we have three browser window and our current driver property is pointing to the third one then

Close():

So when we call the close method in that case it is going to close the browser window which is currently pointed by the driver property, means it will close the last browser window but still the other two browser window will remain active.

Quit():

But in case of Quit it is going to close all the Browser window it doesn’t matter if they’re single or multiple it will close all of them and also stop the WebDriver.

So that’s all about the Page Navigation and Close and Quit methods in Selenium C#. I hope you all have understand it properly.