Search in sources :

Example 1 with SeleniumGridNodeNotAvailable

use of com.seleniumtests.customexception.SeleniumGridNodeNotAvailable in project seleniumRobot by bhecquet.

the class SeleniumGridDriverFactory method getDriver.

/**
 * Connect to grid using RemoteWebDriver
 * As we may have several grid available, takes the first one where driver is created
 *
 * Several waits are defined
 * By default, we wait 30 mins for a node to be found. For this, we loop through all available hubs
 * In case we do not find any node after 30 mins, we fail and increment a fail counter
 * This fail counter is reset every time we find a node
 * If this counter reaches 3, then we don't even try to get a driver
 *
 * @param url
 * @param capability
 * @return
 */
private WebDriver getDriver(MutableCapabilities capability) {
    driver = null;
    Clock clock = Clock.systemUTC();
    Instant end = clock.instant().plusSeconds(instanceRetryTimeout);
    Exception currentException = null;
    if (webDriverConfig.getRunOnSameNode() != null && webDriverConfig.getSeleniumGridConnector() == null) {
        throw new ScenarioException("Cannot create a driver on the same node as an other driver if no previous driver has been created through grid");
    }
    // issue #311: stop after 3 consecutive failure getting nodes
    int noDriverCount = counter.get();
    if (noDriverCount > 2) {
        throw new SkipException("Skipping as the 3 previous tests could not get any matching node. Check your test configuration and grid setup");
    }
    while (end.isAfter(clock.instant())) {
        for (SeleniumGridConnector gridConnector : gridConnectors) {
            // if grid is not active, try the next one
            if (!gridConnector.isGridActive()) {
                logger.warn(String.format("grid %s is not active, looking for the next one", gridConnector.getHubUrl().toString()));
                continue;
            }
            // if we are launching a second driver for the same test, do it on the same hub
            if (webDriverConfig.getRunOnSameNode() != null && webDriverConfig.getSeleniumGridConnector() != gridConnector) {
                continue;
            }
            try {
                driver = new RemoteWebDriver(gridConnector.getHubUrl(), capability);
                activeGridConnector = gridConnector;
                break;
            } catch (WebDriverException e) {
                logger.warn(String.format("Error creating driver on hub %s: %s", gridConnector.getHubUrl().toString(), e.getMessage()));
                currentException = e;
            }
        }
        // do not wait more
        if (driver != null) {
            break;
        }
        if (currentException != null) {
            WaitHelper.waitForSeconds(5);
        } else {
            // we are here if no grid connector is available
            logger.warn("No grid available, wait 30 secs and retry");
            // for test only, reduce wiat
            if (instanceRetryTimeout > 30) {
                WaitHelper.waitForSeconds(30);
            } else {
                WaitHelper.waitForSeconds(1);
            }
        }
    }
    if (driver == null) {
        noDriverCount = counter.getAndIncrement();
        throw new SeleniumGridNodeNotAvailable(String.format("Cannot create driver on grid, it may be fully used [%d times]", noDriverCount), currentException);
    } else {
        // reset counter as we got a driver
        counter.set(0);
    }
    return driver;
}
Also used : RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) Instant(java.time.Instant) SkipException(org.testng.SkipException) Clock(java.time.Clock) SeleniumGridNodeNotAvailable(com.seleniumtests.customexception.SeleniumGridNodeNotAvailable) SeleniumGridConnector(com.seleniumtests.connectors.selenium.SeleniumGridConnector) SkipException(org.testng.SkipException) WebDriverException(org.openqa.selenium.WebDriverException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) SessionNotCreatedException(org.openqa.selenium.SessionNotCreatedException) ScenarioException(com.seleniumtests.customexception.ScenarioException) ScenarioException(com.seleniumtests.customexception.ScenarioException) WebDriverException(org.openqa.selenium.WebDriverException)

Aggregations

SeleniumGridConnector (com.seleniumtests.connectors.selenium.SeleniumGridConnector)1 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)1 ScenarioException (com.seleniumtests.customexception.ScenarioException)1 SeleniumGridNodeNotAvailable (com.seleniumtests.customexception.SeleniumGridNodeNotAvailable)1 Clock (java.time.Clock)1 Instant (java.time.Instant)1 SessionNotCreatedException (org.openqa.selenium.SessionNotCreatedException)1 WebDriverException (org.openqa.selenium.WebDriverException)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1 SkipException (org.testng.SkipException)1