Search in sources :

Example 1 with Response

use of org.openqa.selenium.remote.Response in project java-client by appium.

the class AppiumDriver method getOrientation.

@Override
public ScreenOrientation getOrientation() {
    Response response = execute(DriverCommand.GET_SCREEN_ORIENTATION);
    String orientation = response.getValue().toString().toLowerCase();
    if (orientation.equals(ScreenOrientation.LANDSCAPE.value())) {
        return ScreenOrientation.LANDSCAPE;
    } else if (orientation.equals(ScreenOrientation.PORTRAIT.value())) {
        return ScreenOrientation.PORTRAIT;
    } else {
        throw new WebDriverException("Unexpected orientation returned: " + orientation);
    }
}
Also used : Response(org.openqa.selenium.remote.Response) WebDriverException(org.openqa.selenium.WebDriverException)

Example 2 with Response

use of org.openqa.selenium.remote.Response in project java-client by appium.

the class AppiumDriver method getContextHandles.

@Override
public Set<String> getContextHandles() {
    Response response = execute(DriverCommand.GET_CONTEXT_HANDLES);
    Object value = response.getValue();
    try {
        List<String> returnedValues = (List<String>) value;
        return new LinkedHashSet<>(returnedValues);
    } catch (ClassCastException ex) {
        throw new WebDriverException("Returned value cannot be converted to List<String>: " + value, ex);
    }
}
Also used : Response(org.openqa.selenium.remote.Response) LinkedHashSet(java.util.LinkedHashSet) List(java.util.List) WebDriverException(org.openqa.selenium.WebDriverException)

Example 3 with Response

use of org.openqa.selenium.remote.Response in project java-client by appium.

the class HasSettings method getSettings.

/**
 * Get settings stored for this test session It's probably better to use a
 * convenience function, rather than use this function directly. Try finding
 * the method for the specific setting you want to read.
 *
 * @return JsonObject, a straight-up hash of settings.
 */
@SuppressWarnings("unchecked")
default Map<String, Object> getSettings() {
    Map.Entry<String, Map<String, ?>> keyValuePair = getSettingsCommand();
    Response response = execute(keyValuePair.getKey(), keyValuePair.getValue());
    return ImmutableMap.<String, Object>builder().putAll(Map.class.cast(response.getValue())).build();
}
Also used : Response(org.openqa.selenium.remote.Response) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 4 with Response

use of org.openqa.selenium.remote.Response in project flow by vaadin.

the class ChromeDeviceTest method setConnectionType.

/**
 * Change network connection type in the browser.
 *
 * @param connectionType
 *            the new connection type
 * @throws IOException
 */
protected void setConnectionType(NetworkConnection.ConnectionType connectionType) throws IOException {
    RemoteWebDriver driver = (RemoteWebDriver) ((TestBenchDriverProxy) getDriver()).getWrappedDriver();
    final Map<String, Integer> parameters = new HashMap<>();
    parameters.put("type", connectionType.hashCode());
    final Map<String, Object> connectionParams = new HashMap<>();
    connectionParams.put("parameters", parameters);
    Response response = driver.getCommandExecutor().execute(new Command(driver.getSessionId(), "setNetworkConnection", connectionParams));
    if (response.getStatus() != 0) {
        throw new RuntimeException("Unable to set connection type");
    }
}
Also used : Response(org.openqa.selenium.remote.Response) HashMap(java.util.HashMap) Command(org.openqa.selenium.remote.Command) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver)

Example 5 with Response

use of org.openqa.selenium.remote.Response 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

Response (org.openqa.selenium.remote.Response)11 WebDriverException (org.openqa.selenium.WebDriverException)5 Supplier (com.google.common.base.Supplier)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 Map (java.util.Map)2 W3CHttpCommandCodec (org.openqa.selenium.remote.http.W3CHttpCommandCodec)2 DriverService (org.openqa.selenium.remote.service.DriverService)2 GET_SESSION (io.appium.java_client.MobileCommand.GET_SESSION)1 Activity (io.appium.java_client.android.Activity)1 StartsActivity (io.appium.java_client.android.StartsActivity)1 AppiumW3CHttpCommandCodec (io.appium.java_client.remote.AppiumW3CHttpCommandCodec)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Nullable (javax.annotation.Nullable)1 StringUtils.isBlank (org.apache.commons.lang3.StringUtils.isBlank)1