Browser Handling in Selenium C#

In previous tutorial we have discussed about Page Navigation in Selenium C# , today in this tutorial we will discuss about Browser Handling in Selenium C#, and what are ChromeOptions, InternetExplorerOptions and FirefoxProfile classes.

Browser Handling in Selenium C#

First we will discuss what is Browser Handling and what is ChromeOptions and InternetExplorerOptions and FirefoxProfile classes, theses are the classes which help us to launch the browser with additional Settings, and using our browser with additional Settings is called browser handling in Selenium C#.

Suppose we run our script during the execution of this script it will launch the Chrome browser but the browser will be launch with the default settings, so it is possible that for testing purpose we might need to enable certain Settings.

Such as if we want to enable the JavaScript or want to use XML extension so these things can be done easily with these classes.

So we will use ChromeOptions for Chrome browser, InternetExplorerOptions for Internet Explorer and FirefoxProfile for Firefox browser.

Chrome Options in Selenium C#

Inside ChromeOptions Class we have two methods that are “AddArgument” and the “AddExtension”.

AddArgument:

AddArgument used to supply the command line argument during the launching of browser and AddExtension is used to add the Extension inside the browser.  So here inside our BaseClass we are going to create a static method.

public class BaseClass
{
public static ChromeOptions GetChromeOptions()
 {
ChromeOptions option= new ChromeOptions();
option.AddArgument("start-maximized");
return option;
 }
}

The reason why we made it static because we will call this method inside the static context and we want this method should return us the object of ChromeOptions.

After that we will create the object of this class and if we go to the class definition of ChromeOptions class we can can see here that their are lots of properties and methods to help us to enable those Settings.

As we saw in our previous run that the Chrome browser is not maximise so here we will use one argument to maximise to that browser, In Options.AddArgument and (” “) here we need to supply that argument.

(“start-maximized”)  this is the argument for starting the Chrome browser in maximise mode and after that we will return that object.

Now once this is done we need to use GetChromeOptions(); this object, inside the Chrome driver. So here we are going to use our GetChromeOptions() method to return us the object of ChromeOptions and during the instantiate, it will take that Settings.

Now we will run our script in debug mode as we can see here with that Settings enable Chrome browser has now opened in maximise mode.

AddExtension:

Moving on if we go to the new tab and click on the Apps so it is display us the Apps which is present inside the browser instance.

Currently we don’t have any app. But their is a app called postman we are going to use that app while this browser is launching.

We will just click on Continue. So inside our BaseClass we will add one more Setting that is AddExtension.

public class BaseClass
{
public static ChromeOptions GetChromeOptions()
 { 
ChromeOptions option= new ChromeOptions(); option.AddArgument("start-maximized"); 
option.AddExtension("C:\users\nida.khan\Desktop\Cucumber\extension_3_0_12.crx");
return option;
 }
}

In AddExtension here inside this bracket (” “) we need to provide the location of extension or app file so (“C:\users\nida.khan\Desktop\Cucumber\extension_3_0_12.crx”); this is the location where i have my postman extension file.

Again we run the script in debug mode now this time when we go the app for Chrome browser it is going to list us postman app which is initialized using this particular method.

If we opened the new tab and click on apps so now we can see here the postman app inside our browser instance this is coming because of our Settings for AddExtension.

InternetExplorerOptions in Selenium C#

Now moving on we will discuss about InternetExplorerOptions class, which will launch the Internet Explorer with some additional Settings. In this class we will see a Setting that is IntroduceInstabilityByIgnoringProtectedModeSettings.

IntroduceInstabilityByIgnoringProtectedModeSettings

Basically this setting is used to disable a protection mode sometime when we launch the Internet Explorer browser for testing we might get an error so using this Setting we can avoid that . Again we will create one more method.

public class BaseClass 
{
private static InternetExplorerOptions GetIEOptions()
 {
InternetExplorerOptions options= new InternetExplorerOptions ();
options.IntroduceInstabilityByIgnoringProtectedModeSettings= true;
options.EnsureCleanSession=true;
return options;
 }
}

Here we have made private static method InternetExplorerOptions GetIEOptions() and inside this again we will create the object of InternetExplorerOptions as options= new InternetExplorerOptions () if we go to the class definition of this again it has lot of properties and methods for Setting.

So here we will use a property that is IntroduceInstabilityByIgnoringProtectedModeSettings and we will make it true and add one more property that is options.EnsureCleanSession , so this particular property will make sure that whenever it is going to launch the Internet Explorer that is a fresh session means before launching it, it will clear all the cookies.

So we will make it true and after that return this options object. Now as we know this particular options object should be used while instantiating the InternetExplorerDriver so here we will call our method that is GetIEOptions().

private static IWebDriver GetIEDriver()
{
IWebDriver driver=new InternetExplorerDriver(GetIEOptions());
return driver;
}

And inside our App.Config file we will specify the BrowserType as IExplorer.

<appSettings>
<add Key="Browser" Value="IExplorer"/>
</appSettings>

After that run the script in debug mode so it will launch the Internet Explorer for us with the enable Settings. We will just click on Continue.

Firefox Profile in Selenium C#

Now we will discuss about FirefoxProfile. In Firefox there is a concept of profile, profile means whatever the user has done the Setting for the Firefox.

For example- If i launch my browser using the script.

<add Key="Browser" Value="Firefox"/>

So it is launching the browser again with the default settings, as you can see here the browser does not have any add-on or any bookmarks

but if we look at the actual browser which i have that has lot of bookmarks and add-on enable.

So there is a FirefoxProfile for Firefox.

In order to see the profile for Firefox just open your command prompt and type Firefox~Profile Manager in my case i have only one profile that is a default profile so i will use this. Again inside the BaseClass i will create one more method.

public class BaseClass  
{
private static FirefoxProfile GetFirefoxOptions()
 {
FirefoxProfile profile= new FirefoxProfile();
FirefoxProfileManager manager= new FirefoxProfileManager();
profile= manager.GetProfile("default");
return profile;
 }
}

Here in this method we will take private static and we are using Firefox so we will create the object for FirefoxProfile then write GetFirefoxOptions();

Now inside this we will create the object for FirefoxProfile profile= new FirefoxProfile(); and again if we go to the class definition of this FirefoxProfile class we can see there is lot of methods for Setting the capabilities and Settings for Firefox.

In order to use to get the profile there is another class called FirefoxProfileManager manager= new FirefoxProfileManager(); and using the manager object we are calling GetProfile and in brackets (“”) here we have to give the name of profile.

which is default in my case so i am writing here (“default”); and use the profile object because the return type of GetProfile method is a FirefoxProfile object and then we will return this object.

Now again we have to instantiate our Firefox browser using this profile object so we will call this method and then run this script in debug mode.

private static IWebDriver GetFirefoxDriver()
{
IWebDriver driver= new FirefoxDriver(GetFirefoxOptions());
return driver;
}

So this time when it will be launching the Firefox it should be launch with all the bookmarks and add-on enable for the default profile.

Here you can see all the add-on are enable also with to the bookmarks because this time we are launching it with the help of a profile name called “default”.

If you want to add the Extension so you can use AddExtension as we did in the ChromeOptions.

So that’s all for the browser handling and uses of ChromeOptions, InternetExplorerOptions and FirefoxProfile.