Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/Capabilities.java
1865 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.NullMarked;
28
import org.jspecify.annotations.Nullable;
29
30
/** Describes a series of key/value pairs that encapsulate aspects of a browser. */
31
@NullMarked
32
public interface Capabilities extends Serializable {
33
34
default String getBrowserName() {
35
return String.valueOf(Optional.ofNullable(getCapability("browserName")).orElse(""));
36
}
37
38
default @Nullable Platform getPlatformName() {
39
return Stream.of("platformName")
40
.map(this::getCapability)
41
.filter(Objects::nonNull)
42
.map(
43
cap -> {
44
if (cap instanceof Platform) {
45
return (Platform) cap;
46
}
47
48
try {
49
return Platform.fromString((String.valueOf(cap)));
50
} catch (WebDriverException e) {
51
return null;
52
}
53
})
54
.filter(Objects::nonNull)
55
.findFirst()
56
.orElse(null);
57
}
58
59
default String getBrowserVersion() {
60
return String.valueOf(Optional.ofNullable(getCapability("browserVersion")).orElse(""));
61
}
62
63
/**
64
* @return The capabilities as a Map.
65
*/
66
Map<String, Object> asMap();
67
68
/**
69
* @param capabilityName The capability to return.
70
* @return The value, or null if not set.
71
* @see org.openqa.selenium.remote.CapabilityType
72
*/
73
@Nullable Object getCapability(String capabilityName);
74
75
/**
76
* @param capabilityName The capability to check.
77
* @return Whether the value is not null and not false.
78
* @see org.openqa.selenium.remote.CapabilityType
79
*/
80
default boolean is(String capabilityName) {
81
Object cap = getCapability(capabilityName);
82
if (cap == null) {
83
return false;
84
}
85
return cap instanceof Boolean ? (Boolean) cap : Boolean.parseBoolean(String.valueOf(cap));
86
}
87
88
/**
89
* Merge two {@link Capabilities} together and return the union of the two as a new {@link
90
* Capabilities} instance. Capabilities from {@code other} will override those in {@code this}.
91
*/
92
default Capabilities merge(Capabilities other) {
93
return new ImmutableCapabilities(new MutableCapabilities(this).merge(other));
94
}
95
96
default Set<String> getCapabilityNames() {
97
return Collections.unmodifiableSet(asMap().keySet());
98
}
99
}
100
101