Search in sources :

Example 1 with SessionNotCreatedException

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

the class AppiumCommandExecutor method createSession.

private Response createSession(Command command) throws IOException {
    if (getCommandCodec() != null) {
        throw new SessionNotCreatedException("Session already exists");
    }
    ProtocolHandshake.Result result = new AppiumProtocolHandshake().createSession(getClient().with((httpHandler) -> (req) -> {
        req.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase());
        return httpHandler.execute(req);
    }), command);
    Dialect dialect = result.getDialect();
    if (!(dialect.getCommandCodec() instanceof W3CHttpCommandCodec)) {
        throw new SessionNotCreatedException("Only W3C sessions are supported. " + "Please make sure your server is up to date.");
    }
    setCommandCodec(new AppiumW3CHttpCommandCodec());
    refreshAdditionalCommands();
    setResponseCodec(dialect.getResponseCodec());
    return result.createResponse();
}
Also used : CommandInfo(org.openqa.selenium.remote.CommandInfo) DriverService(org.openqa.selenium.remote.service.DriverService) W3CHttpCommandCodec(org.openqa.selenium.remote.codec.w3c.W3CHttpCommandCodec) URL(java.net.URL) WebDriverException(org.openqa.selenium.WebDriverException) Supplier(com.google.common.base.Supplier) Throwables.throwIfUnchecked(com.google.common.base.Throwables.throwIfUnchecked) NEW_SESSION(org.openqa.selenium.remote.DriverCommand.NEW_SESSION) HttpRequest(org.openqa.selenium.remote.http.HttpRequest) CommandCodec(org.openqa.selenium.remote.CommandCodec) Dialect(org.openqa.selenium.remote.Dialect) HttpResponse(org.openqa.selenium.remote.http.HttpResponse) Duration(java.time.Duration) Map(java.util.Map) ConnectException(java.net.ConnectException) ProtocolHandshake(org.openqa.selenium.remote.ProtocolHandshake) ClientConfig(org.openqa.selenium.remote.http.ClientConfig) DriverCommand(org.openqa.selenium.remote.DriverCommand) Response(org.openqa.selenium.remote.Response) Optional.ofNullable(java.util.Optional.ofNullable) HttpCommandExecutor(org.openqa.selenium.remote.HttpCommandExecutor) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Throwables(com.google.common.base.Throwables) Require(org.openqa.selenium.internal.Require) IOException(java.io.IOException) UUID(java.util.UUID) Field(java.lang.reflect.Field) HttpClient(org.openqa.selenium.remote.http.HttpClient) SessionNotCreatedException(org.openqa.selenium.SessionNotCreatedException) CommandExecutor(org.openqa.selenium.remote.CommandExecutor) Optional(java.util.Optional) Command(org.openqa.selenium.remote.Command) ResponseCodec(org.openqa.selenium.remote.ResponseCodec) ProtocolHandshake(org.openqa.selenium.remote.ProtocolHandshake) W3CHttpCommandCodec(org.openqa.selenium.remote.codec.w3c.W3CHttpCommandCodec) SessionNotCreatedException(org.openqa.selenium.SessionNotCreatedException) Dialect(org.openqa.selenium.remote.Dialect)

Example 2 with SessionNotCreatedException

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

the class AppiumDriver method startSession.

