Skip to content

Commit c23e05b

Browse files
Removal of "magic" strings
1 parent a79c624 commit c23e05b

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

src/main/java/org/openqa/selenium/remote/NewSessionPayload.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public class NewSessionPayload implements Closeable {
7979
.addAll(getAppiumCapabilities(AndroidMobileCapabilityType.class))
8080
.addAll(getAppiumCapabilities(IOSMobileCapabilityType.class))
8181
.addAll(getAppiumCapabilities(YouiEngineCapabilityType.class)).build();
82+
private static final String APPIUM_PREFIX = "appium:";
83+
private static final String DESIRED_CAPABILITIES = "desiredCapabilities";
84+
private static final String CAPABILITIES = "capabilities";
85+
private static final String REQUIRED_CAPABILITIES = "requiredCapabilities";
86+
private static final String FIRST_MATCH = "firstMatch";
87+
private static final String ALWAYS_MATCH = "alwaysMatch";
8288

8389
private final Set<CapabilitiesFilter> adapters;
8490
private final Set<CapabilityTransform> transforms;
@@ -124,7 +130,7 @@ public static NewSessionPayload create(Capabilities caps) throws IOException {
124130

125131
HashMap<String, ?> capabilityMap = new HashMap<>(caps.asMap());
126132
capabilityMap.remove(FORCE_MJSONWP);
127-
Map<String, ?> source = of("desiredCapabilities", capabilityMap);
133+
Map<String, ?> source = of(DESIRED_CAPABILITIES, capabilityMap);
128134
String json = new Json().toJson(source);
129135
return new NewSessionPayload(new StringReader(json), forceMobileJSONWP);
130136
}
@@ -239,20 +245,20 @@ public void writeTo(Appendable appendable) throws IOException {
239245
}
240246

241247
// Write the first capability we get as the desired capability.
242-
json.name("desiredCapabilities");
248+
json.name(DESIRED_CAPABILITIES);
243249
json.write(first, MAP_TYPE);
244250

245251
// And write the first capability for gecko13
246-
json.name("capabilities");
252+
json.name(CAPABILITIES);
247253
json.beginObject();
248254

249-
json.name("desiredCapabilities");
255+
json.name(DESIRED_CAPABILITIES);
250256
json.write(first, MAP_TYPE);
251257

252258
// Then write everything into the w3c payload. Because of the way we do this, it's easiest
253259
// to just populate the "firstMatch" section. The spec says it's fine to omit the
254260
// "alwaysMatch" field, so we do this.
255-
json.name("firstMatch");
261+
json.name(FIRST_MATCH);
256262
json.beginArray();
257263
//noinspection unchecked
258264
getW3C().forEach(map -> json.write(map, MAP_TYPE));
@@ -274,9 +280,9 @@ private void writeMetaData(JsonOutput out) throws IOException {
274280
while (input.hasNext()) {
275281
String name = input.nextName();
276282
switch (name) {
277-
case "capabilities":
278-
case "desiredCapabilities":
279-
case "requiredCapabilities":
283+
case CAPABILITIES:
284+
case DESIRED_CAPABILITIES:
285+
case REQUIRED_CAPABILITIES:
280286
input.skipValue();
281287
break;
282288

@@ -326,7 +332,7 @@ private Map<String, Object> getOss() throws IOException {
326332
input.beginObject();
327333
while (input.hasNext()) {
328334
String name = input.nextName();
329-
if ("desiredCapabilities".equals(name)) {
335+
if (DESIRED_CAPABILITIES.equals(name)) {
330336
return input.read(MAP_TYPE);
331337
} else {
332338
input.skipValue();
@@ -383,7 +389,7 @@ private Stream<Map<String, Object>> getW3C() throws IOException {
383389
public String getKey() {
384390
String key = stringObjectEntry.getKey();
385391
if (APPIUM_CAPABILITIES.contains(key) && !forceMobileJSONWP) {
386-
return "appium:" + key;
392+
return APPIUM_PREFIX + key;
387393
}
388394
return key;
389395
}
@@ -448,11 +454,11 @@ private Map<String, Object> getAlwaysMatch() throws IOException {
448454
input.beginObject();
449455
while (input.hasNext()) {
450456
String name = input.nextName();
451-
if ("capabilities".equals(name)) {
457+
if (CAPABILITIES.equals(name)) {
452458
input.beginObject();
453459
while (input.hasNext()) {
454460
name = input.nextName();
455-
if ("alwaysMatch".equals(name)) {
461+
if (ALWAYS_MATCH.equals(name)) {
456462
return input.read(MAP_TYPE);
457463
} else {
458464
input.skipValue();
@@ -474,11 +480,11 @@ private Collection<Map<String, Object>> getFirstMatches() throws IOException {
474480
input.beginObject();
475481
while (input.hasNext()) {
476482
String name = input.nextName();
477-
if ("capabilities".equals(name)) {
483+
if (CAPABILITIES.equals(name)) {
478484
input.beginObject();
479485
while (input.hasNext()) {
480486
name = input.nextName();
481-
if ("firstMatch".equals(name)) {
487+
if (FIRST_MATCH.equals(name)) {
482488
return input.read(LIST_OF_MAPS_TYPE);
483489
} else {
484490
input.skipValue();

0 commit comments

Comments
 (0)