org.openqa.selenium.remote.NewSessionPayload Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-remote-driver Show documentation
Show all versions of selenium-remote-driver Show documentation
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.remote;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.json.Json.LIST_OF_MAPS_TYPE;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openqa.selenium.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.json.JsonOutput;
import org.openqa.selenium.remote.http.Contents;
public class NewSessionPayload implements Closeable {
private static final Dialect DEFAULT_DIALECT = Dialect.W3C;
private static final Predicate ACCEPTED_W3C_PATTERNS = new AcceptedW3CCapabilityKeys();
private final Json json = new Json();
private final Contents.Supplier supplier;
private final Set dialects;
private NewSessionPayload(Contents.Supplier supplier) {
this.supplier = supplier;
Set dialects = new LinkedHashSet<>();
try {
if (isW3C()) {
dialects.add(Dialect.W3C);
}
this.dialects = Set.copyOf(dialects);
validate();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static NewSessionPayload create(Capabilities caps) {
Require.nonNull("Capabilities", caps);
return create(Collections.singleton(caps));
}
public static NewSessionPayload create(Collection caps) {
// We need to convert the capabilities into a new session payload. At this point we're dealing
// with references, so I'm Just Sure This Will Be Fine.
return create(
Map.of(
"capabilities",
Map.of(
"firstMatch",
caps.stream().map(Capabilities::asMap).collect(Collectors.toList()))));
}
public static NewSessionPayload create(Map source) {
// It is expected that the input to this method contains a properly formed
// "new session" request, and not just a random blob of data. Make sure
// this precondition is met before continuing.
Require.precondition(
source.containsKey("capabilities"), "New session payload must contain capabilities");
return new NewSessionPayload(Contents.asJson(Require.nonNull("Payload", source)));
}
public static NewSessionPayload create(Contents.Supplier supplier) {
return new NewSessionPayload(supplier);
}
public Contents.Supplier getSupplier() {
return supplier;
}
private void validate() throws IOException {
Map alwaysMatch = getAlwaysMatch();
if (alwaysMatch == null) {
alwaysMatch = Map.of();
}
Map always = alwaysMatch;
Collection