Search in sources :

Example 1 with JsonException

use of org.openqa.selenium.json.JsonException in project carina by qaprosoft.

the class EventFiringAppiumCommandExecutor method execute.

@Override
public Response execute(Command command) throws WebDriverException {
    if (DriverCommand.NEW_SESSION.equals(command.getName())) {
        serviceOptional.ifPresent(driverService -> {
            try {
                driverService.start();
            } catch (IOException e) {
                throw new WebDriverException(e);
            }
        });
    }
    Response response;
    try {
        for (IDriverCommandListener listener : listeners) {
            listener.beforeEvent(command);
        }
        try {
            response = super.execute(command);
        } catch (JsonException e) {
            // to handle potential grid/hub issue:
            // Expected to read a START_MAP but instead have: END. Last 0 characters read
            LOGGER.debug("Repeit the command due to the JsonException: " + command.getName(), e);
            CommonUtils.pause(0.1);
            response = super.execute(command);
        }
        for (IDriverCommandListener listener : listeners) {
            listener.afterEvent(command);
        }
    } catch (Throwable t) {
        Throwable rootCause = Throwables.getRootCause(t);
        if (rootCause instanceof ConnectException && rootCause.getMessage().contains("Connection refused")) {
            throw serviceOptional.map(service -> {
                if (service.isRunning()) {
                    return new WebDriverException("The session is closed!", rootCause);
                }
                return new WebDriverException("The appium server has accidentally died!", rootCause);
            }).orElseGet((Supplier<WebDriverException>) () -> new WebDriverException(rootCause.getMessage(), rootCause));
        }
        // throwIfUnchecked(t);
        throw new WebDriverException(t);
    } finally {
        if (DriverCommand.QUIT.equals(command.getName())) {
            serviceOptional.ifPresent(DriverService::stop);
        }
    }
    if (DriverCommand.NEW_SESSION.equals(command.getName()) && getCommandCodec() instanceof W3CHttpCommandCodec) {
        setCommandCodec(new AppiumW3CHttpCommandCodec());
        getAdditionalCommands().forEach(this::defineCommand);
    }
    return response;
}
Also used : Response(org.openqa.selenium.remote.Response) JsonException(org.openqa.selenium.json.JsonException) AppiumW3CHttpCommandCodec(io.appium.java_client.remote.AppiumW3CHttpCommandCodec) W3CHttpCommandCodec(org.openqa.selenium.remote.http.W3CHttpCommandCodec) AppiumW3CHttpCommandCodec(io.appium.java_client.remote.AppiumW3CHttpCommandCodec) Supplier(com.google.common.base.Supplier) IOException(java.io.IOException) WebDriverException(org.openqa.selenium.WebDriverException) ConnectException(java.net.ConnectException) DriverService(org.openqa.selenium.remote.service.DriverService)

Aggregations

Supplier (com.google.common.base.Supplier)1 AppiumW3CHttpCommandCodec (io.appium.java_client.remote.AppiumW3CHttpCommandCodec)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 WebDriverException (org.openqa.selenium.WebDriverException)1 JsonException (org.openqa.selenium.json.JsonException)1 Response (org.openqa.selenium.remote.Response)1 W3CHttpCommandCodec (org.openqa.selenium.remote.http.W3CHttpCommandCodec)1 DriverService (org.openqa.selenium.remote.service.DriverService)1