Search in sources :

Example 1 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class ErrorCauseFinder method compareStepInErrorWithReference.

/**
 * Compare the failed step with it's reference that can be found on seleniumRobot server
 * If reference cannot be found, skip this step
 * @return
 */
public List<ErrorCause> compareStepInErrorWithReference() {
    logger.info("Searching causes: comparing with references");
    List<ErrorCause> causes = new ArrayList<>();
    // do not seearch again
    if (TestNGResultUtils.isErrorCauseSearchedInReferencePicture(testResult)) {
        return causes;
    }
    // don't analyze if result has not been recorded on seleniumRobot server
    TestStepManager testStepManager = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager();
    if (testStepManager.getLastTestStep() == null || testStepManager.getLastTestStep().getStepResultId() == null) {
        return causes;
    }
    for (TestStep testStep : testStepManager.getTestSteps()) {
        // stepResultId is set when step recording is done on server
        Integer stepResultId = testStep.getStepResultId();
        if (Boolean.TRUE.equals(testStep.getFailed()) && !(testStep.getActionException() instanceof AssertionError) && stepResultId != null) {
            try {
                Snapshot stepSnapshot = testStep.getSnapshots().stream().filter(s -> s.getCheckSnapshot().recordSnapshotOnServerForReference()).collect(Collectors.toList()).get(0);
                File stepSnapshotFile = new File(stepSnapshot.getScreenshot().getFullImagePath());
                File referenceSnapshot = SeleniumRobotSnapshotServerConnector.getInstance().getReferenceSnapshot(stepResultId);
                if (referenceSnapshot == null) {
                    continue;
                }
                // perform a match between the picture of this step and the reference stored on server
                // We look at presence, position and text of each field
                List<Label> missingLabels = new ArrayList<>();
                List<Field> missingFields = new ArrayList<>();
                int matching = compareReferenceToStepSnapshot(stepSnapshotFile, referenceSnapshot, missingLabels, missingFields);
                // bad matching: the reference does not match at all the current step, we will check with other reference steps
                if (matching < 50) {
                    searchMatchingInPreviousStep(testStepManager, testStep, stepSnapshotFile, causes);
                // middle matching: we may be on the right web page but the page has changed (some fields appeared or disappeared)
                // or the text changed slightly. This could mean application changed
                } else if (matching < 90) {
                    // draw missing labels and fields
                    for (Label missingLabel : missingLabels) {
                        Rectangle rect = missingLabel.getRectangle();
                        Line2D.Double line = new Line2D.Double(rect.x, rect.y + rect.height, rect.x + rect.width, rect.y + rect.height);
                        ImageProcessor.drawLines(referenceSnapshot, Color.RED, line);
                    }
                    ImageProcessor.drawRectangles(referenceSnapshot, Color.RED, missingFields.stream().map(Field::getRectangle).collect(Collectors.toList()).toArray(new Rectangle[] {}));
                    causes.add(new ErrorCause(ErrorType.APPLICATION_CHANGED, formatApplicationChangedDescription(missingLabels, missingFields), testStep));
                }
                break;
            } catch (IndexOutOfBoundsException e) {
            // skip this step
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }
    TestNGResultUtils.setErrorCauseSearchedInReferencePicture(testResult, true);
    return causes;
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) TestStepManager(com.seleniumtests.core.TestStepManager) ArrayList(java.util.ArrayList) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) Rectangle(java.awt.Rectangle) Line2D(java.awt.geom.Line2D) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) Snapshot(com.seleniumtests.reporter.logger.Snapshot) Field(com.seleniumtests.connectors.selenium.fielddetector.Field) File(java.io.File)

Example 2 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class ErrorCauseFinder method findErrorInLastStepSnapshots.

/**
 * Search in snapshots of the last step if there are any displayed errors (error messages or fields in error)
 * @return
 */
public List<ErrorCause> findErrorInLastStepSnapshots() {
    logger.info("Searching causes: find errors in last snapshot");
    List<ErrorCause> causes = new ArrayList<>();
    // do not seearch again
    if (TestNGResultUtils.isErrorCauseSearchedInLastStep(testResult)) {
        return causes;
    }
    TestStep lastTestStep = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getLastTestStep();
    TestStep lastFailedStep = lastTestStep;
    for (TestStep testStep : TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getTestSteps()) {
        if (Boolean.TRUE.equals(testStep.getFailed()) && testStep != lastTestStep) {
            lastFailedStep = testStep;
        }
    }
    if (lastTestStep != null) {
        for (Snapshot snapshot : lastTestStep.getSnapshots()) {
            try {
                ImageFieldDetector imageFieldDetector = new ImageFieldDetector(new File(snapshot.getScreenshot().getFullImagePath()), 1, FieldType.ERROR_MESSAGES_AND_FIELDS);
                List<Field> fields = imageFieldDetector.detectFields();
                List<Label> labels = imageFieldDetector.detectLabels();
                // are some text considered as error messages (mainly in red on page)
                parseFields(causes, fields, labels, lastFailedStep);
                // do some label contain "error" or "problem"
                parseLabels(causes, labels, lastFailedStep);
            } catch (Exception e) {
                logger.error("Error searching for errors in last snapshots: " + e.getMessage());
                break;
            }
        }
        TestNGResultUtils.setErrorCauseSearchedInLastStep(testResult, true);
    }
    return causes;
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) Field(com.seleniumtests.connectors.selenium.fielddetector.Field) ImageFieldDetector(com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector) ArrayList(java.util.ArrayList) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) File(java.io.File) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException)

