Desired Capability in Selenium:
What is Desired Capability?
- The desired capability is a series of key / value pairs that stores browser properties such as browser names, browser versions, paths of browser driver in the system, etc. so that browser behavior can be set at scheduled time.
- The desired capability can also be used to configure the example of the driver of Selenium WebDriver.
- Using the desired capabilities we can configure driver instance such as Firefox driver, ChromeDriver, Internet Explorer Driver.
Use of Desired Capability:
- This is used to configure the driver instance of Selenium WebDriver.
- We can configure all driver examples such as FirefoxDriver, ChromeDriver, InternetExplorerDriver, using the desired capabilities.
- It allows setting browser properties such as browser name, platform, browser version setting.
- Mostly, the class of desired capabilities was used when we used the Selenium grid.
- On many systems with different versions and different operating systems, we have to execute several test cases.
- Each test scenario should be executed on some specific testing environment. The test environment can be a web browser, mobile device, mobile emulator, mobile simulator, etc.
- The desired capabilities category helps us tell the WebDriver, which is going to be used in our test scripts.
- The set up capacity method of the desired capabilities category, which is explained in the later part of the tutorial, can be used in the Selenium Grid. It is used to perform parallel execution on various machine configurations.
Package:
1 | org.openqa.selenium.remote.DesiredCapabilities; |
Create Object / Instance of DesiredCapabilities class:
1 | DesiredCapabilities capabilities = new DesiredCapabilities(); |
Set android deviceName desired capability. Set your device name:
1 | capabilities.setCapability("deviceName", "your Device Name"); |
Set BROWSER_NAME desired capability:
1 | capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome"); |
Set android VERSION desired capability. Set your mobile device’s OS version:
1 | capabilities.setCapability(CapabilityType.VERSION, "5.1"); |
Set android platformName desired capability. It’s Android in our case here:
1 | capabilities.setCapability("platformName", "Android"); |
Create Firefox Driver with Marionette capabilities:
1 2 3 4 5 | DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability(“marionette”, true); WebDriver driver = new FirefoxDriver(capabilities); |
Different types of Desired Capabilities Methods in Selenium:
- getBrowserName(): Using this Method getBrowserName.
1 | public java.lang.String getBrowserName() |
- setBrowserName(): Using this Method Set browserName.
1 | public void setBrowserName(java.lang.String browserName) |
- getVersion(): Using this Method getVersion.
1 | public java.lang.String getVersion() |
- setVersion(): Using this Method setVersion.
1 | public void setVersion(java.lang.String version) |
- getPlatform(): Using this Method getPlatform Details.
1 | public Platform getPlatform() |
- setPlatform(): Using this Method setPlatform Details.
1 | public Platform setPlatform() |
Example of Desired Capability in Selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; 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"); } } |
What are the use of DesiredCapabilities in Selenium WebDriver and desired capabilities in selenium webdriver for chrome