[Selenium+java] Desired capabilities in Selenium

Source: Internet
Author: User
Tags selenium grid guru99

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

Desired capabilities in Selenium Webdriver

Every testing scenario should be executed on some specific testing environment. The testing environment can be a Web browser, mobile device, mobile emulator, Mobile simulator, etc.

The desired capabilities Class helps us to tell the webdriver, which environment we is going to use with our test script.

The setcapability method of the Desiredcapabilities Class, which is explained in the later part of the tutorial, Can is used in Selenium Grid. It is used to perform a parallel execution on different machine configurations.

Ex:grid

It is used to set the browser properties (ex. Chrome, IE), Platform Name (ex. Linux, Windows) that's used while Executin G The test cases.

In the case of mobile automation, as we perform the tests on different varieties of mobile devices, the mobile Platform (E X. IOS, Android) Platform Version (Ex. 3.x,4.x in Android) can is set.

The above emulator example shows the platform set which are Android and the platform version set which is Icecream Sandwich (4.x).

In this tutorial, you'll learn-

    • What is desired Capability
    • Different types of desired capabilities Methods
    • Example for Set capability method
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 is used to configure the driver instance of Selenium Webdriver.
    • We can configure driver instance like Firefoxdriver, Chromedriver, internetexplorerdriver by using desired capabilities.

Desired capabilities is more useful in cases like:

    • In mobile application automation, where the browser properties and the device properties can is set.
    • In Selenium grid if we want to run the test cases on a different browser with different operating systems and versions.
Different types of desired capabilities Methods

Here we'll see a different type of desired capabilities methods and see how to use one of the This method "setcapability Method ".

    1. Getbrowsername ()
Public java.lang.String Getbrowsername ()
    1. Setbrowsername ()
public void Setbrowsername (java.lang.String browsername)
    1. GetVersion ()
Public java.lang.String getversion ()
    1. Setversion ()
public void Setversion (java.lang.String version)
    1. GetPlatform ()
Public Platform GetPlatform ()
    1. Setplatform ()
Public Platform GetPlatform ()
    1. Getcapability Method

The Getcapability method of the Desiredcapabilities class can be used to get the capability that's in use currently in th E system.

Public Java.lang.Object getcapability (java.lang.String capabilityname)
    1. Setcapabilitymethod

The Setcapability () method of the desired capabilities class can be used to set the device name, platform version, platfor M 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 have the below declarations:

setcapability:public void setcapability (java.lang.String capabilityname,boolean value)
Setcapability  :p ublic void setcapability (java.lang.String capabilityname,java.lang.string value)
Setcapability  :p ublic void setcapability (java.lang.String capabilityname,platform value)
Setcapability  :p ublic void setcapability (java.lang.String key,java.lang.object value)
Example for Set capability method

Let us consider a example where we want to run our Test case on Internet Explorer browser to open www.gmail.com website u Sing Selenium webdriver.

Following is the code.

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'll throw the following error when above code is executed. The error occurs because the path to the browser driver (IE, the above case) are not set. The browser could not being located by the selenium code.

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

Dec One, 201212:59:43pm org.openqa.selenium.ie.InternetExplorerDriverServer initializelib

Warning:this method of starting the IE driver is deprecated and would be removed in selenium 2.26. Download the IEDriverServer.exe from http://code.google.com/p/selenium/downloads/list and ensure that it's in your PATH.

Solution:

The solution for the above problem are given in the warning section of the 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, which is the stored of the system location. (Ex: "C:\IEDriverLocation\IEDriver.exe")
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 E Xplorer driver, packages for the desired capabilities.
    • Setcapability takes the various capabilities as input variables which is then used by the Web driver to launch the Applic ation 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 is on Internet Explorer browser would run successfully using Selenium webdriver.

Conclusion

The desired capabilities class would help to set a environment to define the behaviour of the browser/environment on which The test can be executed.

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

This article was contributed by Krithika Ramkumar

[Selenium+java] Desired capabilities in Selenium

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.