如何在硒服务器上提供自定义功能? [英] How to provide custom capabilities on the selenium server?
问题描述
我知道可以通过一种方法获得某些硒的功能,其中之一就是这样:
I know that some selenium capabilities can be obtained with a method, one of them like this :
driver.getCapabilities().getBrowserName();
它返回浏览器名称的值.
It returns the value of the browser name.
但是,如果它指的是可用的方法,如果我没有误会它,这似乎与自定义功能有关,就像我的意思:
But if it refers to an available method, if I don't misunderstand it, this seems to be related to custom capabilities, like this I mean :
driver.getCapabilities().getCapability("something ?");
返回值:值;如果未设置,则返回null.
Returns: The value, or null if not set.
所以,我试图编写一个简单的代码来获得我的意思.
So, I've tried to make a simple code to get the value I mean.
private RemoteWebDriver driver;
private URL url;
private DesiredCapabilities dc = new DesiredCapabilities();
@Before
public void setUp() throws MalformedURLException {
url = new URL("http://localhost:4444/wd/hub");
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
//this is custom capability i mean
dc.setCapability("testName", "Login");
driver = new RemoteWebDriver(url, dc);
}
@Test
public void test() {
some code.....
}
@After
public void tearDown() {
System.out.println("Browser Name : "+ driver.getCapabilities().getCapability("browserName"));
System.out.println("Test Name : "+ driver.getCapabilities().getCapability("testName"));
driver.quit();
}
使用json
时,服务器日志显示为:
With json
, server log say :
Capabilities are: {
"browserName": "chrome",
"testName": "Login"
}
但是我得到一个null
值.
如何正确设置?您如何使我们的服务器提供testName
功能?可以通过driver.getCapabilities().getCapability("testName");
How to the right setup ? How do you make our server provide the capabilities testName
I mean? and can be obtained with driver.getCapabilities().getCapability("testName");
当前结果 Browser Name : chrome
Test Name : null
预期结果 Browser Name : chrome
Test Name : Login
感谢前进
推荐答案
听起来您正在寻找如何在Grid配置中添加自定义功能.这是可能的,但是需要几个步骤.
It sounds like what you're looking for is how to add a custom capability to your Grid configuration. This is possible, but requires several steps.
首先,您需要构建一个功能匹配器.匹配器将是其自己的项目,将Selenium-Server库和Selenium-Java库作为依赖项导入.您将需要一个扩展org.openqa.grid.internal.utils.DefaultCapabilityMatcher
的类,并使用自己的逻辑覆盖matches()
方法,以确定节点是否具备所需的功能.完成后,您将构建该项目并生成一个jar文件.
First, you need to build a capability matcher. The matcher will be its own project, importing the Selenium-Server and Selenium-Java libraries as dependencies. You'll need a single class that extends org.openqa.grid.internal.utils.DefaultCapabilityMatcher
, and overrides the matches()
method with your own logic to determine whether or not a node possesses the desired capability. When complete, you'll build this project and generate a jar file.
第二,您需要将新的匹配器附加到Grid Hub.我将匹配器jar存储在与selenium-server-standalone jar相同的目录中,并更改了正常的启动命令以容纳匹配器.
Second, you'll need to attach your new matcher to your Grid Hub. I store my matcher jars in the same directory as my selenium-server-standalone jar, and I alter my normal launch command to accommodate the matcher.
java -cp <custom-matcher>-1.0.0.jar;selenium-server-standalone-3.141.59.jar org.openqa.grid.selenium.GridLauncher -role hub -hubConfig hubConfig.json
在我的hubConfig.json
中,我必须在JSON中添加两行以插入匹配器:
In my hubConfig.json
, I had to add two lines to the JSON to wire in the matcher:
"capabilityMatcher": "fully.qualified.path.to.Matcher",
"throwOnCapabilityNotPresent": true,
第三,您需要配置节点以接受新功能.如果您还使用JSON来配置节点,则就像为功能添加新行一样简单:
Third, you'll need to configure your nodes to accept the new capability. If you're using JSON to also configure your nodes, it's just as simple as adding a new line for your capability:
"capabilityName": "foo"
就是这样.还值得注意的是,DesiredCapabilities
确实具有称为applicationName
的功能,该功能保留为用户可定义的值.根据您的特定用例,您也许可以利用此功能来代替添加新内容.
That's pretty much it. It's also worth noting that DesiredCapabilities
does have a capability called applicationName
, which is left as a user-definable value. Depending on your specific use case, you might be able to leverage this capability in place of adding something new.
当我学习此内容时,我在Github上发现了一个示例存储库.处理自己.这是一个基本设置,但是很好地说明了上面的步骤.它应该为您自己的实现提供一个很好的起点.
I found a sample repo on Github a while back when I was learning this process myself. It's a basic setup, but it illustrates the steps above pretty well. It should give you a great starting point for your own implementation.
这篇关于如何在硒服务器上提供自定义功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!