Spread the love

What Is Desired Capabilities in Selenium?

Spread the love

Desired Capabilities got introduced in Selenium to work with lot of browser capabilities. Mostly like-profile data,handling browser cookies , SSL security popup file upload/save etc. In an easy line-It is used to specify the desired capability mostly related to session before the session gets created. In a layman’s language, it is few set of properties/flags/options that needs to be set before we start our test run.

While designing the test it is specified in Webdriver so the client receive the desired capabilities where the run needs to be initiated. If the server sends no data then no capabilities have been supported.

More on webdriver capabilities can be found here. Google’s doc is also very helpful in this respect. More can be found here as well.

The most interesting aspect of desired capabilities is , it gives birth the concept of exception handling.The food for thought can be found here.

If we go little details , this class helps us to communicate with webdriver like which environment we will be running our test scripts. Basically helps us to configure webDriver.It is not only applicable for browser, it is also applicable for mobile testing,where we can set the device properties along with browser version.If we want to run test cases on a different browser on different O.S.

Below are the different methods supported by Desired Capability Class:

READ  How To Configure Jenkins With Selenium Projects
Method NamesSyntaxDescription
getBrowserNamepublic java.lang.String getBrowserName()Gets the current Browser Name
setBrowserNamepublic void setBrowserName(java.lang.String browserName)Sets the current Browser Name(java.lang.String browserName)
getVersionpublic java.lang.string getVersion()Gets the browser version
setVersionpublic void setVersion(java.lang.String browserVersion)Sets the version to the current browser
getPlatformpublic Platform getPlatform()Gets the current platform information
setPlatformpublic Platform setPlatform()Sets the platform
getCapabilitypublic java.lang.object getCapability(java.lang.String CapabilityName)Use to get the current capability of the system
setCapabilitypublic void setCapability(java.lang.String capabilityName ,boolean value)Use to set the current capability of the system
setCapabilitypublic void setCapability(java.lang.String capabilityName ,java.lang.String.value)sets the current capability with a given string
setCapabilitypublic void setCapability(java.lang.String capabilityName ,PlatformValue)sets the current capability with a given Platform info
setCapabilitypublic void setCapability(java.lang.String capabilityName ,java.lang.Object.value)sets the current capability with a given object
READ  How to Resolve SocketException In Selenium for Internet Explorer and Chrome


Sample Implementations:


import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class DesiredCapability {

public static void main(String[] args) {

DesiredCapabilities cap=DesiredCapabilities.internetExplorer();
cap.setCapability(CapabilityType.BROWSER_NAME, "IE");
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
cap.setPlatform(Platform.WIN8);
System.setProperty("Webdriver.ie.driver","C:\test\IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver(cap);
driver.get("www.google.com");
}

}

Few more capabilities:

Method NamesSyntaxDescription
acceptSslCertsCapabilityType.Accept_SSL_CERTSThis is used to enable or disable browser session to accept or reject the SSL certificates
applicationCacheEnabledCapabilityType.SUPPORTS_APPLICATION_CACHEEnables browsers to use cache
CssSelectorEnabledCapabilityType.SUPPORTS_FINDING_BY_CSSChecks if the CSS selector is enabled for finding web element
javaScriptEnabledCapabilityType.SUPPORTS_JAVASCRIPTChecks if the javascript execution is enabled for the browser
TakesScreenshotCapabilityType.TAKES_SCREENSHOTChecks if screenshot taking ability is enabled or not
WebStorage EnabledCapabilityType.SUPPORTS_WEB_STORAGEChecks if the browser instance can interact with storage object
HandleAlertsCapabilityType.SUPPORTS_ALERTSChecks if the browser instance can handle window popup.
READ  Commonly Used Clipboard Objects in Testing

One good example of Desired capability is to disable the save password popup in Chrome. How that is done?


ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-save-password-bubble");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

How to Resolve NoSuchSessionException in selenium free?

How to Resolve NoSuchSessionException in selenium? NoSuchSessionException in Selenium is a tricky...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love