Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,12 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Optional.ofNullable;
import static java.util.logging.Logger.getLogger;
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION;

import com.google.common.base.Supplier;
import com.google.common.base.Throwables;

import com.google.common.io.CountingOutputStream;
import com.google.common.io.FileBackedOutputStream;

import io.appium.java_client.internal.Config;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.Command;
Expand All @@ -45,24 +36,13 @@
import org.openqa.selenium.remote.Response;
import org.openqa.selenium.remote.ResponseCodec;
import org.openqa.selenium.remote.codec.w3c.W3CHttpCommandCodec;
import org.openqa.selenium.remote.http.Filter;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.WebSocket;
import org.openqa.selenium.remote.http.WebSocket.Listener;
import org.openqa.selenium.remote.service.DriverService;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ConnectException;
import java.net.URL;
import java.util.Map;
Expand Down Expand Up @@ -95,7 +75,6 @@ public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
this(additionalCommands, null, checkNotNull(addressOfRemoteServer), httpClientFactory);
}


public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
URL addressOfRemoteServer) {
this(additionalCommands, addressOfRemoteServer, HttpClient.Factory.createDefault());
Expand Down Expand Up @@ -165,65 +144,13 @@ private Response createSession(Command command) throws IOException {
if (getCommandCodec() != null) {
throw new SessionNotCreatedException("Session already exists");
}
ProtocolHandshake handshake = new ProtocolHandshake() {
@SuppressWarnings("unchecked")
public Result createSession(HttpClient client, Command command) throws IOException {
Capabilities desiredCapabilities = (Capabilities) command.getParameters().get("desiredCapabilities");
Capabilities desired = desiredCapabilities == null ? new ImmutableCapabilities() : desiredCapabilities;

//the number of bytes before the stream should switch to buffering to a file
int threshold = (int) Math.min(Runtime.getRuntime().freeMemory() / 10, Integer.MAX_VALUE);
FileBackedOutputStream os = new FileBackedOutputStream(threshold);
try {

CountingOutputStream counter = new CountingOutputStream(os);
Writer writer = new OutputStreamWriter(counter, UTF_8);
NewAppiumSessionPayload payload = NewAppiumSessionPayload.create(desired);
payload.writeTo(writer);

try (InputStream rawIn = os.asByteSource().openBufferedStream();
BufferedInputStream contentStream = new BufferedInputStream(rawIn)) {

Method createSessionMethod = this.getClass().getSuperclass()
.getDeclaredMethod("createSession", HttpClient.class, InputStream.class, long.class);
createSessionMethod.setAccessible(true);

Optional<Result> result = (Optional<Result>) createSessionMethod.invoke(this,
client.with(httpHandler -> req -> {
req.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase());
return httpHandler.execute(req);
}), contentStream, counter.getCount());

return result.map(result1 -> {
Result toReturn = result.get();
getLogger(ProtocolHandshake.class.getName())
.info(format("Detected dialect: %s", toReturn.getDialect()));
return toReturn;
}).orElseThrow(() -> new SessionNotCreatedException(
format("Unable to create a new remote session. Desired capabilities = %s", desired)));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new SessionNotCreatedException(format("Unable to create a new remote session. "
+ "Make sure your project dependencies config does not override "
+ "Selenium API version %s used by java-client library.",
Config.main().getValue("selenium.version", String.class)), e);
} catch (InvocationTargetException e) {
String message = "Unable to create a new remote session.";
if (e.getCause() != null) {
if (e.getCause() instanceof WebDriverException) {
message += " Please check the server log for more details.";
}
message += format(" Original error: %s", e.getCause().getMessage());
}
throw new SessionNotCreatedException(message, e);
}
} finally {
os.reset();
}
}
};

ProtocolHandshake.Result result = handshake
.createSession(getClient(), command);
ProtocolHandshake.Result result = new ProtocolHandshake().createSession(
getClient().with((httpHandler) -> (req) -> {
req.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase());
return httpHandler.execute(req);
}), command
);
Dialect dialect = result.getDialect();
setCommandCodec(dialect.getCommandCodec());
getAdditionalCommands().forEach(this::defineCommand);
Expand All @@ -241,9 +168,9 @@ public Response execute(Command command) throws WebDriverException {
throw new WebDriverException(e.getMessage(), e);
}
});
}
if (getAdditionalCommands().containsKey(command.getName())) {
super.defineCommand(command.getName(), getAdditionalCommands().get(command.getName()));
if (getAdditionalCommands().containsKey(command.getName())) {
super.defineCommand(command.getName(), getAdditionalCommands().get(command.getName()));
}
}

Response response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ public interface MobileCapabilityType extends CapabilityType {
*/
String EVENT_TIMINGS = "eventTimings";

/**
* This is the flag which forces server to switch to the mobile JSONWP.
* If {@code false} then it is switched to W3C mode.
*/
String FORCE_MJSONWP = "forceMjsonwp";

/**
* (Web and webview only) Enable ChromeDriver's (on Android)
* or Safari's (on iOS) performance logging (default {@code false}).
Expand Down
Loading