问题
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 ?");
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();
}
With json
, server log say :
Capabilities are: {
"browserName": "chrome",
"testName": "Login"
}
But i get a null
value.
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");
Current result Browser Name : chrome
Test Name : null
Expected result Browser Name : chrome
Test Name : Login
Thanks advance
回答1:
You can extract the WebDriver Capabilities e.g. Browser Name
, Browser Version
, Platform Name
, etc using either of the following solutions:
Using direct APIs:
Code Block:
System.out.println("Class Name is : "+((RemoteWebDriver) driver).getCapabilities().getClass().toString()); System.out.println("Browser Name is : "+((RemoteWebDriver) driver).getCapabilities().getBrowserName().toLowerCase()); System.out.println("Browser Version is : "+((RemoteWebDriver) driver).getCapabilities().getVersion().toString()); System.out.println("Platform Name is : "+((RemoteWebDriver) driver).getCapabilities().getPlatform().toString());
Console Output:
Class Name is : class org.openqa.selenium.MutableCapabilities Browser Name is : firefox Browser Version is : 67.0 Platform Name is : WINDOWS
Using
getCapability()
:Code Block:
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities(); System.out.println("acceptInsecureCerts value is: "+cap.getCapability("acceptInsecureCerts")); System.out.println("Browser Name is : "+cap.getBrowserName()); System.out.println("Browser version is : "+cap.getVersion()); System.out.println("Platform is : "+cap.getPlatform().toString()); System.out.println("javascriptEnabled value is: "+cap.getCapability("javascriptEnabled")); System.out.println("moz:accessibilityChecks value is: "+cap.getCapability("moz:accessibilityChecks")); System.out.println("moz:buildID value is: "+cap.getCapability("moz:buildID")); System.out.println("geckodriverVersion value is: "+cap.getCapability("geckodriverVersion")); System.out.println("Headless Mozilla value is: "+((RemoteWebDriver) driver).getCapabilities().getCapability("moz:headless")); System.out.println("moz:headless value is: "+cap.getCapability("moz:headless")); System.out.println("Mozilla Profile value is : "+ ((RemoteWebDriver) driver).getCapabilities().getCapability("moz:profile")); System.out.println("moz:processID value is : "+cap.getCapability("moz:processID")); System.out.println("moz:profile value is : "+cap.getCapability("moz:profile")); System.out.println("moz:shutdownTimeout value is : "+cap.getCapability("moz:shutdownTimeout")); System.out.println("moz:useNonSpecCompliantPointerOrigin value is : "+cap.getCapability("moz:useNonSpecCompliantPointerOrigin")); System.out.println("moz:webdriverClick value is : "+cap.getCapability("moz:webdriverClick")); System.out.println("pageLoadStrategy value is : "+cap.getCapability("pageLoadStrategy")); System.out.println("Platform is : "+cap.getPlatform().toString()); System.out.println("platformName value is : "+cap.getCapability("platformName")); System.out.println("platformVersion value is : "+cap.getCapability("platformVersion")); System.out.println("rotatable value is : "+cap.getCapability("rotatable")); System.out.println("setWindowRect value is : "+cap.getCapability("setWindowRect")); System.out.println("strictFileInteractability value is : "+cap.getCapability("strictFileInteractability")); System.out.println("timeouts values are : "+cap.getCapability("timeouts")); System.out.println("unhandledPromptBehavior value is : "+cap.getCapability("unhandledPromptBehavior"));
Console Output:
acceptInsecureCerts value is: true Browser Name is : firefox Browser version is : 67.0 Platform is : WINDOWS javascriptEnabled value is: true moz:accessibilityChecks value is: false moz:buildID value is: 20190516215225 geckodriverVersion value is: null Headless Mozilla value is: false moz:headless value is: false Mozilla Profile value is : C:\Users\Debanjan.B\AppData\Local\Temp\rust_mozprofile.7HI7QUtzF1YP moz:processID value is : 7308 moz:profile value is : C:\Users\Debanjan.B\AppData\Local\Temp\rust_mozprofile.7HI7QUtzF1YP moz:shutdownTimeout value is : 60000 moz:useNonSpecCompliantPointerOrigin value is : false moz:webdriverClick value is : true pageLoadStrategy value is : normal Platform is : WINDOWS platformName value is : WINDOWS platformVersion value is : 6.2 rotatable value is : false setWindowRect value is : true strictFileInteractability value is : false timeouts values are : {implicit=0, pageLoad=300000, script=30000} unhandledPromptBehavior value is : dismiss and notify
You can find the list of the supported capabilities in the Capabilities section within the WebDriver W3C Recommendation
回答2:
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.
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.
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
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,
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"
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.
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.
来源:https://stackoverflow.com/questions/57352924/how-to-provide-custom-capabilities-on-the-selenium-server