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;
}
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;
}
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));
}
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>");
}
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));
}
Aggregations