Skip to content

Commit

Permalink
Automatically retry driver instantiation in the case of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Apr 18, 2024
1 parent 24e5b8d commit 041d96f
Showing 1 changed file with 5 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public WebDriverFactory() {

public WebDriverFactory(EnvironmentVariables environmentVariables) {
this(environmentVariables,
SerenityInfrastructure.getFixtureProviderService(),
SerenityInfrastructure.getFixtureProviderService(),
SerenityInfrastructure.getCloseBrowser());
}

Expand Down Expand Up @@ -143,15 +143,7 @@ protected synchronized WebDriver newWebdriverInstance(final Class<? extends WebD
} catch (SerenityManagedException toPassThrough) {
throw toPassThrough;
} catch (Exception cause) {
if (shouldRetry(cause)) {
LOGGER.debug("Waiting to retry: " + cause.getMessage() + ")");
return waitThenRetry(driverClass, options, environmentVariables);
} else {
throw new DriverConfigurationError(
"WebDriver was unable to create a new instance of type " + driverClass + System.lineSeparator()
+ "WebDriver reported the following message: " + cause.getMessage() + System.lineSeparator()
+ "See below for more details.", cause);
}
return waitThenRetry(driverClass, options, environmentVariables);
}
}

Expand Down Expand Up @@ -190,7 +182,7 @@ private WebDriver waitThenRetry(int remainingTries,
if (remainingTries == 0) {
throw new DriverConfigurationError(
"After several attempts, could not instantiate new WebDriver instance of type " + driverClass +
" (" + cause.getMessage() + "). See below for more details.", cause);
" (" + cause.getMessage() + "). See below for more details.", cause);
}

PauseTestExecution.forADelayOf(30).seconds();
Expand All @@ -212,7 +204,7 @@ private boolean shouldRetry(Exception cause) {
.from(environmentVariables, "All parallel tests are currently in use"));
return RETRY_CAUSES.stream().anyMatch(
partialErrorMessage -> (cause != null) && (cause.getMessage() != null)
&& (cause.getMessage().contains(partialErrorMessage))
&& (cause.getMessage().contains(partialErrorMessage))
);
}

Expand All @@ -229,7 +221,7 @@ public static String getDriverFrom(EnvironmentVariables environmentVariables, St
}

public static String getDriverFrom(EnvironmentVariables environmentVariables) {
return EnvironmentSpecificConfiguration
return EnvironmentSpecificConfiguration
.from(environmentVariables)
.getOptionalProperty(ThucydidesSystemProperty.WEBDRIVER_DRIVER, DRIVER, WEBDRIVER_CAPABILITIES_BROWSERNAME)
.orElse(null);
Expand Down

0 comments on commit 041d96f

Please sign in to comment.