Basically this is used to set certain properties of browser for the WebDriver.
Properties such as:
- set the path of the browser if not installed in default location.
- set the browser Version, Browser Name, platform (Normally we use in selenium GRID)
- to set the firefox profile
- configurations related to browser cookies, SSL security popups,
Let’s see some of the implementation of DesiredCapabilities in selenium:
A part of the code to set browser, platform and version:
String NodeUrl = "http://10.112.76.224:4444/wd/hub"; //Node URL String BaseUrl = "http://www.google.com"; //Web App under test DesiredCapabilities cap = new DesiredCapabilities(); cap = DesiredCapabilities.firefox(); /*cap = DesiredCapabilities.internetExplorer(); *cap = DesiredCapabilities.chrome(); */ cap.setPlatform(Platform.WINDOWS); WebDriver driver = new RemoteWebDriver(new URL(NodeUrl), cap); driver.get(BaseUrl); /*Rest of the code goes on from here*/
Disable application cache:
//To disable application cache cap.setCapability("applicationCacheEnabled", "false"); WebDriver driver= new FirefoxDriver(cap);
Set firefox profile through code :
//A way to set fireFox profile through DesiredCapabilities FirefoxProfile fp = new FirefoxProfile(); DesiredCapabilities cap = DesiredCapabilities.firefox(); cap.setCapability(FirefoxDriver.PROFILE, fp); WebDriver driver = new RemoteWebDriver(cap);
Refer firefox profile and it’s preferences for more information.
Set firefox installation path, if not on default path:
//To set firefox path, if not in default location cap.setCapability("firefox_binary", "Path of FireFox browser installation"); //path is like "C:FirefoxPortableAppFirefoxfirefox.exe" WebDriver driver = new FirefoxDriver(cap);
Through DesiredCapabilities, we can set ChromeOptions (setting ChromeDriver-specific capabilities)
Example of setting ChromeOptions,
Some times we get certificate error while launching Chrome browser using selenium webDriver
error:You are using unsupported command-line flag..
To get rid of the error banner on Chrome-
DesiredCapabilities cap = DesiredCapabilities.chrome(); ChromeOptions options = new ChromeOptions(); options.addArguments("test-type"); capabilities.setCapability("chrome.binary", ""); capabilities.setCapability(ChromeOptions.CAPABILITY, options); webDriver = new ChromeDriver(cap);
1 Comment