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

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

Introduction

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

Prototype

public SessionId(String opaqueKey) 

Source Link

Usage

From source file:com.thoughtworks.selenium.webdriven.WebDriverBackedSeleniumServlet.java

License:Apache License

private void startNewSession(HttpServletResponse resp, String browserString, String baseUrl, String options)
        throws IOException {
    SessionId sessionId = null;//from ww w .ja v  a  2s.  com

    if (options.startsWith("webdriver.remote.sessionid")) {
        // We may have a hit
        List<String> split = Splitter.on("=").omitEmptyStrings().trimResults().limit(2).splitToList(options);
        if (!"webdriver.remote.sessionid".equals(split.get(0))) {
            getServletContext()
                    .log("Unable to find existing webdriver session. Wrong parameter name: " + options);
            sendError(resp, "Unable to find existing webdriver session. Wrong parameter name: " + options);
            return;
        }
        if (split.size() != 2) {
            getServletContext().log("Attempted to find webdriver id, but none specified. Bailing");
            sendError(resp, "Unable to find existing webdriver session. No ID specified");
            return;
        }
        sessionId = new SessionId(split.get(1));
    }

    if (sessionId != null) {
        Session session = sessionsSupplier.get().get(sessionId);
        // Let's assume the person knew what they're doing... Skip to the end
    } else {
        // Fine. Let's see if the user chose "webdriver" or something specific.
        DesiredCapabilities caps;
        switch (browserString) {
        case "*webdriver":
            caps = new DesiredCapabilities();
            break;

        case "*chrome":
        case "*firefox":
        case "*firefoxproxy":
        case "*firefoxchrome":
        case "*pifirefox":
            caps = DesiredCapabilities.firefox();
            break;

        case "*iehta":
        case "*iexplore":
        case "*iexploreproxy":
        case "*piiexplore":
            caps = DesiredCapabilities.internetExplorer();
            break;

        case "*googlechrome":
            caps = DesiredCapabilities.chrome();
            break;

        case "*MicrosoftEdge":
            caps = DesiredCapabilities.edge();
            break;

        case "*opera":
        case "*operablink":
            caps = DesiredCapabilities.operaBlink();
            break;

        case "*safari":
        case "*safariproxy":
            caps = DesiredCapabilities.safari();
            break;

        default:
            sendError(resp, "Unable to match browser string: " + browserString);
            return;
        }

        try {
            sessionId = sessionsSupplier.get().newSession(caps);
        } catch (Exception e) {
            getServletContext().log("Unable to start session", e);
            sendError(resp,
                    "Unable to start session. Cause can be found in logs. Message is: " + e.getMessage());
            return;
        }
    }

    Session session = sessionsSupplier.get().get(sessionId);
    if (session == null) {
        getServletContext().log("Attempt to use non-existant session: " + sessionId);
        sendError(resp, "Attempt to use non-existant session: " + sessionId);
        return;
    }
    WebDriver driver = session.getDriver();
    CommandProcessor commandProcessor = new WebDriverCommandProcessor(baseUrl, driver);
    SESSIONS.put(sessionId, commandProcessor);
    sendResponse(resp, sessionId.toString());
}

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.//from  w w w .  j  a  v a  2 s.c om
 * @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;
}

From source file:org.jboss.arquillian.ajocado2.reusable.TestReusingBrowserSession.java

License:Apache License

@Test
public void whenBrowserSessionIsCreatedThenCouldBeReused() throws UnableReuseSessionException {

    RemoteWebDriver driver = new RemoteWebDriver(HUB_URL, DesiredCapabilities.firefox());
    driver.navigate().to(SERVER_URL.toString());
    Capabilities reusedCapabilities = serializeDeserialize(driver.getCapabilities());
    SessionId reusedSessionId = new SessionId(serializeDeserialize(driver.getSessionId().toString()));

    ReusableRemoteWebDriver reusedDriver = new ReusableRemoteWebDriver(HUB_URL, reusedCapabilities,
            reusedSessionId);/*  w w w.  j av  a2  s  .c  o  m*/
    reusedDriver.navigate().to(HUB_URL.toString());
    reusedDriver.quit();
}

From source file:org.jboss.arquillian.ajocado2.reusable.TestReusingBrowserSession.java

License:Apache License

@Test
public void whenBrowserSessionIsCreatedAndQuitAndTriedToReuseThenItShouldThrowException() {

    RemoteWebDriver driver = new RemoteWebDriver(HUB_URL, DesiredCapabilities.firefox());
    driver.navigate().to(SERVER_URL.toString());
    Capabilities reusedCapabilities = serializeDeserialize(driver.getCapabilities());
    SessionId reusedSessionId = new SessionId(serializeDeserialize(driver.getSessionId().toString()));
    driver.quit();/*from   w w w .  j  a  v  a2 s .  co  m*/

    try {
        new ReusableRemoteWebDriver(HUB_URL, reusedCapabilities, reusedSessionId);
        fail("Original driver had quited before, so session should not be reusable");
    } catch (UnableReuseSessionException e) {
        // exception should be thrown
    }
}

