- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 768
#655 FIX #682
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
      
      
            TikhomirovSergey
  merged 5 commits into
  appium:master
from
TikhomirovSergey:w3c_compatibility_issue_fix
  
      
      
   
  Aug 5, 2017 
      
    
  
     Merged
                    #655 FIX #682
Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      12cb9d9
              
                #655 FIX
              
              
                TikhomirovSergey f5ee595
              
                Improvement with optional
              
              
                TikhomirovSergey 5996661
              
                service field was turned into serviceOptional
              
              
                TikhomirovSergey b83b090
              
                check style issues were fixed
              
              
                TikhomirovSergey dd15bc0
              
                removal of the unnecessary casting
              
              
                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
    
  
  
    
              
  
    
      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,29 +16,19 @@ | |
|  | ||
| 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 static java.util.Optional.ofNullable; | ||
|  | ||
| import com.google.common.base.Function; | ||
| 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.CommandCodec; | ||
| import org.openqa.selenium.remote.CommandExecutor; | ||
| import org.openqa.selenium.remote.Dialect; | ||
| import org.openqa.selenium.remote.CommandInfo; | ||
| import org.openqa.selenium.remote.DriverCommand; | ||
| import org.openqa.selenium.remote.HttpSessionId; | ||
| import org.openqa.selenium.remote.HttpCommandExecutor; | ||
| 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; | ||
|  | ||
|  | @@ -47,131 +37,56 @@ | |
| import java.net.URL; | ||
| import java.util.Map; | ||
|  | ||
| public class AppiumCommandExecutor implements CommandExecutor { | ||
| public class AppiumCommandExecutor extends HttpCommandExecutor { | ||
|  | ||
| 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; | ||
| private final DriverService service; | ||
|  | ||
| /** | ||
| * 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) { | ||
| checkNotNull(addressOfRemoteServer); | ||
| remoteServer = addressOfRemoteServer; | ||
| this.additionalCommands = additionalCommands; | ||
| this.client = httpClientFactory.createClient(remoteServer); | ||
| public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, | ||
| URL addressOfRemoteServer, HttpClient.Factory httpClientFactory) { | ||
| super(additionalCommands, addressOfRemoteServer, httpClientFactory); | ||
| service = null; | ||
| } | ||
|  | ||
| public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands, DriverService service, | ||
| HttpClient.Factory httpClientFactory) { | ||
| this(additionalCommands, service.getUrl(), httpClientFactory); | ||
| public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service, | ||
| HttpClient.Factory httpClientFactory) { | ||
| super(additionalCommands, service.getUrl(), httpClientFactory); | ||
| this.service = service; | ||
| } | ||
|  | ||
| public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands, | ||
| URL addressOfRemoteServer) { | ||
| public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, | ||
| URL addressOfRemoteServer) { | ||
| this(additionalCommands, addressOfRemoteServer, new ApacheHttpClient.Factory()); | ||
| } | ||
|  | ||
| public AppiumCommandExecutor(Map<String, AppiumCommandInfo> additionalCommands, | ||
| DriverService service) { | ||
| public AppiumCommandExecutor(Map<String, CommandInfo> 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) { | ||
| response.setSessionId(HttpSessionId.getSessionId(httpResponse.getTargetHost())); | ||
| } else { | ||
| response.setSessionId(command.getSessionId().toString()); | ||
| @Override public Response execute(Command command) throws WebDriverException { | ||
| if (DriverCommand.NEW_SESSION.equals(command.getName())) { | ||
| ofNullable(service).ifPresent(driverService -> { | ||
|          | ||
| try { | ||
| driverService.start(); | ||
| } catch (IOException e) { | ||
| throw new WebDriverException(e.getMessage(), e); | ||
| } | ||
| } | ||
| 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 doExecute(command); | ||
| return super.execute(command); | ||
| } catch (Throwable t) { | ||
| Throwable rootCause = getRootCause(t); | ||
| Throwable rootCause = Throwables.getRootCause(t); | ||
| if (rootCause instanceof ConnectException | ||
| && rootCause.getMessage().contains("Connection refused") | ||
| && service != null) { | ||
| if (service.isRunning()) { | ||
| throw new WebDriverException("The session is closed!", t); | ||
| } | ||
|  | ||
| if (!service.isRunning()) { | ||
| throw new WebDriverException("The appium server has accidentally died!", t); | ||
| } | ||
| && rootCause.getMessage().contains("Connection refused")) { | ||
| throw ofNullable(service).map((Function<DriverService, WebDriverException>) service -> { | ||
| if (service.isRunning()) { | ||
| return new WebDriverException("The session is closed!", rootCause); | ||
| } | ||
|  | ||
| return new WebDriverException("The appium server has accidentally died!", rootCause); | ||
| }).orElse(new WebDriverException(rootCause.getMessage(), rootCause)); | ||
|          | ||
| } | ||
| throwIfUnchecked(t); | ||
| throw new WebDriverException(t); | ||
|  | @@ -182,4 +97,4 @@ private Response doExecute(Command command) throws IOException, WebDriverExcepti | |
| } | ||
| } | ||
|  | ||
| } | ||
| } | ||
      
      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.
can this be a reason of unexpected NPE?
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.
No. It is checked inside this method
https://github.com/TikhomirovSergey/java-client/blob/12cb9d9b6e117274340e8c73f40b0fa7a11396ba/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java#L64
Uh oh!
There was an error while loading. Please reload this page.
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.
then it might be useful to mark the property as Nullable
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.
or make it optional (I'd prefer this option)
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 ok. I will improve it soon.