Firefox profile and preferences in selenium

Firefox profile is basically the personal settings of the firefox browser.

Firefox profile contains information like your homepage, bookmarks, browser settings, history, saved passwords.

Profile is basically a specific folder stored locally in your hard drive other than your firefox installation folder.

We will see how we can set Firefox profile manually and then call in code or directly we can set the Firefox profile in code itself.



Manually:

  • First, exit from firefox browser, it it's opened. File -> Exit,
  • In windows, Start -> Run, enter firefox -p,
  • Create a new profile, name it anything (let's say MyFFProfile)  
  • Select  MyFFProfile and click on "Start Firefox"
  • Now you can set the firefox configurations like, set homepage, download path, Themes etc
Now we will see how we can use this particular above profile in Selenium

public static void main(String[] args) {
 ProfilesIni profini = new ProfilesIni();
 FirefoxProfile profile = profini.getProfile("MyFFProfile");
 WebDriver driver = new FirefoxDriver(profile);
 driver.get("http://www.qavalidation.com/");
        //rest code goes here
  }

Through Code:

There are browser Preferences like set download path. set language etc... we can set in profile.

Note: To get the list of firefox preferences, open firefox browser and type "about:config" in browser url and press enter.





Let's see the implementation

public static void main(String[] args) { 
 FirefoxProfile profile= new FirefoxProfile();
 //set homepage
 profile.setPreference("browser.startup.homepage","http://qavalidation.com");
 //Set language
 profile.setPreference("intl.accept_languages", "no,en-us,en");
 //Accept SSL certificate errors
 profile.setAcceptUntrustedCertificates(true);
 profile.setAssumeUntrustedCertificateIssuer(true);
 //Download a file in FF browser
 profile.setPreference("browser.download.folderList", 2);
 profile.setPreference("browser.helperApps.alwaysAsk.force", false);
 profile.setPreference("browser.download.manager.showWhenStarting",false); 
 profile.setPreference("browser.download.dir", "C:\\Downloads"); 
 profile.setPreference("browser.download.downloadDir","C:\\Downloads"); 
 profile.setPreference("browser.download.defaultFolder","C:\\Downloads"); 
 profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/anytext ,text/plain,text/html,application/plain" );
  
 DesiredCapabilities dc=DesiredCapabilities.firefox();
 dc.setCapability(FirefoxDriver.PROFILE, profile);
 FirefoxDriver driver =  new FirefoxDriver(dc);
 driver.get("http://www.qavalidation.com/p/demo.html");
        //rest code goes here
   }

No comments:

Post a Comment