From source file:org.jboss.arquillian.drone.webdriver.factory.remote.reusable.ftest.TestReusableRemoteWebDriver.java

License:Apache License

@Test
public void whenBrowserIsCreatedThenCouldBeReused(@Drone @Reusable RemoteWebDriver driver)
        throws UnableReuseSessionException {

    driver.navigate().to(SERVER_URL.toString());
    Capabilities reusedCapabilities = serializeDeserialize(driver.getCapabilities());
    SessionId reusedSessionId = new SessionId(serializeDeserialize(driver.getSessionId().toString()));

    ReusableRemoteWebDriver reusedDriver = ReusableRemoteWebDriver.fromReusedSession(HUB_URL,
            reusedCapabilities, reusedSessionId);
    reusedDriver.navigate().to(HUB_URL.toString());
}

From source file:org.jboss.arquillian.drone.webdriver.factory.remote.reusable.ftest.TestReusableRemoteWebDriver.java

License:Apache License

@Test
public void whenBrowserIsCreatedAndQuitAndTriedToReuseThenItShouldThrowException(
        @Drone @Reusable RemoteWebDriver driver) {

    driver.navigate().to(SERVER_URL.toString());
    Capabilities reusedCapabilities = serializeDeserialize(driver.getCapabilities());
    SessionId reusedSessionId = new SessionId(serializeDeserialize(driver.getSessionId().toString()));
    driver.quit();/*from   www  .j a  va2s  .  com*/

    try {
        ReusableRemoteWebDriver.fromReusedSession(HUB_URL, reusedCapabilities, reusedSessionId);
        fail("Original driver had quited before, so session should not be reusable");
    } catch (UnableReuseSessionException e) {
        // exception should be thrown
    }
}

From source file:org.jboss.arquillian.drone.webdriver.factory.remote.reusable.ReusedSession.java

License:Apache License

public SessionId getSessionId() {
    return new SessionId(opaqueKey);
}

From source file:org.jboss.arquillian.drone.webdriver.factory.remote.reusable.TestReusableRemoteWebDriver.java

License:Apache License

@Test
public void whenBrowserIsCreatedThenCouldBeReused(@Drone @Reusable RemoteWebDriver driver)
        throws UnableReuseSessionException {

    driver.navigate().to(SERVER_URL.toString());

    new AugmentingEnhancer().deenhance(driver, Reusable.class); // without deenhancing we can't serialize capabilities

    Capabilities reusedCapabilities = serializeDeserialize(
            ReusedSession.createReusableCapabilities(driver.getCapabilities()));
    SessionId reusedSessionId = new SessionId(serializeDeserialize(driver.getSessionId().toString()));

    RemoteWebDriver reusedDriver = ReusableRemoteWebDriver.fromReusedSession(HUB_URL, reusedCapabilities,
            reusedSessionId);/*from w  ww.j ava 2s.  c o  m*/
    reusedDriver.navigate().to(HUB_URL.toString());
}

From source file:org.jboss.arquillian.drone.webdriver.factory.remote.reusable.TestReusableRemoteWebDriver.java

License:Apache License

@Test
public void whenBrowserIsCreatedAndQuitAndTriedToReuseThenItShouldThrowException(
        @Drone @Reusable RemoteWebDriver driver) {

    driver.navigate().to(SERVER_URL.toString());
    new AugmentingEnhancer().deenhance(driver, Reusable.class); // without deenhancing we can't serialize capabilities
    Capabilities reusedCapabilities = serializeDeserialize(
            ReusedSession.createReusableCapabilities(driver.getCapabilities()));
    SessionId reusedSessionId = new SessionId(serializeDeserialize(driver.getSessionId().toString()));
    driver.quit();//from   w ww  .ja va2 s.  c  o m

    try {
        ReusableRemoteWebDriver.fromReusedSession(HUB_URL, reusedCapabilities, reusedSessionId);
        fail("Original driver had quited before, so session should not be reusable");
    } catch (UnableReuseSessionException e) {
        // exception should be thrown
    }
}

From source file:org.jboss.arquillian.drone.webdriver.factory.remote.reusable.TestReusedSessionStoreImpl.java

License:Apache License

@Before
public void initialize() {
    URL url1;// ww w.j  av a  2s  .  c  om
    URL url2;
    try {
        url1 = new URL("http://localhost:8080/1/");
        url2 = new URL("http://localhost:8080/2/");
    } catch (MalformedURLException e) {
        throw new IllegalStateException(e);
    }
    DesiredCapabilities capabilities1 = new DesiredCapabilities();
    DesiredCapabilities capabilities2 = new DesiredCapabilities();
    SessionId sessionId1 = new SessionId("1");
    SessionId sessionId2 = new SessionId("2");

    key1 = new InitializationParameter(url1, capabilities1);
    key2 = new InitializationParameter(url2, capabilities2);
    session1 = new ReusedSession(sessionId1, capabilities1);
    session2 = new ReusedSession(sessionId2, capabilities2);
}