@Override
protected void startSession(Capabilities capabilities) {
    Response response = execute(new AppiumNewSessionCommandPayload(capabilities));
    if (response == null) {
        throw new SessionNotCreatedException("The underlying command executor returned a null response.");
    }
    Object responseValue = response.getValue();
    if (responseValue == null) {
        throw new SessionNotCreatedException("The underlying command executor returned a response without payload: " + response);
    }
    if (!(responseValue instanceof Map)) {
        throw new SessionNotCreatedException("The underlying command executor returned a response with a non well formed payload: " + response);
    }
    @SuppressWarnings("unchecked") Map<String, Object> rawCapabilities = (Map<String, Object>) responseValue;
    // A workaround for Selenium API enforcing some legacy capability values
    rawCapabilities.remove(CapabilityType.PLATFORM);
    if (rawCapabilities.containsKey(CapabilityType.BROWSER_NAME) && isBlank((String) rawCapabilities.get(CapabilityType.BROWSER_NAME))) {
        rawCapabilities.remove(CapabilityType.BROWSER_NAME);
    }
    MutableCapabilities returnedCapabilities = new BaseOptions<>(rawCapabilities);
    try {
        Field capsField = RemoteWebDriver.class.getDeclaredField("capabilities");
        capsField.setAccessible(true);
        capsField.set(this, returnedCapabilities);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new WebDriverException(e);
    }
    setSessionId(response.getSessionId());
}
Also used : SessionNotCreatedException(org.openqa.selenium.SessionNotCreatedException) AppiumNewSessionCommandPayload(io.appium.java_client.remote.AppiumNewSessionCommandPayload) Response(org.openqa.selenium.remote.Response) BaseOptions(io.appium.java_client.remote.options.BaseOptions) Field(java.lang.reflect.Field) MutableCapabilities(org.openqa.selenium.MutableCapabilities) Map(java.util.Map) WebDriverException(org.openqa.selenium.WebDriverException)

Example 3 with SessionNotCreatedException

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

the class BaseIOSWebViewTest method beforeClass.

@BeforeClass
public static void beforeClass() throws IOException {
    startAppiumServer();
    XCUITestOptions options = new XCUITestOptions().setDeviceName(DEVICE_NAME).setWdaLaunchTimeout(WDA_LAUNCH_TIMEOUT).setCommandTimeouts(Duration.ofSeconds(240)).setApp(vodQaAppZip().toAbsolutePath().toString());
    Supplier<IOSDriver> createDriver = () -> new IOSDriver(service.getUrl(), options);
    try {
        driver = createDriver.get();
    } catch (SessionNotCreatedException e) {
        // Sometimes WDA session creation freezes unexpectedly on CI:
        // https://dev.azure.com/srinivasansekar/java-client/_build/results?buildId=356&view=ms.vss-test-web.build-test-results-tab
        options.useNewWDA();
        driver = createDriver.get();
    }
}
Also used : XCUITestOptions(io.appium.java_client.ios.options.XCUITestOptions) SessionNotCreatedException(org.openqa.selenium.SessionNotCreatedException) BeforeClass(org.junit.BeforeClass)

Example 4 with SessionNotCreatedException

use of org.openqa.selenium.SessionNotCreatedException in project seleniumRobot by bhecquet.

the class SeleniumGridConnector method getSessionInformationFromGrid.

