Вы находитесь на странице: 1из 9

12/13/2018 Desired Capabilities in Selenium WebDriver

(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

Desired Capabili es in Selenium WebDriver


What is Desired Capability?
The desired capability is a series of key/value pairs that stores the browser properties like
browsername, browser version, the path of the browser driver in the system, etc. to
determine the behaviour of the browser at run time.

Desired capability can also be used to configure the driver instance of Selenium
WebDriver.
We can configure driver instance like FirefoxDriver, ChromeDriver, InternetExplorerDriver
by using desired capabilities.

In this tutorial, you will learn-

What is Desired Capability?


Why do we need Desired Capabilities?
Different types of Desired Capabilities Methods
Example for set capability method

Why do we need Desired Capabili es?


Every Testing (/software-testing.html)scenario should be executed on some specific testing
environment. The testing environment can be a web browser, Mobile (/mobile-
testing.html)device, mobile emulator, mobile simulator, etc.

The Desired Capabilities Class helps us to tell the webdriver, which environment we are
going to use in our test script.

The setCapability method of the DesiredCapabilities Class, which is explained in the later
part of the tutorial, can be used in Selenium Grid. It is used to perform a parallel execution
on different machine configurations.

Ex: Grid

https://www.guru99.com/desired-capabilities-selenium.html 1/9
12/13/2018 Desired Capabilities in Selenium WebDriver

(/images/3-

2016/032216_0948_DesiredCapa1.jpg)

It is used to set the browser properties (Ex. Chrome, IE), Platform Name (Ex. Linux,
Windows) that are used while executing the test cases.

In the case of mobile automation, as we perform the tests on different varieties of mobile
devices, the Mobile Platform (ex. iOS, Android) Platform Version (Ex. 3.x,4.x in Android) can
be set.

(/images/3-

2016/032216_0948_DesiredCapa2.jpg)
https://www.guru99.com/desired-capabilities-selenium.html 2/9
The above
12/13/2018 emulator example shows the platform
Desired setin which
Capabilities Selenium is android and the platform
WebDriver

version set which is IceCream Sandwich (4.x).

Desired Capabilities are more useful in cases like:

In mobile application automation, where the browser properties and the device properties
can be set.
In Selenium grid when we want to run the test cases on a different browser with different
operating systems and versions.

Different types of Desired Capabili es Methods


Here we will see a different type of desired capabilities methods and see how to use one of
this method "setCapability Method".

1. getBrowserName()

public java.lang.String getBrowserName()

2. setBrowserName()

public void setBrowserName(java.lang.String browserName)

3. getVersion()

public java.lang.String getVersion()

4. setVersion()

public void setVersion(java.lang.String version)

5. getPlatform()

public Platform getPlatform()

6. setPlatform()

public Platform getPlatform()

https://www.guru99.com/desired-capabilities-selenium.html 3/9
7. getCapability
12/13/2018 Method Desired Capabilities in Selenium WebDriver

The getCapability method of the DesiredCapabilities class can be used to get the capability
that is in use currently in the system.

public java.lang.Object getCapability(java.lang.String capabilityName)

8. setCapabilityMethod

The setCapability() method of the Desired Capabilities class can be used to set the device
name, platform version, platform name, absolute path of the app under test (the .apk file of
the app(Android) under test), app Activity (in Android) and appPackage(java).

"setCapability method" in Java (/java-tutorial.html)has the below declarations:

setCapability : public void setCapability(java.lang.String capabilityName,boolean value)

setCapability :public void setCapability(java.lang.String capabilityName,java.lang.String


value)

setCapability :public void setCapability(java.lang.String capabilityName,Platform value)

setCapability :public void setCapability(java.lang.String key,java.lang.Object value)

Example for set capability method


Let us consider an example where we want to run our Test Case (/test-case.html)on Internet
explorer browser to open www.gmail.com website using Selenium Webdriver.

Following is the code.

https://www.guru99.com/desired-capabilities-selenium.html 4/9
12/13/2018 Desired Capabilities in Selenium WebDriver
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.ie.InternetExplorerDriver;

public class IEtestforDesiredCapabilities {

public static void main(String[] args) {

WebDriver IEdriver = new InternetExplorerDriver();


driver.manage().window().maximize();
driver.get("http://gmail.com");

driver.quit();
}

Now run this code from Eclipse and check out the console.

Output:

It will throw the following error when above code is executed. The error occurs because the
path to the browser driver (IE in the above case) is not set.The browser could not be located
by the selenium code.

The path to the driver executable must be set by the webdriver.ie.driver system
property; formore information, see
http://code.google.com/p/selenium/wiki/InternetExplorerDriver. The latest version can
be downloaded from http://code.google.com/p/selenium/downloads/list

Dec 11, 201212:59:43PM org.openqa.selenium.ie.InternetExplorerDriverServer


initializeLib

WARNING: This method of starting the IE driver is deprecated and will be removed in
selenium 2.26. Please download the IEDriverServer.exe from
http://code.google.com/p/selenium/downloads/list and ensure that it is in your PATH.

Solution:

The solution for the above problem is given in the warning section of the error itself.

Download the Internet ExplorerDriver standalone server for 32bit or 64bit.


Save the driver in a suitable location in the system.
Set the path for the driver using the System.setProperty method.
It is used to set the IE driver with the webdriver property. It helps to locate the driver
executable file that is stored in the system location.
(Ex:"C:\IEDriverLocation\IEDriver.exe")

https://www.guru99.com/desired-capabilities-selenium.html 5/9
12/13/2018 Desired Capabilities in Selenium WebDriver
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.ie.InternetExplorerDriver;
importorg.openqa.selenium.remote.DesiredCapabilities;

public class IEtestforDesiredCapabilities {

public static void main(String[] args) {

//it is used to define IE capability


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", "C:\\IEDriverServer.exe");

//it is used to initialize the IE driver


WebDriver driver = new InternetExplorerDriver(capabilities);

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

driver.get("http://gmail.com");

driver.quit();
}

Code Explanation:

In the code above,

The import statements is to import the required packages for the selenium web driver,
required packages for the Internet Explorer driver, packages for the desired capabilities.
setCapability takes the various capabilities as input variables which are then used by the
web driver to launch the application in the desired environment.
setProperty is used to set the path where the driver is located. Web Driver then locates
the required driver.
Gmail website is opened in the Internet Explorer browser by using "get" method.

Output:

The test case on Internet explorer browser will run successfully using Selenium Webdriver.

Conclusion

https://www.guru99.com/desired-capabilities-selenium.html 6/9
The Desired
12/13/2018 Capabilities class will helpDesired
to setCapabilities
an environment to define the behaviour of the
in Selenium WebDriver

browser/environment on which the test can be executed.

It helps to launch our application in the desired environment having the capabilities that we
desire to use.

This article is contributed by Krithika Ramkumar

 Prev (/use-autoit-selenium.html) Report a Bug

Next  (/ssl-certificate-error-handling-selenium.html)

YOU MIGHT LIKE:

SELENIUM SELENIUM SELENIUM

(/xslt-report- (/xpath-selenium.html) (/firefox-profile-selenium-


selenium.html) (/xpath- webdriver.html)
(/xslt-report- selenium.html) (/firefox-profile-
selenium.html) selenium-
XPath in Selenium
XSLT Report in Selenium WebDriver: Complete webdriver.html)
(/xslt-report-selenium.html) Tutorial How to Create Firefox
(/xpath-selenium.html) Profile in Selenium
WebDriver
(/firefox-profile-selenium-
webdriver.html)

SELENIUM SELENIUM SELENIUM

(/handling-cookies- (/implicit-explicit-waits- (/find-element-


selenium-webdriver.html) selenium.html) selenium.html)
(/handling-cookies- (/implicit-explicit- (/find-element-
selenium- waits- selenium.html)
webdriver.html) selenium.html) Find Element and
Cookie Handling in Selenium Implicit Wait & Explicit Wait FindElements in Selenium
WebDriver in Selenium WebDriver
(/handling-cookies- (/implicit-explicit-waits- (/find-element-
selenium-webdriver.html) selenium.html) selenium.html)

Selenium Tutorials
24) Introduction to TestNG Groups (/introduction-testng-groups.html)
https://www.guru99.com/desired-capabilities-selenium.html 7/9
25) Test
12/13/2018 Case Priority in TestNG (/test-case-priority-testng.html)
Desired Capabilities in Selenium WebDriver

26) Sessions,Parallel & Dependency (/sessions-parallel-run-and-dependency-in-selenium.html)