Example 3 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class TestTestLogging method testBuildScreenshotStringWithoutLocation.

@Test(groups = { "ut" })
public void testBuildScreenshotStringWithoutLocation() {
    ScreenShot screenshot = new ScreenShot();
    screenshot.setTitle("title");
    screenshot.setHtmlSourcePath("file.html");
    screenshot.setImagePath("file.png");
    Snapshot snapshotLogger = new Snapshot(screenshot, "main", SnapshotCheckType.TRUE);
    String screenshotStr = snapshotLogger.buildScreenshotLog().replace("\n", "").replaceAll(">\\s+<", "><");
    Matcher matcher = Pattern.compile(".*<img id\\=\"(.+)\" src\\=.*").matcher(screenshotStr);
    String imageId = "";
    if (matcher.matches()) {
        imageId = matcher.group(1);
    } else {
        throw new IndexOutOfBoundsException("no match");
    }
    Assert.assertEquals(screenshotStr, String.format("<div class=\"text-center\">" + "<a href=\"#\" onclick=\"$('#imagepreview').attr('src', $('#%s').attr('src'));$('#imagemodal').modal('show');\">" + "<img id=\"%s\" src=\"file.png\" style=\"width: 300px\">" + "</a>" + "</div>" + "<div class=\"text-center\">main: title</div>" + "<div class=\"text-center font-weight-lighter\"> | <a href='file.html' target=html>HTML Source</a></div>", imageId, imageId));
}
Also used : Snapshot(com.seleniumtests.reporter.logger.Snapshot) Matcher(java.util.regex.Matcher) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest)

Example 4 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class TestTestLogging method testBuildScreenshotStringWithoutInfo.

@Test(groups = { "ut" })
public void testBuildScreenshotStringWithoutInfo() {
    ScreenShot screenshot = new ScreenShot();
    Snapshot snapshotLogger = new Snapshot(screenshot, "main", SnapshotCheckType.FALSE);
    String screenshotStr = snapshotLogger.buildScreenshotLog().replace("\n", "");
    Assert.assertEquals(screenshotStr, "<div class=\"text-center\">main</div>" + "<div class=\"text-center font-weight-lighter\"></div>");
}
Also used : Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest)

Example 5 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class TestTestLogging method testBuildScreenshotStringWithoutSource.

@Test(groups = { "ut" })
public void testBuildScreenshotStringWithoutSource() {
    ScreenShot screenshot = new ScreenShot();
    screenshot.setTitle("title");
    screenshot.setLocation("http://location");
    screenshot.setImagePath("file.png");
    Snapshot snapshotLogger = new Snapshot(screenshot, "main", SnapshotCheckType.FALSE);
    String screenshotStr = snapshotLogger.buildScreenshotLog().replace("\n", "").replaceAll(">\\s+<", "><");
    Matcher matcher = Pattern.compile(".*<img id\\=\"(.+)\" src\\=.*").matcher(screenshotStr);
    String imageId = "";
    if (matcher.matches()) {
        imageId = matcher.group(1);
    } else {
        throw new IndexOutOfBoundsException("no match");
    }
    Assert.assertEquals(screenshotStr, String.format("<div class=\"text-center\">" + "<a href=\"#\" onclick=\"$('#imagepreview').attr('src', $('#%s').attr('src'));$('#imagemodal').modal('show');\">" + "<img id=\"%s\" src=\"file.png\" style=\"width: 300px\">" + "</a>" + "</div>" + "<div class=\"text-center\">main: title</div><div class=\"text-center font-weight-lighter\"><a href='http://location' target=url>URL</a></div>", imageId, imageId));
}
Also used : Snapshot(com.seleniumtests.reporter.logger.Snapshot) Matcher(java.util.regex.Matcher) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest)

Aggregations

Snapshot (com.seleniumtests.reporter.logger.Snapshot)29 ScreenShot (com.seleniumtests.driver.screenshots.ScreenShot)23 TestStep (com.seleniumtests.reporter.logger.TestStep)22 File (java.io.File)18 Test (org.testng.annotations.Test)18 GenericTest (com.seleniumtests.GenericTest)15 ArrayList (java.util.ArrayList)11 GenericFile (com.seleniumtests.reporter.logger.GenericFile)8 TestAction (com.seleniumtests.reporter.logger.TestAction)5 SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 Matcher (java.util.regex.Matcher)3 Field (com.seleniumtests.connectors.selenium.fielddetector.Field)2 Label (com.seleniumtests.connectors.selenium.fielddetector.Label)2 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Rectangle (org.openqa.selenium.Rectangle)2