0

Could someone explain to me what is the use of DesiredCapabilities in Selenium Webdriver with an example?

I am confused with setting a profile and using the DesiredCapabilities.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Rajendra
  • 21
  • 5
  • 1
    Check it out here - http://stackoverflow.com/questions/17527951/what-is-the-use-of-desiredcapabilities-in-selenium-webdriver – ievche Nov 03 '16 at 12:56

2 Answers2

2

You, as a user of WebDriver, have the flexibility to create a session for a browser with your own set of desired capabilities that a browser should or shouldn't have. Using the capabilities feature in WebDriver, you are given a way to specify your choice of how your browser should behave.

Some of the examples of browser capabilities include enabling a browser session to support taking screenshots of the webpage, executing custom JavaScript on the webpage, enabling the browser session to interact with window alerts, and so on.

There are many capabilities that are specific to individual browsers, but there are some specific capabilities that are generic to all the browsers. We will discuss some of them here, and the remaining, as and when we come across those features in this book. The browser-specific capabilities will be discussed in greater detail in the next chapter.

Capabilities is an interface in the WebDriver library whose direct implementation is the DesiredCapabilities class. The series of steps involved in creating a browser session with specific capabilities is as follows:

Identify all of the capabilities that you want to arm your browser with.

  1. Create a DesiredCapabilities class instance and set all of the capabilities to it.
  2. Now, create an instance of WebDriver with all of the above capabilities passed to it.
  3. This will create an instance of Firefox/IE/Chrome or whichever browser you have instantiated with all of your desired capabilities.

Let's create an instance of FirefoxDriver while enabling the takesScreenShot capability:

public class BrowserCapabilities {
public static void main(String... args) {
  Map capabilitiesMap = new HashMap();
  capabilitiesMap.put("takesScreenShot", true);
  DesiredCapabilities capabilities 
             = new DesiredCapabilities(capabilitiesMap);
  WebDriver driver = new FirefoxDriver(capabilities);
  driver.get("http://www.google.com");
}
}

In the preceding code, we set all of the capabilities that we desire in a map and created an instance of DesiredCapabilities using that map. Now, we have created an instance of FirefoxDriver with these capabilities. This will now launch a Firefox browser that will have support for taking screenshots of the webpage. If you see the definition of the DesiredCapabilities class, the constructor of the class is overloaded in many different ways. Passing a map is one of them. You can use the default constructor and create an instance of the DesiredCapabilities class, and then set the capabilities using the setCapability() method.

Some of the default capabilities that are common across browsers are shown in the following table:

Capability What it is used for

takesScreenShot
Tells whether the browser session can take a screenshot of the webpage

handlesAlert
Tells whether the browser session can handle modal dialogs

cssSelectorsEnabled
Tells whether the browser session can use CSS selectors while searching for         elements

javascriptEnabled
Enables/disables user-supplied JavaScript execution in the context of the webpage

acceptSSLCerts
Enables/disables the browser to accept all of the SSL certificates by default

webStorageEnabled
This is an HTML5 feature, and it is possible to enable or disable the browser session to interact with storage objects

There are many other capabilities of WebDriver.

Source: Book "Selenium WebDriver Practical Guide" by Satya Avasarala

Fenio
  • 3,528
  • 1
  • 13
  • 27
0

Desired capability is a series of key/value pairs that stores browser properties like browser name, versioN and the path of the browser driver in the system, etc. to determine the behaviour of the browser at run time.

It can also be used to configure the driver instance of Selenium WebDriver like FirefoxDriver, ChromeDriver, InternetExplorerDriver.

An Example:

importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.ie.InternetExplorerDriver;
importorg.openqa.selenium.remote.DesiredCapabilities;

public class IEtestforDesiredCapabilities {

public static void main(String[] args) {


DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

capabilities.setCapability(CapabilityType.BROWSER_NAME, "IE");
capabilities.setCapability(InternetExplorerDriver.
  INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);


System.setProperty("webdriver.ie.driver", "Put IEDriverServer path here");

WebDriver driver = newInternetExplorerDriver(capabilities);

driver.manage().window().maximize();

driver.get("http://www.yahoo.com");

driver.quit();
 }

}