Selenium C# Tutorial: Browser Profiles

Updated: Jan 18

As you all know, Selenium WebDriver can support multiple types of browsers; you should also know that each browser has its unique profile and why this fact is crucial when it comes to automation execution?


The simple answer is that you want to run your automation on a predefined profile; this profile should always be the same. Otherwise, your test results will be different on every execution cycle, and that’s simply unacceptable.


In this article, I want to demonstrate how we can control the profile that will be used per automation execution, but before that, let's examine a few more aspects of the browser profile.

  • The things that you should know:

  • Each browser can hold one or more profiles (Each profile can be unique).

  • Each profile contains a predefined password and Log-ins.

  • Each profile includes a different favorites list.

  • Each profile contains user preferences.

  • Each profile contains various Add-Ons.

Based on the above list, we can see that our profile has a critical impact on our automation execution. Therefore we must control the profile and what it contains.

Note: This article is relevant to all browsers, but I will demonstrate on Firefox only to simplify things.


Where can you find your browser profiles?

The location of your browser profile depends on the operating system you use, but I assume that you are using Microsoft XP and above; based on this, the path will be:


C:\Users\User_Name\AppData\Roaming\Mozilla\Firefox\Profiles


How can you create a browser profile?

Open firefox -> in the URL field type: “about:profiles”

Press on “Create a New Profile”

continue with the instructions.


Controlling the profile using Selenium

We have two options that we can use for working with browser profiles:


Method 1: Specific Profile Selection


private static void SelectProfile()

{

FirefoxProfileManager Profile = new FirefoxProfileManager();

FirefoxProfile ProfileForOurAutomation = new FirefoxProfile();

ProfileForOurAutomation = Profile.GetProfile("Profile Name That You Want");

IWebDriver FirefoxInstance = new FirefoxDriver(ProfileForOurAutomation);

FirefoxInstance.Navigate().GoToUrl("http://www.google.com");

}


Method 2: Get a collection of the available profiles

This code will extract all the available profiles that you can use in your code execution

private static void ProfileList()

{

FirefoxProfileManager Profile = new FirefoxProfileManager();

FirefoxProfile ProfileForOurAutomation = new FirefoxProfile();


foreach (var item in Profile.ExistingProfiles)

{

DebuggClass.PrintToFile(item.ToString());

}




10 views0 comments