Friday, January 24, 2014

Firefox Webdriver Profile & Desired Capabilities Setting

What is Desired Capabilities ?


Basically, DesiredCapabilities help to set properties for the WebDriver. A typical usecase would be to set the path for the FirefoxDriver if your local installation doesn't correspond to the default settings. capabilities describes a series of key/value pairs that encapsulate aspects of a browser.

FireFox WebDriver and Desired capabilities


Usually we use below code to initiate FireFox driver

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

Webdriver driver =  new FirefoxDriver();

Now, we need to learn about how to setup custom profile or capabilities of Firefox. So that we can have desired preference for browser. Someone may want to have Firefox accept site certificates and may want to file download to specific location without and popup for save etc..Selenium webdriver helps in setting desired capabilities for FireFox browser.

For setting desired capabilities we need to use below code.

import org.openqa.selenium.remote.DesiredCapabilities; 

DesiredCapabilities dc=DesiredCapabilities.firefox();
Webdriver driver =  new FirefoxDriver(dc);


FireFox Profile 


We can create custom FireFox profile and use it with desired capabilities for example :

import org.openqa.selenium.firefox.FirefoxProfile;

FirefoxProfile profile = new FirefoxProfile();

Example to use Firefox profile with desired capabilities

DesiredCapabilities dc=DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE, profile);
Webdriver driver =  new FirefoxDriver(dc);

Thus the webdriver will get initilized with desired capabilities & profile set.

What preferences to use in firefox profile?


Here is the interesting question, now we know that how to set Firefox profile? What is the use of it? or which preferences can be used with it?  The answer is simple but we need to understand what does the preferences mean here and how we can get to know them.

Preferences can be some setting of Firefox browser specific to the download or any other type of preffred settings. To get the preferences you can open Firefox browser and type "about:config" in url and hit enter. you will get the list of all the Firefox prerences list and Settings.

Now we need to use them in our code for setting Firefox profile for example :

FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(false);
profile.setAssumeUntrustedCertificateIssuer(true);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.alwaysAsk.force", false); 



Finally you can combile all the things above and initiate FireFox driver as shown in below example:


FirefoxProfile profile = new FirefoxProfile();
DesiredCapabilities dc=DesiredCapabilities.firefox();
profile.setAcceptUntrustedCertificates(false);
profile.setAssumeUntrustedCertificateIssuer(true);
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" );
dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
Webdriver driver =  new FirefoxDriver(dc);



Above code will initilise firefox driver which will never ask for file download for file MIME type mentioned. We can add more file MIME types in above code as per requirenment. File MIME type can be found here.

For more information on desired capabilities please refere this link.

11 comments:

  1. HI...Very nice explained....keep it posting ...! very helpful.
    :-) :-)

    ReplyDelete
  2. Thanks a lot!! clears most of concepts!!

    ReplyDelete
  3. If I am installing Selenium in Windows 7, do I type the above commands in the Windows command prompt? When I typed import org.openqa.selenium.WebDriver;, it was not recognized as an internal or external command. Please help. Thanks.

    ReplyDelete
  4. Really very well explained.Thanks alot

    ReplyDelete
  5. Really very well explained.Thanks alot

    ReplyDelete
  6. Can you modify this article to include the opening of an existing FF profile? As I understand, this simply will "create" a profile.

    ReplyDelete
  7. Can you update your article to include opening an already created FF profile? This one creates a new one.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Is the concept of profile is only for only FF driver or do we have it in the chrome and IE as well?

    ReplyDelete
  10. Below code snipped will work

    WebDriver driver = new ChromeDriver()
    LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
    for (LogEntry entry : logEntries) {
    System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
    }

    ReplyDelete
  11. Hi there,

    Please update this topic wrt Selenium 3

    ReplyDelete