27) Execute multiple test suites (/testng-execute-multiple-test-suites.html)

28) TestNG Listeners (/listeners-selenium-webdriver.html)

29) TestNG Tutorial (/testng-tutorial.html)

30) TestNG Report Generation (/testng-report.html)

31) PDF, Emails and Screenshot (/pdf-emails-and-screenshot-of-test-reports-in-selenium.html)

32) POM & Page Factory (/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html)

33) Parameterization (/parameterization-using-xml-and-dataproviders-selenium.html)

34) Excel in Selenium (/all-about-excel-in-selenium-poi-jxl.html)

35) Handling Date Time Picker (/handling-date-time-picker-using-selenium.html)

36) Selenium Grid (/introduction-to-selenium-grid.html)

37) Maven & Jenkins (/maven-jenkins-with-selenium-complete-tutorial.html)

38) Create Framework Key & Hybrid (/creating-keyword-hybrid-frameworks-with-selenium.html)

39) Database Testing (/database-testing-using-selenium-step-by-step-guide.html)

40) Handling Iframes (/handling-iframes-selenium.html)

23) Contains, Sibling, Ancestor (/using-contains-sbiling-ancestor-to-find-element-in-selenium.html)

24) Implicit & Explicit (/implicit-explicit-waits-selenium.html)

33) Cross Browser Testing (/cross-browser-testing-using-selenium.html)

35) Take Screenshot in Selenium (/take-screenshot-selenium-webdriver.html)

37) Log4j and LogExpert (/tutorial-on-log4j-and-logexpert-with-selenium.html)

38) HTMLUnit Driver & PhantomJS (/selenium-with-htmlunit-driver-phantomjs.html)

39) Robot API (/using-robot-api-selenium.html)

40) AutoIT with Selenium (/use-autoit-selenium.html)

42) SSL Certificate Error Handling (/ssl-certificate-error-handling-selenium.html)

43) Handling Ajax call (/handling-ajax-call-selenium-webdriver.html)

 (https://www.facebook.com/guru99com/) 
(https://twitter.com/guru99com) 
https://www.guru99.com/desired-capabilities-selenium.html 8/9
12/13/2018 (https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
Desired Capabilities in Selenium WebDriver


(https://forms.aweber.com/form/46/724807646.htm)

About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)

Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)

Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)

Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)

© Copyright - Guru99 2018


Privacy Policy (/privacy-policy.html)

https://www.guru99.com/desired-capabilities-selenium.html 9/9

Вам также может понравиться