use of com.seleniumtests.customexception.ConfigurationException in project seleniumRobot by bhecquet.
the class SeleniumRobotServerConnector method getEnvironmentId.
/**
* Returns the environment id in variable server, from the name of the tested env
* @throws ConfigurationException when environment does not exist to force user to register its variables
* @return
*/
public int getEnvironmentId() {
if (environmentId != null) {
return environmentId;
}
try {
JSONObject response = getJSonResponse(buildGetRequest(url + NAMED_ENVIRONMENT_API_URL).queryString(FIELD_NAME, SeleniumTestsContextManager.getGlobalContext().getTestEnv()));
environmentId = response.getInt("id");
return environmentId;
} catch (UnirestException | SeleniumRobotServerException e) {
throw new ConfigurationException(String.format("Environment %s not get from variable server: %s", SeleniumTestsContextManager.getGlobalContext().getTestEnv(), e.getMessage()));
}
}
use of com.seleniumtests.customexception.ConfigurationException in project seleniumRobot by bhecquet.
the class SeleniumRobotServerConnector method isAlive.
/**
* Test if the server is alive
* @param testUrl the URL to test for "alive". It MUST begin with "/" as root url only contains http://<host>:<port>
* @return
*/
protected boolean isAlive(String testUrl) {
try (UnirestInstance unirest = Unirest.spawnInstance()) {
HttpResponse<String> reply;
if (authToken != null) {
reply = unirest.get(url + testUrl).header(AUTHORIZATION_HEADER, authToken).asString();
} else {
reply = unirest.get(url + testUrl).asString();
}
int status = reply.getStatus();
// change base url and try again
if ((status == 308 || status == 301) && !url.toLowerCase().startsWith("https")) {
String newLocation = reply.getHeaders().getFirst("Location");
url = newLocation.replace(testUrl, "");
status = buildGetRequest(url + testUrl).asString().getStatus();
}
if (status == 401) {
logger.error("-------------------------------------------------------------------------------------");
logger.error("Access to seleniumRobot server unauthorized, access token must be provided");
logger.error("Token can be set via 'seleniumRobotServerToken' parameter: '-DseleniumRobotServerToken=<token>");
logger.error(String.format("Access token can be generated using your browser at %s/api-token-auth/?username=<user>&password=<password>", url));
logger.error("-------------------------------------------------------------------------------------");
throw new ConfigurationException("Access to seleniumRobot server unauthorized, access token must be provided by adding '-DseleniumRobotServerToken=<token>' to test command line");
} else {
return status == 200;
}
} catch (UnirestException e) {
return false;
}
}
use of com.seleniumtests.customexception.ConfigurationException in project seleniumRobot by bhecquet.
the class SeleniumRobotServerConnector method getApplicationId.
/**
* Returns the application id in variable server, from the name of the tested application
* @throws ConfigurationException when environment does not exist to force user to register its variables
* @return
*/
public int getApplicationId() {
if (applicationId != null) {
return applicationId;
}
try {
JSONObject response = getJSonResponse(buildGetRequest(url + NAMED_APPLICATION_API_URL).queryString(FIELD_NAME, SeleniumTestsContextManager.getApplicationName()));
applicationId = response.getInt("id");
return applicationId;
} catch (UnirestException | SeleniumRobotServerException e) {
throw new ConfigurationException(String.format("Application %s not get from variable server: %s", SeleniumTestsContextManager.getApplicationName(), e.getMessage()));
}
}
use of com.seleniumtests.customexception.ConfigurationException in project seleniumRobot by bhecquet.
the class SeleniumRobotSnapshotServerConnector method checkSnapshotHasNoDifferences.
/**
* Send snapshot to server, for comparison, and check there is no difference with the reference picture
* This method will return true if
* - comparison is OK
* It returns null if:
* - server is inactive
* - computing error occurred
* - server side error ( e.g: if the server is not up to date)
*/
public SnapshotComparisonResult checkSnapshotHasNoDifferences(Snapshot snapshot, String testName, String stepName) {
if (!active) {
return SnapshotComparisonResult.NOT_DONE;
}
if (testName == null) {
throw new ConfigurationException("testName must not be null");
}
if (stepName == null) {
throw new ConfigurationException("stepName must not be null");
}
if (snapshot == null || snapshot.getScreenshot() == null || snapshot.getScreenshot().getFullImagePath() == null) {
throw new SeleniumRobotServerException("Provided snapshot does not exist");
}
String snapshotName = snapshot.getName().length() > MAX_SNAPSHOT_NAME_LENGHT ? snapshot.getName().substring(0, MAX_SNAPSHOT_NAME_LENGHT) : snapshot.getName();
try {
File pictureFile = new File(snapshot.getScreenshot().getFullImagePath());
BrowserType browser = SeleniumTestsContextManager.getGlobalContext().getBrowser();
browser = browser == null ? BrowserType.NONE : browser;
String strippedTestName = getTestName(testName);
String strippedStepName = getTestStepName(stepName);
JSONObject snapshotJson = getJSonResponse(buildPutRequest(url + SNAPSHOT_API_URL).socketTimeout(5000).field(FIELD_IMAGE, pictureFile).field(FIELD_NAME, snapshotName).field("compare", snapshot.getCheckSnapshot().getName()).field("diffTolerance", String.valueOf(snapshot.getCheckSnapshot().getErrorThreshold())).field("versionId", versionId.toString()).field("environmentId", environmentId.toString()).field("browser", browser.getBrowserType()).field("testCaseName", strippedTestName).field("stepName", strippedStepName));
if (snapshotJson != null) {
String computingError = snapshotJson.getString(FIELD_COMPUTING_ERROR);
Float diffPixelPercentage = snapshotJson.getFloat("diffPixelPercentage");
Boolean tooManyDiffs = snapshotJson.getBoolean("tooManyDiffs");
if (!computingError.isEmpty()) {
return SnapshotComparisonResult.NOT_DONE;
} else if (Boolean.TRUE.equals(tooManyDiffs)) {
logger.error(String.format("Snapshot comparison for %s has a difference of %.2f%% with reference", snapshot.getName(), diffPixelPercentage));
return SnapshotComparisonResult.KO;
} else {
return SnapshotComparisonResult.OK;
}
} else {
return SnapshotComparisonResult.NOT_DONE;
}
} catch (UnirestException | JSONException | SeleniumRobotServerException e) {
// in case selenium server is not up to date, we shall not raise an error / retry
logger.error("cannot send snapshot to server", e);
return SnapshotComparisonResult.NOT_DONE;
}
}
use of com.seleniumtests.customexception.ConfigurationException in project seleniumRobot by bhecquet.
the class SeleniumGridConnectorFactory method getInstances.
/**
* Returns the list of available grid connectors
* From a list of grid URL, look for grids which are available at the time of request
* If all grids are available, returns all
* If none are available, wait for at least one to be there
* @param urls
* @return
*/
public static synchronized List<SeleniumGridConnector> getInstances(List<String> urls) {
if (urls.isEmpty()) {
throw new ConfigurationException("cannot create grid, no address provided");
}
// check addresses
List<URL> hubUrls = new ArrayList<>();
for (String url : urls) {
try {
hubUrls.add(new URL(url));
} catch (MalformedURLException e1) {
throw new ConfigurationException(String.format("Hub url '%s' is invalid: %s", url, e1.getMessage()));
}
}
Clock clock = Clock.systemUTC();
Instant end = clock.instant().plusSeconds(retryTimeout);
Exception currentException = null;
while (end.isAfter(clock.instant())) {
List<SeleniumGridConnector> seleniumGridConnectors = new ArrayList<>();
for (URL hubUrl : hubUrls) {
if (hubUrl.getHost().contains("browserstack")) {
seleniumGridConnectors.add(new BrowserStackGridConnector(hubUrl.toString()));
break;
}
// connect to console page to see if grid replies
try {
HttpResponse<String> response = Unirest.get(String.format("http://%s:%s%s", hubUrl.getHost(), hubUrl.getPort(), SeleniumGridConnector.CONSOLE_SERVLET)).asString();
if (response.getStatus() == 200) {
// try to connect to a SeleniumRobot grid specific servlet. If it replies, we are on a seleniumRobot grid
HttpResponse<String> responseGuiServlet = Unirest.get(String.format("http://%s:%s%s", hubUrl.getHost(), hubUrl.getPort(), SeleniumRobotGridConnector.GUI_SERVLET)).asString();
if (responseGuiServlet.getStatus() == 200) {
seleniumGridConnectors.add(new SeleniumRobotGridConnector(hubUrl.toString()));
} else {
seleniumGridConnectors.add(new SeleniumGridConnector(hubUrl.toString()));
}
} else {
logger.error("Cannot connect to the grid hub at " + hubUrl.toString());
}
} catch (Exception ex) {
WaitHelper.waitForMilliSeconds(500);
currentException = ex;
}
}
// if at least one hub replies, continue
if (!seleniumGridConnectors.isEmpty()) {
return seleniumGridConnectors;
}
}
throw new ConfigurationException("Cannot connect to the grid hubs at " + urls, currentException);
}
Aggregations