-
-
Notifications
You must be signed in to change notification settings - Fork 764
#594 FIX #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
#594 FIX #597
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
84b5dbd
#594 FIX:
shs96c b0ca0d9
#594 FIX:
TikhomirovSergey aaeee64
#594 FIX: AppiumCommandExecutor was reimplemented.
shs96c 4d062a5
#594 FIX: The final commit
TikhomirovSergey 2be5498
checkstyle issue fixes
TikhomirovSergey 0e6c04c
Work on remark.
TikhomirovSergey 02374da
AppiumProtocolHandShake code style improving (usage of Optional)
TikhomirovSergey b8bc1f0
Build issue fix.
TikhomirovSergey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/io/appium/java_client/AppiumCommandInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* 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 io.appium.java_client; | ||
|
||
import org.openqa.selenium.remote.CommandInfo; | ||
import org.openqa.selenium.remote.http.HttpMethod; | ||
|
||
public class AppiumCommandInfo extends CommandInfo { | ||
private final String url; | ||
private final HttpMethod method; | ||
|
||
/** | ||
* It conntains method and URL of the command. | ||
* | ||
* @param url command URL | ||
* @param method is http-method | ||
*/ | ||
public AppiumCommandInfo(String url, HttpMethod method) { | ||
super(url, method); | ||
this.url = url; | ||
this.method = method; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public HttpMethod getMethod() { | ||
return method; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,16 +16,29 @@ | |
|
||
package io.appium.java_client.remote; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.google.common.base.Throwables.getRootCause; | ||
import static com.google.common.base.Throwables.throwIfUnchecked; | ||
import static org.openqa.selenium.remote.DriverCommand.GET_ALL_SESSIONS; | ||
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION; | ||
import static org.openqa.selenium.remote.DriverCommand.QUIT; | ||
|
||
import com.google.common.base.Throwables; | ||
|
||
import io.appium.java_client.AppiumCommandInfo; | ||
import org.openqa.selenium.NoSuchSessionException; | ||
import org.openqa.selenium.SessionNotCreatedException; | ||
import org.openqa.selenium.UnsupportedCommandException; | ||
import org.openqa.selenium.WebDriverException; | ||
import org.openqa.selenium.remote.Command; | ||
import org.openqa.selenium.remote.CommandInfo; | ||
import org.openqa.selenium.remote.CommandCodec; | ||
import org.openqa.selenium.remote.CommandExecutor; | ||
import org.openqa.selenium.remote.Dialect; | ||
import org.openqa.selenium.remote.DriverCommand; | ||
import org.openqa.selenium.remote.HttpCommandExecutor; | ||
import org.openqa.selenium.remote.HttpSessionId; | ||
import org.openqa.selenium.remote.Response; | ||
import org.openqa.selenium.remote.ResponseCodec; | ||
import org.openqa.selenium.remote.http.HttpClient; | ||
import org.openqa.selenium.remote.http.HttpRequest; | ||
import org.openqa.selenium.remote.http.HttpResponse; | ||
import org.openqa.selenium.remote.internal.ApacheHttpClient; | ||
import org.openqa.selenium.remote.service.DriverService; | ||
|
||
|
@@ -34,41 +47,121 @@ | |
import java.net.URL; | ||
import java.util.Map; | ||
|
||
public class AppiumCommandExecutor extends HttpCommandExecutor { | ||
public class AppiumCommandExecutor implements CommandExecutor { | ||
|
||
private final DriverService service; | ||
private final URL remoteServer; | ||
private final HttpClient client; | ||
private final Map<String, AppiumCommandInfo> additionalCommands; | ||
private CommandCodec<HttpRequest> commandCodec; | ||
private ResponseCodec<HttpResponse> responseCodec; | ||
private DriverService service; | ||
|
||
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, | ||
/** | ||
* Cretes an instance that sends requests and receives responses. | ||
* | ||
* @param additionalCommands is the mapped command repository | ||
* @param addressOfRemoteServer is the url to connect to the Appium remote/local server | ||
* @param httpClientFactory is the http client factory | ||
*/ | ||
public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands, | ||
URL addressOfRemoteServer, HttpClient.Factory httpClientFactory) { | ||
super(additionalCommands, addressOfRemoteServer, httpClientFactory); | ||
service = null; | ||
checkNotNull(addressOfRemoteServer); | ||
remoteServer = addressOfRemoteServer; | ||
this.additionalCommands = additionalCommands; | ||
this.client = httpClientFactory.createClient(remoteServer); | ||
} | ||
|
||
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service, | ||
public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands, DriverService service, | ||
HttpClient.Factory httpClientFactory) { | ||
super(additionalCommands, service.getUrl(), httpClientFactory); | ||
this(additionalCommands, service.getUrl(), httpClientFactory); | ||
this.service = service; | ||
} | ||
|
||
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, | ||
public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands, | ||
URL addressOfRemoteServer) { | ||
this(additionalCommands, addressOfRemoteServer, new ApacheHttpClient.Factory()); | ||
} | ||
|
||
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, | ||
public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands, | ||
DriverService service) { | ||
this(additionalCommands, service, new ApacheHttpClient.Factory()); | ||
} | ||
|
||
public URL getAddressOfRemoteServer() { | ||
return remoteServer; | ||
} | ||
|
||
private Response doExecute(Command command) throws IOException, WebDriverException { | ||
if (command.getSessionId() == null) { | ||
if (QUIT.equals(command.getName())) { | ||
return new Response(); | ||
} | ||
if (!GET_ALL_SESSIONS.equals(command.getName()) | ||
&& !NEW_SESSION.equals(command.getName())) { | ||
throw new NoSuchSessionException( | ||
"Session ID is null. Using WebDriver after calling quit()?"); | ||
} | ||
} | ||
|
||
if (NEW_SESSION.equals(command.getName())) { | ||
if (commandCodec != null) { | ||
throw new SessionNotCreatedException("Session already exists"); | ||
} | ||
AppiumProtocolHandShake handshake = new AppiumProtocolHandShake(); | ||
AppiumProtocolHandShake.Result result = handshake.createSession(client, command); | ||
Dialect dialect = result.getDialect(); | ||
commandCodec = dialect.getCommandCodec(); | ||
|
||
additionalCommands.forEach((key, value) -> { | ||
checkNotNull(key); | ||
checkNotNull(value); | ||
commandCodec.defineCommand(key, value.getMethod(), value.getUrl()); | ||
} ); | ||
|
||
responseCodec = dialect.getResponseCodec(); | ||
return result.createResponse(); | ||
} | ||
|
||
if (commandCodec == null || responseCodec == null) { | ||
throw new WebDriverException( | ||
"No command or response codec has been defined. Unable to proceed"); | ||
} | ||
|
||
HttpRequest httpRequest = commandCodec.encode(command); | ||
try { | ||
HttpResponse httpResponse = client.execute(httpRequest, true); | ||
|
||
Response response = responseCodec.decode(httpResponse); | ||
if (response.getSessionId() == null) { | ||
if (httpResponse.getTargetHost() != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we assume, that httpResponse and response can never be equal to null? |
||
response.setSessionId(HttpSessionId.getSessionId(httpResponse.getTargetHost())); | ||
} else { | ||
response.setSessionId(command.getSessionId().toString()); | ||
} | ||
} | ||
if (QUIT.equals(command.getName())) { | ||
client.close(); | ||
} | ||
return response; | ||
} catch (UnsupportedCommandException e) { | ||
if (e.getMessage() == null || "".equals(e.getMessage())) { | ||
throw new UnsupportedOperationException( | ||
"No information from server. Command name was: " + command.getName(), | ||
e.getCause()); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
@Override public Response execute(Command command) throws IOException, WebDriverException { | ||
if (DriverCommand.NEW_SESSION.equals(command.getName()) && service != null) { | ||
service.start(); | ||
} | ||
|
||
try { | ||
return super.execute(command); | ||
return doExecute(command); | ||
} catch (Throwable t) { | ||
Throwable rootCause = Throwables.getRootCause(t); | ||
Throwable rootCause = getRootCause(t); | ||
if (rootCause instanceof ConnectException | ||
&& rootCause.getMessage().contains("Connection refused") | ||
&& service != null) { | ||
|
@@ -80,7 +173,7 @@ public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, | |
throw new WebDriverException("The appium server has accidentally died!", t); | ||
} | ||
} | ||
Throwables.propagateIfPossible(t); | ||
throwIfUnchecked(t); | ||
throw new WebDriverException(t); | ||
} finally { | ||
if (DriverCommand.QUIT.equals(command.getName()) && service != null) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't it be more useful to keep url as an URL object instead of just a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mykola-mokhnach
No. Take a look at the
https://github.com/appium/java-client/blob/master/src/main/java/io/appium/java_client/MobileCommand.java#L127
and
https://github.com/appium/java-client/blob/master/src/main/java/io/appium/java_client/MobileCommand.java#L197
I think that an URL like that can't be created by Java. It throws MalformedException at that case.