Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/Capabilities.java
3987 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import java.io.Serializable;
21
import java.util.Collections;
22
import java.util.Map;
23
import java.util.Objects;
24
import java.util.Optional;
25
import java.util.Set;
26
import java.util.stream.Stream;
27
import org.jspecify.annotations.Nullable;
28
29
/** Describes a series of key/value pairs that encapsulate aspects of a browser. */
30
public interface Capabilities extends Serializable {
31
32
default String getBrowserName() {
33
return String.valueOf(Optional.ofNullable(getCapability("browserName")).orElse(""));
34
}
35
36
default @Nullable Platform getPlatformName() {
37
return Stream.of("platformName")
38
.map(this::getCapability)
39
.filter(Objects::nonNull)
40
.map(
41
cap -> {
42
if (cap instanceof Platform) {
43
return (Platform) cap;
44
}
45
46
try {
47
return Platform.fromString((String.valueOf(cap)));
48
} catch (WebDriverException e) {
49
return null;
50
}
51
})
52
.filter(Objects::nonNull)
53
.findFirst()
54
.orElse(null);
55
}
56
57
default String getBrowserVersion() {
58
return String.valueOf(Optional.ofNullable(getCapability("browserVersion")).orElse(""));
59
}
60
61
/**
62
* @return The capabilities as a Map.
63
*/
64
Map<String, Object> asMap();
65
66
/**
67
* @param capabilityName The capability to return.
68
* @return The value, or null if not set.
69
* @see org.openqa.selenium.remote.CapabilityType
70
*/
71
@Nullable Object getCapability(String capabilityName);
72
73
/**
74
* @param capabilityName The capability to check.
75
* @return Whether the value is not null and not false.
76
* @see org.openqa.selenium.remote.CapabilityType
77
*/
78
default boolean is(String capabilityName) {
79
Object cap = getCapability(capabilityName);
80
if (cap == null) {
81
return false;
82
}
83
return cap instanceof Boolean ? (Boolean) cap : Boolean.parseBoolean(String.valueOf(cap));
84
}
85
86
/**
87
* Merge two {@link Capabilities} together and return the union of the two as a new {@link
88
* Capabilities} instance. Capabilities from {@code other} will override those in {@code this}.
89
*/
90
default Capabilities merge(Capabilities other) {
91
return new ImmutableCapabilities(new MutableCapabilities(this).merge(other));
92
}
93
94
default Set<String> getCapabilityNames() {
95
return Collections.unmodifiableSet(asMap().keySet());
96
}
97
}
98
99