Skip to content

Commit 12cb9d9

Browse files
#655 FIX
1 parent ac44a16 commit 12cb9d9

File tree

7 files changed

+32
-339
lines changed

7 files changed

+32
-339
lines changed

src/main/java/io/appium/java_client/AppiumDriver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.openqa.selenium.remote.DriverCommand;
4343
import org.openqa.selenium.remote.ErrorHandler;
4444
import org.openqa.selenium.remote.ExecuteMethod;
45+
import org.openqa.selenium.remote.HttpCommandExecutor;
4546
import org.openqa.selenium.remote.Response;
4647
import org.openqa.selenium.remote.html5.RemoteLocationContext;
4748
import org.openqa.selenium.remote.http.HttpClient;
@@ -82,7 +83,7 @@ public class AppiumDriver<T extends WebElement>
8283
* @param capabilities take a look
8384
* at {@link org.openqa.selenium.Capabilities}
8485
*/
85-
public AppiumDriver(AppiumCommandExecutor executor, Capabilities capabilities) {
86+
public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) {
8687
super(executor, capabilities);
8788
this.executeMethod = new AppiumExecutionMethod(this);
8889
locationContext = new RemoteLocationContext(executeMethod);

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.collect.ImmutableMap;
2020

2121
import org.apache.commons.lang3.StringUtils;
22+
import org.openqa.selenium.remote.CommandInfo;
2223
import org.openqa.selenium.remote.http.HttpMethod;
2324

2425
import java.time.Duration;
@@ -79,7 +80,7 @@ public class MobileCommand {
7980
protected static final String SET_SETTINGS;
8081
protected static final String GET_CURRENT_PACKAGE;
8182

82-
public static final Map<String, AppiumCommandInfo> commandRepository;
83+
public static final Map<String, CommandInfo> commandRepository;
8384

8485
static {
8586
RESET = "reset";

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
import io.appium.java_client.CommandExecutionHelper;
2525
import io.appium.java_client.FindsByAndroidUIAutomator;
2626
import io.appium.java_client.PressesKeyCode;
27-
import io.appium.java_client.remote.AppiumCommandExecutor;
2827
import io.appium.java_client.remote.MobilePlatform;
2928
import io.appium.java_client.service.local.AppiumDriverLocalService;
3029
import io.appium.java_client.service.local.AppiumServiceBuilder;
3130
import org.openqa.selenium.Capabilities;
3231
import org.openqa.selenium.WebElement;
32+
import org.openqa.selenium.remote.HttpCommandExecutor;
3333
import org.openqa.selenium.remote.http.HttpClient;
3434

3535
import java.net.URL;
@@ -58,7 +58,7 @@ public class AndroidDriver<T extends WebElement>
5858
* @param capabilities take a look
5959
* at {@link org.openqa.selenium.Capabilities}
6060
*/
61-
public AndroidDriver(AppiumCommandExecutor executor, Capabilities capabilities) {
61+
public AndroidDriver(HttpCommandExecutor executor, Capabilities capabilities) {
6262
super(executor, substituteMobilePlatform(capabilities, ANDROID_PLATFORM));
6363
}
6464

src/main/java/io/appium/java_client/ios/IOSDriver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
import io.appium.java_client.FindsByIosNSPredicate;
2525
import io.appium.java_client.FindsByIosUIAutomation;
2626
import io.appium.java_client.HidesKeyboardWithKeyName;
27-
import io.appium.java_client.remote.AppiumCommandExecutor;
2827
import io.appium.java_client.remote.MobilePlatform;
2928
import io.appium.java_client.service.local.AppiumDriverLocalService;
3029
import io.appium.java_client.service.local.AppiumServiceBuilder;
3130
import org.openqa.selenium.Alert;
3231
import org.openqa.selenium.Capabilities;
3332
import org.openqa.selenium.WebElement;
3433
import org.openqa.selenium.remote.DriverCommand;
34+
import org.openqa.selenium.remote.HttpCommandExecutor;
3535
import org.openqa.selenium.remote.Response;
3636
import org.openqa.selenium.remote.http.HttpClient;
3737
import org.openqa.selenium.security.Credentials;
@@ -64,7 +64,7 @@ public class IOSDriver<T extends WebElement>
6464
* @param capabilities take a look
6565
* at {@link org.openqa.selenium.Capabilities}
6666
*/
67-
public IOSDriver(AppiumCommandExecutor executor, Capabilities capabilities) {
67+
public IOSDriver(HttpCommandExecutor executor, Capabilities capabilities) {
6868
super(executor, substituteMobilePlatform(capabilities, IOS_PLATFORM));
6969
}
7070

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

Lines changed: 22 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,17 @@
1616

1717
package io.appium.java_client.remote;
1818

19-
import static com.google.common.base.Preconditions.checkNotNull;
20-
import static com.google.common.base.Throwables.getRootCause;
2119
import static com.google.common.base.Throwables.throwIfUnchecked;
22-
import static org.openqa.selenium.remote.DriverCommand.GET_ALL_SESSIONS;
23-
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION;
24-
import static org.openqa.selenium.remote.DriverCommand.QUIT;
2520

26-
import io.appium.java_client.AppiumCommandInfo;
27-
import org.openqa.selenium.NoSuchSessionException;
28-
import org.openqa.selenium.SessionNotCreatedException;
29-
import org.openqa.selenium.UnsupportedCommandException;
21+
import com.google.common.base.Throwables;
22+
3023
import org.openqa.selenium.WebDriverException;
3124
import org.openqa.selenium.remote.Command;
32-
import org.openqa.selenium.remote.CommandCodec;
33-
import org.openqa.selenium.remote.CommandExecutor;
34-
import org.openqa.selenium.remote.Dialect;
25+
import org.openqa.selenium.remote.CommandInfo;
3526
import org.openqa.selenium.remote.DriverCommand;
36-
import org.openqa.selenium.remote.HttpSessionId;
27+
import org.openqa.selenium.remote.HttpCommandExecutor;
3728
import org.openqa.selenium.remote.Response;
38-
import org.openqa.selenium.remote.ResponseCodec;
3929
import org.openqa.selenium.remote.http.HttpClient;
40-
import org.openqa.selenium.remote.http.HttpRequest;
41-
import org.openqa.selenium.remote.http.HttpResponse;
4230
import org.openqa.selenium.remote.internal.ApacheHttpClient;
4331
import org.openqa.selenium.remote.service.DriverService;
4432

@@ -47,124 +35,44 @@
4735
import java.net.URL;
4836
import java.util.Map;
4937

50-
public class AppiumCommandExecutor implements CommandExecutor {
38+
public class AppiumCommandExecutor extends HttpCommandExecutor {
5139

52-
private final URL remoteServer;
53-
private final HttpClient client;
54-
private final Map<String, AppiumCommandInfo> additionalCommands;
55-
private CommandCodec<HttpRequest> commandCodec;
56-
private ResponseCodec<HttpResponse> responseCodec;
57-
private DriverService service;
40+
private final DriverService service;
5841

59-
/**
60-
* Cretes an instance that sends requests and receives responses.
61-
*
62-
* @param additionalCommands is the mapped command repository
63-
* @param addressOfRemoteServer is the url to connect to the Appium remote/local server
64-
* @param httpClientFactory is the http client factory
65-
*/
66-
public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands,
67-
URL addressOfRemoteServer, HttpClient.Factory httpClientFactory) {
68-
checkNotNull(addressOfRemoteServer);
69-
remoteServer = addressOfRemoteServer;
70-
this.additionalCommands = additionalCommands;
71-
this.client = httpClientFactory.createClient(remoteServer);
42+
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
43+
URL addressOfRemoteServer, HttpClient.Factory httpClientFactory) {
44+
super(additionalCommands, addressOfRemoteServer, httpClientFactory);
45+
service = null;
7246
}
7347

74-
public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands, DriverService service,
75-
HttpClient.Factory httpClientFactory) {
76-
this(additionalCommands, service.getUrl(), httpClientFactory);
48+
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service,
49+
HttpClient.Factory httpClientFactory) {
50+
super(additionalCommands, service.getUrl(), httpClientFactory);
7751
this.service = service;
7852
}
7953

80-
public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands,
81-
URL addressOfRemoteServer) {
54+
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
55+
URL addressOfRemoteServer) {
8256
this(additionalCommands, addressOfRemoteServer, new ApacheHttpClient.Factory());
8357
}
8458

85-
public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands,
86-
DriverService service) {
59+
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
60+
DriverService service) {
8761
this(additionalCommands, service, new ApacheHttpClient.Factory());
8862
}
8963

90-
public URL getAddressOfRemoteServer() {
91-
return remoteServer;
92-
}
93-
94-
private Response doExecute(Command command) throws IOException, WebDriverException {
95-
if (command.getSessionId() == null) {
96-
if (QUIT.equals(command.getName())) {
97-
return new Response();
98-
}
99-
if (!GET_ALL_SESSIONS.equals(command.getName())
100-
&& !NEW_SESSION.equals(command.getName())) {
101-
throw new NoSuchSessionException(
102-
"Session ID is null. Using WebDriver after calling quit()?");
103-
}
104-
}
105-
106-
if (NEW_SESSION.equals(command.getName())) {
107-
if (commandCodec != null) {
108-
throw new SessionNotCreatedException("Session already exists");
109-
}
110-
AppiumProtocolHandShake handshake = new AppiumProtocolHandShake();
111-
AppiumProtocolHandShake.Result result = handshake.createSession(client, command);
112-
Dialect dialect = result.getDialect();
113-
commandCodec = dialect.getCommandCodec();
114-
115-
additionalCommands.forEach((key, value) -> {
116-
checkNotNull(key);
117-
checkNotNull(value);
118-
commandCodec.defineCommand(key, value.getMethod(), value.getUrl());
119-
} );
120-
121-
responseCodec = dialect.getResponseCodec();
122-
return result.createResponse();
123-
}
124-
125-
if (commandCodec == null || responseCodec == null) {
126-
throw new WebDriverException(
127-
"No command or response codec has been defined. Unable to proceed");
128-
}
129-
130-
HttpRequest httpRequest = commandCodec.encode(command);
131-
try {
132-
HttpResponse httpResponse = client.execute(httpRequest, true);
133-
134-
Response response = responseCodec.decode(httpResponse);
135-
if (response.getSessionId() == null) {
136-
if (httpResponse.getTargetHost() != null) {
137-
response.setSessionId(HttpSessionId.getSessionId(httpResponse.getTargetHost()));
138-
} else {
139-
response.setSessionId(command.getSessionId().toString());
140-
}
141-
}
142-
if (QUIT.equals(command.getName())) {
143-
client.close();
144-
}
145-
return response;
146-
} catch (UnsupportedCommandException e) {
147-
if (e.getMessage() == null || "".equals(e.getMessage())) {
148-
throw new UnsupportedOperationException(
149-
"No information from server. Command name was: " + command.getName(),
150-
e.getCause());
151-
}
152-
throw e;
153-
}
154-
}
155-
15664
@Override public Response execute(Command command) throws IOException, WebDriverException {
15765
if (DriverCommand.NEW_SESSION.equals(command.getName()) && service != null) {
15866
service.start();
15967
}
16068

16169
try {
162-
return doExecute(command);
70+
return super.execute(command);
16371
} catch (Throwable t) {
164-
Throwable rootCause = getRootCause(t);
72+
Throwable rootCause = Throwables.getRootCause(t);
16573
if (rootCause instanceof ConnectException
166-
&& rootCause.getMessage().contains("Connection refused")
167-
&& service != null) {
74+
&& rootCause.getMessage().contains("Connection refused")
75+
&& service != null) {
16876
if (service.isRunning()) {
16977
throw new WebDriverException("The session is closed!", t);
17078
}
@@ -182,4 +90,4 @@ private Response doExecute(Command command) throws IOException, WebDriverExcepti
18290
}
18391
}
18492

185-
}
93+
}

0 commit comments

Comments
 (0)