Skip to content

Commit 92396dc

Browse files
refactor!: Use the new session payload creator inherited from Selenium (#1535)
1 parent 8057ac6 commit 92396dc

File tree

3 files changed

+9
-627
lines changed

3 files changed

+9
-627
lines changed

src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,12 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Throwables.throwIfUnchecked;
21-
import static java.lang.String.format;
22-
import static java.nio.charset.StandardCharsets.UTF_8;
2321
import static java.util.Optional.ofNullable;
24-
import static java.util.logging.Logger.getLogger;
2522
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION;
2623

2724
import com.google.common.base.Supplier;
2825
import com.google.common.base.Throwables;
2926

30-
import com.google.common.io.CountingOutputStream;
31-
import com.google.common.io.FileBackedOutputStream;
32-
33-
import io.appium.java_client.internal.Config;
34-
import org.openqa.selenium.Capabilities;
35-
import org.openqa.selenium.ImmutableCapabilities;
3627
import org.openqa.selenium.SessionNotCreatedException;
3728
import org.openqa.selenium.WebDriverException;
3829
import org.openqa.selenium.remote.Command;
@@ -45,24 +36,13 @@
4536
import org.openqa.selenium.remote.Response;
4637
import org.openqa.selenium.remote.ResponseCodec;
4738
import org.openqa.selenium.remote.codec.w3c.W3CHttpCommandCodec;
48-
import org.openqa.selenium.remote.http.Filter;
4939
import org.openqa.selenium.remote.http.HttpClient;
50-
import org.openqa.selenium.remote.http.HttpHandler;
5140
import org.openqa.selenium.remote.http.HttpRequest;
5241
import org.openqa.selenium.remote.http.HttpResponse;
53-
import org.openqa.selenium.remote.http.WebSocket;
54-
import org.openqa.selenium.remote.http.WebSocket.Listener;
5542
import org.openqa.selenium.remote.service.DriverService;
5643

57-
import java.io.BufferedInputStream;
5844
import java.io.IOException;
59-
import java.io.InputStream;
60-
import java.io.OutputStreamWriter;
61-
import java.io.UncheckedIOException;
62-
import java.io.Writer;
6345
import java.lang.reflect.Field;
64-
import java.lang.reflect.InvocationTargetException;
65-
import java.lang.reflect.Method;
6646
import java.net.ConnectException;
6747
import java.net.URL;
6848
import java.util.Map;
@@ -95,7 +75,6 @@ public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
9575
this(additionalCommands, null, checkNotNull(addressOfRemoteServer), httpClientFactory);
9676
}
9777

98-
9978
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
10079
URL addressOfRemoteServer) {
10180
this(additionalCommands, addressOfRemoteServer, HttpClient.Factory.createDefault());
@@ -165,65 +144,13 @@ private Response createSession(Command command) throws IOException {
165144
if (getCommandCodec() != null) {
166145
throw new SessionNotCreatedException("Session already exists");
167146
}
168-
ProtocolHandshake handshake = new ProtocolHandshake() {
169-
@SuppressWarnings("unchecked")
170-
public Result createSession(HttpClient client, Command command) throws IOException {
171-
Capabilities desiredCapabilities = (Capabilities) command.getParameters().get("desiredCapabilities");
172-
Capabilities desired = desiredCapabilities == null ? new ImmutableCapabilities() : desiredCapabilities;
173-
174-
//the number of bytes before the stream should switch to buffering to a file
175-
int threshold = (int) Math.min(Runtime.getRuntime().freeMemory() / 10, Integer.MAX_VALUE);
176-
FileBackedOutputStream os = new FileBackedOutputStream(threshold);
177-
try {
178-
179-
CountingOutputStream counter = new CountingOutputStream(os);
180-
Writer writer = new OutputStreamWriter(counter, UTF_8);
181-
NewAppiumSessionPayload payload = NewAppiumSessionPayload.create(desired);
182-
payload.writeTo(writer);
183-
184-
try (InputStream rawIn = os.asByteSource().openBufferedStream();
185-
BufferedInputStream contentStream = new BufferedInputStream(rawIn)) {
186-
187-
Method createSessionMethod = this.getClass().getSuperclass()
188-
.getDeclaredMethod("createSession", HttpClient.class, InputStream.class, long.class);
189-
createSessionMethod.setAccessible(true);
190147

191-
Optional<Result> result = (Optional<Result>) createSessionMethod.invoke(this,
192-
client.with(httpHandler -> req -> {
193-
req.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase());
194-
return httpHandler.execute(req);
195-
}), contentStream, counter.getCount());
196-
197-
return result.map(result1 -> {
198-
Result toReturn = result.get();
199-
getLogger(ProtocolHandshake.class.getName())
200-
.info(format("Detected dialect: %s", toReturn.getDialect()));
201-
return toReturn;
202-
}).orElseThrow(() -> new SessionNotCreatedException(
203-
format("Unable to create a new remote session. Desired capabilities = %s", desired)));
204-
} catch (NoSuchMethodException | IllegalAccessException e) {
205-
throw new SessionNotCreatedException(format("Unable to create a new remote session. "
206-
+ "Make sure your project dependencies config does not override "
207-
+ "Selenium API version %s used by java-client library.",
208-
Config.main().getValue("selenium.version", String.class)), e);
209-
} catch (InvocationTargetException e) {
210-
String message = "Unable to create a new remote session.";
211-
if (e.getCause() != null) {
212-
if (e.getCause() instanceof WebDriverException) {
213-
message += " Please check the server log for more details.";
214-
}
215-
message += format(" Original error: %s", e.getCause().getMessage());
216-
}
217-
throw new SessionNotCreatedException(message, e);
218-
}
219-
} finally {
220-
os.reset();
221-
}
222-
}
223-
};
224-
225-
ProtocolHandshake.Result result = handshake
226-
.createSession(getClient(), command);
148+
ProtocolHandshake.Result result = new ProtocolHandshake().createSession(
149+
getClient().with((httpHandler) -> (req) -> {
150+
req.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase());
151+
return httpHandler.execute(req);
152+
}), command
153+
);
227154
Dialect dialect = result.getDialect();
228155
setCommandCodec(dialect.getCommandCodec());
229156
getAdditionalCommands().forEach(this::defineCommand);
@@ -241,9 +168,9 @@ public Response execute(Command command) throws WebDriverException {
241168
throw new WebDriverException(e.getMessage(), e);
242169
}
243170
});
244-
}
245-
if (getAdditionalCommands().containsKey(command.getName())) {
246-
super.defineCommand(command.getName(), getAdditionalCommands().get(command.getName()));
171+
if (getAdditionalCommands().containsKey(command.getName())) {
172+
super.defineCommand(command.getName(), getAdditionalCommands().get(command.getName()));
173+
}
247174
}
248175

249176
Response response;

src/main/java/io/appium/java_client/remote/MobileCapabilityType.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,6 @@ public interface MobileCapabilityType extends CapabilityType {
119119
*/
120120
String EVENT_TIMINGS = "eventTimings";
121121

122-
/**
123-
* This is the flag which forces server to switch to the mobile JSONWP.
124-
* If {@code false} then it is switched to W3C mode.
125-
*/
126-
String FORCE_MJSONWP = "forceMjsonwp";
127-
128122
/**
129123
* (Web and webview only) Enable ChromeDriver's (on Android)
130124
* or Safari's (on iOS) performance logging (default {@code false}).

0 commit comments

Comments
 (0)