Thursday, September 12, 2019

Post#26.Custom Firefox Profile


Creating Firefox Profile preferences/ Create Custom Firefox Profile
When Firefox is launched with Selenium Web Driver by default, certain plug-ins such as Firebug, Cookie Manager, and Selenium IDE will not be available. Sometimes, we need to use these plug-ins when debugging selenium tests. Therefore, creating custom profile on Firefox and launching web driver with the custom profile is essential for some cases. Here, I will explain how to create Firefox custom profile and use it with Selenium Web Driver. 

On Windows Start Menu, search for the Run command, and then enter “firefox.exe -profilemanager” and then click on “OK” button. 



2. On the Choose User Profile window, click on “Create Profile” button.



3. on the Create Profile Wizard window, click on Next button.



4. Enter a name for the profile. For example, “WebDriveProfile”.



5. Click on the Finish button to complete the profile. Now your profile is ready you can select your profile and open Firefox.



6. Select the profile just created and click on the “Start Firefox” button. Firefox is launched with the custom profile.



7. Here you will notice that the new Firefox window will not show any of your Bookmarks, Favorite icons or any add-ons. Now you can add all the required add-ons to this newly created firefox preference.
Note: The last selected profile, will load automatically at next Firefox launch. You will need to restart profile manager if you wish to change profiles.

Selenium Script:
To access newly created Firefox profile in Selenium Webdriver software test, we need to use webdrivers inbuilt class ‘profilesIni’ and it’s method getProfile as shown below.

//This is a code to implement a profile, which can be embedded in the selenium code.
ProfilesIni profile = new ProfilesIni();

// this will create an object for the Firefox profile
FirefoxProfile fp = profile.getProfile(“WebDriveProfile”);

//Set the profile
FirefoxOptions options = new FirefoxOptions().setProfile(fp);

// this will Initialize the Firefox driver
WebDriver driver = new FirefoxDriver(options)

Firefox Profile Example:

package pack4;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;

public class CustomProfileEx {
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
                        ProfilesIni profileIni = new ProfilesIni();
                        FirefoxProfile fp = profileIni.getProfile("WebDriveProfile");
                        FirefoxOptions options = new FirefoxOptions().setProfile(fp);
                        WebDriver driver = new FirefoxDriver(options);
                        driver.get("https://bing.com");

                        }
}

No comments:

Post a Comment