Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/MutableCapabilities.java
3990 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.util.Collections;
21
import java.util.HashSet;
22
import java.util.Map;
23
import java.util.Set;
24
import java.util.TreeMap;
25
import org.jspecify.annotations.Nullable;
26
import org.openqa.selenium.internal.Require;
27
28
public class MutableCapabilities implements Capabilities {
29
30
private static final Set<String> OPTION_KEYS;
31
32
static {
33
HashSet<String> keys = new HashSet<>();
34
keys.add("goog:chromeOptions");
35
keys.add("ms:edgeOptions");
36
keys.add("moz:firefoxOptions");
37
keys.add("se:ieOptions");
38
OPTION_KEYS = Collections.unmodifiableSet(keys);
39
}
40
41
private final Map<String, Object> caps = new TreeMap<>();
42
43
public MutableCapabilities() {
44
// no-arg constructor
45
}
46
47
public MutableCapabilities(Capabilities other) {
48
this(other.asMap());
49
}
50
51
public MutableCapabilities(Map<String, ?> capabilities) {
52
capabilities.forEach(
53
(key, value) -> {
54
if (value != null) {
55
setCapability(key, value);
56
}
57
});
58
}
59
60
/**
61
* Merge two {@link Capabilities} together and return the union of the two as a new {@link
62
* Capabilities} instance. Capabilities from {@code other} will override those in {@code this}.
63
*/
64
@Override
65
public MutableCapabilities merge(Capabilities other) {
66
MutableCapabilities newInstance = new MutableCapabilities(this);
67
if (other != null) {
68
other.asMap().forEach(newInstance::setCapability);
69
}
70
return newInstance;
71
}
72
73
public void setCapability(String capabilityName, boolean value) {
74
setCapability(capabilityName, (Object) value);
75
}
76
77
public void setCapability(String capabilityName, String value) {
78
setCapability(capabilityName, (Object) value);
79
}
80
81
public void setCapability(String capabilityName, Platform value) {
82
setCapability(capabilityName, (Object) value);
83
}
84
85
public void setCapability(String key, @Nullable Object value) {
86
Require.nonNull("Capability name", key);
87
88
// We have to special-case some keys and values because of the popular idiom of calling
89
// something like "capabilities.setCapability(SafariOptions.CAPABILITY, new SafariOptions());"
90
// and this is no longer needed as options are capabilities. There will be a large amount of
91
// legacy code that will always try and follow this pattern, however.
92
if (OPTION_KEYS.contains(key) && value instanceof Capabilities) {
93
((Capabilities) value).asMap().forEach(this::setCapability);
94
return;
95
}
96
97
if (value == null) {
98
caps.remove(key);
99
return;
100
}
101
102
SharedCapabilitiesMethods.setCapability(caps, key, value);
103
}
104
105
@Override
106
public Map<String, Object> asMap() {
107
return Collections.unmodifiableMap(caps);
108
}
109
110
@Override
111
public @Nullable Object getCapability(String capabilityName) {
112
return caps.get(capabilityName);
113
}
114
115
@Override
116
public Set<String> getCapabilityNames() {
117
return Collections.unmodifiableSet(caps.keySet());
118
}
119
120
public Map<String, Object> toJson() {
121
return asMap();
122
}
123
124
@Override
125
public int hashCode() {
126
return SharedCapabilitiesMethods.hashCode(this);
127
}
128
129
@Override
130
public boolean equals(@Nullable Object o) {
131
if (!(o instanceof Capabilities)) {
132
return false;
133
}
134
return SharedCapabilitiesMethods.equals(this, (Capabilities) o);
135
}
136
137
@Override
138
public String toString() {
139
return SharedCapabilitiesMethods.toString(this);
140
}
141
}
142
143