public void getSessionInformationFromGrid(RemoteWebDriver driver, long driverCreationDuration) {
    // logging node ip address:
    JSONObject object;
    try {
        object = Unirest.get(String.format("http://%s:%d%s", hubUrl.getHost(), hubUrl.getPort(), API_TEST_SESSSION)).queryString("session", driver.getSessionId().toString()).asJson().getBody().getObject();
    } catch (Exception e) {
        throw new SessionNotCreatedException(String.format("Could not get session information from grid: %s", e.getMessage()));
    }
    // if we have an error (session not found), raise an exception
    try {
        nodeUrl = (String) object.get("proxyId");
    } catch (JSONException e) {
        // },
        throw new SessionNotCreatedException(object.getString("msg"));
    }
    try {
        String node = nodeUrl.split("//")[1].split(":")[0];
        String browserName = driver.getCapabilities().getBrowserName();
        String version = driver.getCapabilities().getVersion();
        // if so, keep the previous sessionId so that recordings are correctly handled
        if (sessionId == null) {
            setSessionId(driver.getSessionId());
        }
        // store some information about driver creation
        MutableCapabilities caps = (MutableCapabilities) driver.getCapabilities();
        caps.setCapability(DriverUsage.GRID_HUB, hubUrl);
        caps.setCapability(DriverUsage.SESSION_ID, sessionId);
        caps.setCapability(DriverUsage.GRID_NODE, node);
        logger.info(String.format("Brower %s (%s) created in %.1f secs on node %s [%s] with session %s", browserName, version, driverCreationDuration / 1000.0, node, hubUrl, sessionId).replace(",", "."));
    } catch (Exception ex) {
        throw new SessionNotCreatedException(ex.getMessage());
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) MutableCapabilities(org.openqa.selenium.MutableCapabilities) SessionNotCreatedException(org.openqa.selenium.SessionNotCreatedException) JSONException(kong.unirest.json.JSONException) MalformedURLException(java.net.MalformedURLException) JSONException(kong.unirest.json.JSONException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) SessionNotCreatedException(org.openqa.selenium.SessionNotCreatedException) UnirestException(kong.unirest.UnirestException)

Example 5 with SessionNotCreatedException

use of org.openqa.selenium.SessionNotCreatedException in project seleniumRobot by bhecquet.

the class SeleniumGridDriverFactory method createWebDriver.

@Override
public WebDriver createWebDriver() {
    // create capabilities, specific to OS
    MutableCapabilities capabilities = createSpecificGridCapabilities(webDriverConfig);
    capabilities.merge(driverOptions);
    // upload file on all available grids as we don't know which one will be chosen before driver has been created
    for (SeleniumGridConnector gridConnector : gridConnectors) {
        gridConnector.uploadMobileApp(capabilities);
    }
    // connection to grid is made here
    for (int i = 0; i < 3; i++) {
        long start = new Date().getTime();
        driver = getDriver(capabilities);
        long duration = new Date().getTime() - start;
        setImplicitWaitTimeout(webDriverConfig.getImplicitWaitTimeout());
        if (webDriverConfig.getPageLoadTimeout() >= 0 && SeleniumTestsContextManager.isWebTest()) {
            setPageLoadTimeout(webDriverConfig.getPageLoadTimeout());
        }
        this.setWebDriver(driver);
        // if session has not been really created, we retry
        try {
            activeGridConnector.getSessionInformationFromGrid((RemoteWebDriver) driver, duration);
        } catch (SessionNotCreatedException e) {
            logger.error(e.getMessage());
            continue;
        }
        // sets a file detector. This is only useful for remote drivers
        ((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
        // create a BrowserInfo based on information get from grid hub
        selectedBrowserInfo = new BrowserInfo(BrowserType.getBrowserTypeFromSeleniumBrowserType(((RemoteWebDriver) driver).getCapabilities().getBrowserName()), ((RemoteWebDriver) driver).getCapabilities().getVersion());
        return driver;
    }
    throw new SessionNotCreatedException("Session not created on any grid hub, after 3 tries");
}
Also used : LocalFileDetector(org.openqa.selenium.remote.LocalFileDetector) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) MutableCapabilities(org.openqa.selenium.MutableCapabilities) SessionNotCreatedException(org.openqa.selenium.SessionNotCreatedException) SeleniumGridConnector(com.seleniumtests.connectors.selenium.SeleniumGridConnector) Date(java.util.Date)

Aggregations

SessionNotCreatedException (org.openqa.selenium.SessionNotCreatedException)8 MutableCapabilities (org.openqa.selenium.MutableCapabilities)3 XCUITestOptions (io.appium.java_client.ios.options.XCUITestOptions)2 File (java.io.File)2 Field (java.lang.reflect.Field)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BeforeClass (org.junit.BeforeClass)2 WebDriverException (org.openqa.selenium.WebDriverException)2 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)2 ChromeOptions (org.openqa.selenium.chrome.ChromeOptions)2 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)2 Response (org.openqa.selenium.remote.Response)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Supplier (com.google.common.base.Supplier)1 Throwables (com.google.common.base.Throwables)1 Throwables.throwIfUnchecked (com.google.common.base.Throwables.throwIfUnchecked)1 SeleniumGridConnector (com.seleniumtests.connectors.selenium.SeleniumGridConnector)1 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)1