Search in sources :

Example 1 with SeleniumRobotServerException

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

the class SeleniumRobotServerTestRecorder method recordSteps.

/**
 * Record test steps to server
 * @param serverConnector
 * @param sessionId
 * @param testCaseInSessionId
 * @param testSteps
 */
private void recordSteps(SeleniumRobotSnapshotServerConnector serverConnector, Integer sessionId, Integer testCaseInSessionId, List<TestStep> testSteps, ITestResult testResult) {
    for (TestStep testStep : testSteps) {
        logger.info(String.format("Recording step %s on server", testStep.getName()));
        // record test step
        Integer testStepId = serverConnector.createTestStep(testStep.getName(), testCaseInSessionId);
        String stepLogs = testStep.toJson().toString();
        Integer stepResultId = serverConnector.recordStepResult(!testStep.getFailed(), stepLogs, testStep.getDuration(), sessionId, testCaseInSessionId, testStepId);
        testStep.setStepResultId(stepResultId);
        // sends all snapshots that are flagged as comparable
        for (Snapshot snapshot : new ArrayList<>(testStep.getSnapshots())) {
            if (snapshot.getCheckSnapshot().recordSnapshotOnServerForComparison() && SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerCompareSnapshot()) {
                if (snapshot.getName() == null || snapshot.getName().isEmpty()) {
                    logger.warn("Snapshot hasn't any name, it won't be sent to server");
                    continue;
                }
                try {
                    Integer snapshotId = serverConnector.createSnapshot(snapshot, sessionId, testCaseInSessionId, stepResultId);
                    for (Rectangle excludeZone : snapshot.getCheckSnapshot().getExcludeElementsRect()) {
                        serverConnector.createExcludeZones(excludeZone, snapshotId);
                    }
                    logger.info("Check snapshot created");
                } catch (SeleniumRobotServerException e) {
                    logger.error("Could not create snapshot on server", e);
                }
            // record reference image on server if step is successful
            } else if (snapshot.getCheckSnapshot().recordSnapshotOnServerForReference() && SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerRecordResults()) {
                if (Boolean.FALSE.equals(testStep.getFailed())) {
                    try {
                        serverConnector.createStepReferenceSnapshot(snapshot, stepResultId);
                        logger.info("Step OK: reference created");
                    } catch (SeleniumRobotServerException e) {
                        logger.error("Could not create reference snapshot on server", e);
                    }
                    // remove this snapshot, extracted from video as it won't be used anymore
                    testStep.getSnapshots().remove(snapshot);
                } else {
                    try {
                        // move snapshot to "screenshots" directory as "video" directory will be removed at the end of the test
                        snapshot.relocate(TestNGResultUtils.getSeleniumRobotTestContext(testResult).getOutputDirectory(), ScreenshotUtil.SCREENSHOT_DIR + "/" + snapshot.getScreenshot().getImageName());
                        File referenceSnapshot = serverConnector.getReferenceSnapshot(stepResultId);
                        if (referenceSnapshot != null) {
                            logger.info("Step KO: reference snapshot got from server");
                            Path newPath = Paths.get(TestNGResultUtils.getSeleniumRobotTestContext(testResult).getScreenshotOutputDirectory(), referenceSnapshot.getName()).toAbsolutePath();
                            FileUtils.moveFile(referenceSnapshot, newPath.toFile());
                            testStep.addSnapshot(new Snapshot(new ScreenShot(newPath.getParent().getParent().relativize(newPath).toString()), "Valid-reference", SnapshotCheckType.FALSE), 0, null);
                            // change flag so that it's displayed in report (by default reference image extracted from video are not displayed)
                            snapshot.setDisplayInReport(true);
                        }
                    } catch (SeleniumRobotServerException e) {
                        logger.error("Could not get reference snapshot from server", e);
                    } catch (IOException e) {
                        logger.error("Could not copy reference snapshot", e);
                    }
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) ArrayList(java.util.ArrayList) Rectangle(org.openqa.selenium.Rectangle) IOException(java.io.IOException) File(java.io.File) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Example 2 with SeleniumRobotServerException

use of com.seleniumtests.customexception.SeleniumRobotServerException 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()));
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) UnirestException(kong.unirest.UnirestException) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Example 3 with SeleniumRobotServerException

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

the class SeleniumRobotServerConnector method createVersion.

/**
 * create version
 * If version name already exists on server, it's id will be returned. Else, a new one will be created
 */
public void createVersion() {
    if (!active) {
        return;
    }
    if (applicationId == null) {
        createApplication();
    }
    try {
        JSONObject versionJson = getJSonResponse(buildPostRequest(url + VERSION_API_URL).field(FIELD_NAME, SeleniumTestsContextManager.getApplicationVersion()).field("application", applicationId.toString()));
        versionId = versionJson.getInt("id");
    } catch (UnirestException | JSONException | SeleniumRobotServerException e) {
        throw new SeleniumRobotServerException("cannot create version", e);
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) UnirestException(kong.unirest.UnirestException) JSONException(kong.unirest.json.JSONException) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Example 4 with SeleniumRobotServerException

use of com.seleniumtests.customexception.SeleniumRobotServerException 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()));
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) UnirestException(kong.unirest.UnirestException) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Example 5 with SeleniumRobotServerException

use of com.seleniumtests.customexception.SeleniumRobotServerException 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;
    }
}
Also used : JSONObject(kong.unirest.json.JSONObject) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) BrowserType(com.seleniumtests.driver.BrowserType) UnirestException(kong.unirest.UnirestException) JSONException(kong.unirest.json.JSONException) File(java.io.File) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Aggregations

SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)19 UnirestException (kong.unirest.UnirestException)14 JSONException (kong.unirest.json.JSONException)11 JSONObject (kong.unirest.json.JSONObject)9 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)7 File (java.io.File)7 ArrayList (java.util.ArrayList)4 TestStep (com.seleniumtests.reporter.logger.TestStep)3 SeleniumRobotSnapshotServerConnector (com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector)2 BrowserType (com.seleniumtests.driver.BrowserType)2 Snapshot (com.seleniumtests.reporter.logger.Snapshot)2 IOException (java.io.IOException)2 GetRequest (kong.unirest.GetRequest)2 JSONArray (kong.unirest.json.JSONArray)2 GenericTest (com.seleniumtests.GenericTest)1 MockitoTest (com.seleniumtests.MockitoTest)1 SnapshotComparisonResult (com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector.SnapshotComparisonResult)1 TestVariable (com.seleniumtests.core.TestVariable)1 ErrorCause (com.seleniumtests.core.testanalysis.ErrorCause)1 ErrorCauseFinder (com.seleniumtests.core.testanalysis.ErrorCauseFinder)1