Monday, August 3, 2015

Lesson #16 - Selenium WebDriver – Browser Profiles(C#)

Overview

As you all know, Selenium WebDriver can support, multiple types of browsers, as such you should also know that each browser has its own 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 predefined profile, this profile should always be the same, otherwise your end 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 lets examine 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 contains different favorites list.
  • Each profile contains the user preferences.
  • Each profile contains different Add-Ons.
  • Etc.

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

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

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 the “RUN” dialog box.
Type: firefox.exe –p.
Press “Ok”.



Press “Create Profile…” -> and then “Next”.
Enter Profile Name “DtVisionTech”.
Press “Finish”.


Controlling the profile using Selenium

Code:

Method 1: Specifying the Profile


Now that we have two profiles that we can use, it’s time to show how to use selenium to select a specific profile at the beginning of the code execution.

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());
            }


No comments:

Post a Comment

My Presentations