Desired Capabilities Use in Selenium WebDriver: Current Days most of the companies are before release the application to end user the application is tested under various environments like Local environment, Release environment, production environment, BAU (Business As Usual ) environment, etc. like these environments the application is also tested on various devices like Android device, iOS device, Tablet, and Desktop device. During the testing on different environment and devices, we are keeping the behavior of the application.
As we discussed earlier that we could test our application in a different environment with the help of the selenium grid in the same way we can set the different devices through the desired capabilities. So we are going to what are the different desired capabilities are available in selenium webDriver.
If you are not checked yet about complete Selenium WebDriver Tutorial Series which can help for both Freshers & Experience then you can check that by following the article.
What are the Desired Capabilities?
These Desired Capabilities help us to set the properties of the WebDriver browsers. We have the Independent Browser capabilities like browser version, name, acceptSslCerts, and also browser-specific capabilities like chromeOptions for setting the chrome specific desired capabilities and firefox_binary for Mozilla firefox specific capabilities.
Use of Desired Capabilities
- We can use it to configure the Selenium WebDriver driver instance.
- With this, we can configure all drivers like FirefoxDriver, ChromeDriver, and InternetExplorerDriver.
- We can also set the browser properties like browser name, platform, and browser version settings.
- With the help of Desired Capabilities, we can run our several automation scripts in different operating systems and different versions.
- As per the current standard, we need to test our application on every testing environment that may be a web browser, mobile device, or that can be a mobile emulator or simulator so that we never miss a single bug to the end-user.
- With The help of Desired capabilities, we can directly interact with the WebDriver, and in that, we can mention which is going to be used in our scripts.
- During the cross-browser testing, you can easily set the configuration.
Desired Capabilities Methods
Here are some of the methods which we are using in the Desired Capabilities in Selenium WebDriver. The methods look like the Getter and Setter method. By using this method, we can set the properties, and also we can get their values as well. You can use the various methods below:
- getBrowserName()
- setBrowserName()
- getPlatform()
- setPlatform()
- getVersion()
- setVersion()
- getCapability()
- setCapability()
- acceptInsecureCerts()
- setJavaScriptEnabled()
- setAcceptInsecureCerts()
To use this class, you have first to need to create an instance, and with the use of that instance, you can use the methods mentioned above. Hence you can declare the class instance like below
DesiredCapabilities capability = new DesiredCapabilities();
DesiredCapabilities With Respect To Browser / Platform
For use the DesiredCapabilities in automation script you have to import the package
org.openqa.selenium.remote.DesiredCapabilities;
In the above, we have mentioned how you can create an instance of DesiredCapabilities. Still, we can also instantiate the DesiredCapabilities class directly with a specific browser without using the setBrowserName() or setCapability() method. you can instantiate like below:
DesiredCapabilities cap = DesiredCapabilities.chrome();
Capabilities Types
During our automation execution, we are passing some of the capability types as an argument to the setCapability() method to perform some actions as per our requirement. Below we have mentioned how you can use those capability type and the different capability type methods:
DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
List of Capability Types
- ACCEPT_INSECURE_CERTS
- BROWSER_VERSION
- ACCEPT_SSL_CERTS
- APPLICATION_NAME
- BROWSER_NAME
- TAKES_SCREENSHOT
- PROXY
- LOGGING_PREFS
- ELEMENT_SCROLL_BEHAVIOUR
- ENABLE_PERSISTENT_HOVERING
- HAS_NATIVE_EVENTS
- ENABLE_PROFILING_CAPABILITY
- HAS_TOUCHSCREEN
- SUPPORTS_APPLICATION_CACHE
- PLATFORM
- VERSION
- PLATFORM_NAME
Here are some of the commonly used methods, but you can get more methods from this link.
Example Program:
public class Desired_Capability { public static void main(String[] args) throws IOException { // Path of geckodriver System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Go to URL driver.get("https://www.stqatools.com/"); // Created object of DesiredCapabilities class DesiredCapabilities capabilities = new DesiredCapabilities(); // Set your device name capabilities.setCapability("deviceName", "your Device Name"); // Set BROWSER_NAME desired capability. capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome"); // Set your mobile device's OS version. capabilities.setCapability(CapabilityType.VERSION, "5.1"); // Set android platformName desired capability. capabilities.setCapability("platformName", "Android"); } }
I hope with this article you can get an idea about what is the desired capabilities and how to use that in you script. If you are want to more about the desired capabilities, then you can chk this link.
Leave a Reply