博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Selenium+Java] Desired Capabilities in Selenium
阅读量:4610 次
发布时间:2019-06-09

本文共 6961 字,大约阅读时间需要 23 分钟。

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

Desired Capabilities in Selenium WebDriver

Everyscenario should be executed on some specific testing environment. The testing environment can be a web browser,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

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.

The above emulator example shows the platform set which is android and the platform version set which is IceCream Sandwich (4.x).

In this tutorial, you will learn-

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.

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 Capabilities 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()
  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 is in use currently in the 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, 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" inhas 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 ouron Internet explorer browser to open www.gmail.com website using 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 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")
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

The Desired Capabilities class will help to set an 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 having the capabilities that we desire to use.

This article is contributed by Krithika Ramkumar

转载于:https://www.cnblogs.com/alicegu2009/p/9098688.html

你可能感兴趣的文章
[2018/11/18] Java数据结构(2) 简单排序 冒泡排序 选择排序 插入排序
查看>>
关于WPF程序只运行一个实例的方法
查看>>
局域网内访问机器时出现“未授予在次计算机上的请求登陆类型”
查看>>
Bogart BogartAutoCode.vb
查看>>
JavaScript面试题
查看>>
[转帖]架构师眼中的高并发架构
查看>>
ios的一些开源资源
查看>>
HTTP 错误 500.21 - Internal Server Error 解决方案
查看>>
Bucks sign Sanders to $44 million extension
查看>>
【PHP】Windows下配置用mail()发送邮件
查看>>
人类简史
查看>>
java 设计模式学习
查看>>
【Python使用】使用pip安装卸载Python包(含离线安装Python包)未完成???
查看>>
一语道破项目管理知识体系五大过程组
查看>>
C# 备份、还原、拷贝远程文件夹
查看>>
在windows环境下运行compass文件出现的错误提示解决方案
查看>>
CSS常用样式--font
查看>>
恩如氏--蜗牛精华补水蚕丝面膜
查看>>
大工具-收藏
查看>>
codevs3027 线段覆盖 2
查看>>