Example usage for org.openqa.selenium.remote Command Command

List of usage examples for org.openqa.selenium.remote Command Command

Introduction

In this page you can find the example usage for org.openqa.selenium.remote Command Command.

Prototype

public Command(SessionId sessionId, String name, Map<String, ?> parameters) 

Source Link

Usage

From source file:galen.api.server.GalenCommandExecutor.java

License:Apache License

/**
 * Executes the command received over the Thrift interface inside an instance of RemoteWebDriver.
 * @param sessionId WebDriver SessionId.
 * @param commandName Command name./* w ww  .j  av  a  2s.  c o m*/
 * @param params Command params.
 * @return an instance of {@link org.openqa.selenium.remote.Response}
 * @throws TException
 */
@Override
public Response execute(String sessionId, String commandName, String params) throws TException {
    Map<String, Object> paramsAsMap = fromJsonToStringObjectMap(params);
    if (commandName.equals(DriverCommand.NEW_SESSION)) {
        try {
            log.info("Setting up new WebDriver session");
            HashMap<String, Object> hashMap = extractDesiredCapabilities(paramsAsMap);
            WebDriver driver = new RemoteWebDriver(new URL(remoteServerAddress),
                    new DesiredCapabilities(hashMap));
            DriversPool.get().set(driver);
            return createSessionInitSuccessResponse(driver);
        } catch (MalformedURLException e) {
            log.error("Provided URL is malformed " + remoteServerAddress);
            return createSessionInitFailureResponse("Provided URL is malformed " + remoteServerAddress);
        } catch (UnreachableBrowserException e) {
            log.error("Could not reach browser at URL " + remoteServerAddress
                    + " check remote server is running.");
            return createSessionInitFailureResponse("Could not reach browser at URL " + remoteServerAddress
                    + " check remote server is running.");
        } catch (WebDriverException e) {
            throw new RemoteWebDriverException(e.getMessage());
        }
    }
    Command driverCommand = new Command(new SessionId(sessionId), handleCommandNameExceptions(commandName),
            paramsAsMap);
    try {
        log.info(format("Executing command %s for sessionId %s", commandName, sessionId));
        WebDriver driver = DriversPool.get().getBySessionId(sessionId);
        org.openqa.selenium.remote.Response response = null;
        if (driver instanceof RemoteWebDriver) {
            response = ((RemoteWebDriver) driver).getCommandExecutor().execute(driverCommand);
        }
        if (response == null) {
            return null;
        } else {
            if (commandName.equals(DriverCommand.QUIT)) {
                DriversPool.get().removeDriverBySessionId(sessionId);
            }
            ThriftValueWrapper valueWrapper = new ThriftValueWrapper(response.getValue());
            return new Response(valueWrapper.getValue(), valueWrapper.getContainedValues(),
                    response.getSessionId(), response.getStatus(), response.getState());
        }
    } catch (IOException ioe) {
        log.error(format("IOException while executing command %s: %s", commandName, ioe.toString()));
    } catch (WebDriverException wex) {
        log.error(format("WebDriverException while executing command %s: + %s", commandName, wex.toString()));
        throw new RemoteWebDriverException(wex.getMessage());
    }
    return null;
}