use of com.seleniumtests.connectors.selenium.fielddetector.Label 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.connectors.selenium.fielddetector.Label 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.connectors.selenium.fielddetector.Label in project seleniumRobot by bhecquet.
the class TestErrorCauseFinder method testCompareStepInErrorWithReferenceMediumMatch2.
@Test(groups = { "ut" })
public void testCompareStepInErrorWithReferenceMediumMatch2() throws Exception {
ITestResult testResult = Reporter.getCurrentTestResult();
TestNGResultUtils.setSeleniumRobotTestContext(testResult, SeleniumTestsContextManager.getThreadContext());
SeleniumTestsContextManager.getThreadContext().getTestStepManager().setTestSteps(Arrays.asList(step1, stepFailed, lastStep));
// comparison successful
PowerMockito.whenNew(StepReferenceComparator.class).withArguments(new File(stepFailed.getSnapshots().get(0).getScreenshot().getFullImagePath()), referenceImgStep2).thenReturn(stepReferenceComparatorStep2);
when(stepReferenceComparatorStep2.compare()).thenReturn(50);
when(stepReferenceComparatorStep2.getMissingLabels()).thenReturn(Arrays.asList(new Label(0, 100, 20, 50, "some label")));
List<ErrorCause> causes = new ErrorCauseFinder(testResult).compareStepInErrorWithReference();
Assert.assertEquals(causes.size(), 1);
Assert.assertEquals(causes.get(0).getType(), ErrorType.APPLICATION_CHANGED);
Assert.assertEquals(causes.get(0).getDescription(), "1 Label(s) missing: \n" + "some label\n");
Assert.assertTrue(TestNGResultUtils.isErrorCauseSearchedInReferencePicture(testResult));
// check line has been drawn below the missing label
BufferedImage image = ImageIO.read(referenceImgStep2);
Assert.assertEquals(new Color(image.getRGB(0, 50)), Color.RED);
Assert.assertNotEquals(new Color(image.getRGB(0, 20)), Color.RED);
Assert.assertEquals(new Color(image.getRGB(100, 50)), Color.RED);
Assert.assertNotEquals(new Color(image.getRGB(100, 20)), Color.RED);
}
use of com.seleniumtests.connectors.selenium.fielddetector.Label in project seleniumRobot by bhecquet.
the class TestErrorCauseFinder method testSearchInLastStepErrorMessageWithFailedStep.
/**
* When a failed step is present, attach ErrorCause to this one instead of "Last Step"
* @throws Exception
*/
@Test(groups = { "ut" })
public void testSearchInLastStepErrorMessageWithFailedStep() throws Exception {
ITestResult testResult = Reporter.getCurrentTestResult();
TestNGResultUtils.setSeleniumRobotTestContext(testResult, SeleniumTestsContextManager.getThreadContext());
SeleniumTestsContextManager.getThreadContext().getTestStepManager().setTestSteps(Arrays.asList(step1, stepFailed, lastStep));
PowerMockito.whenNew(ImageFieldDetector.class).withArguments(new File(lastStep.getSnapshots().get(0).getScreenshot().getFullImagePath()), (double) 1, FieldType.ERROR_MESSAGES_AND_FIELDS).thenReturn(imageFieldDetector);
List<Label> labels = new ArrayList<>();
labels.add(new Label(0, 100, 20, 50, "il y a eu un gros problème"));
labels.add(new Label(0, 100, 100, 120, "some error"));
List<Field> fields = new ArrayList<>();
fields.add(new Field(0, 100, 0, 20, "", "field"));
fields.add(new Field(0, 100, 200, 220, "", "field"));
when(imageFieldDetector.detectFields()).thenReturn(fields);
when(imageFieldDetector.detectLabels()).thenReturn(labels);
List<ErrorCause> causes = new ErrorCauseFinder(testResult).findErrorInLastStepSnapshots();
Assert.assertEquals(causes.size(), 2);
Assert.assertEquals(causes.get(0).getType(), ErrorType.ERROR_MESSAGE);
Assert.assertEquals(causes.get(0).getDescription(), "il y a eu un gros problème");
// error cause is associated to the last failed step
Assert.assertEquals(causes.get(0).getTestStep(), stepFailed);
Assert.assertEquals(causes.get(1).getType(), ErrorType.ERROR_MESSAGE);
Assert.assertEquals(causes.get(1).getDescription(), "some error");
// error cause is associated to the last failed step
Assert.assertEquals(causes.get(1).getTestStep(), stepFailed);
Assert.assertTrue(TestNGResultUtils.isErrorCauseSearchedInLastStep(testResult));
}
use of com.seleniumtests.connectors.selenium.fielddetector.Label in project seleniumRobot by bhecquet.
the class TestErrorCauseFinder method testSearchInLastStepAlreadyDone.
@Test(groups = { "ut" })
public void testSearchInLastStepAlreadyDone() throws Exception {
ITestResult testResult = Reporter.getCurrentTestResult();
TestNGResultUtils.setSeleniumRobotTestContext(testResult, SeleniumTestsContextManager.getThreadContext());
SeleniumTestsContextManager.getThreadContext().getTestStepManager().setTestSteps(Arrays.asList(step1, lastStep));
PowerMockito.whenNew(ImageFieldDetector.class).withArguments(new File(lastStep.getSnapshots().get(0).getScreenshot().getFullImagePath()), (double) 1, FieldType.ERROR_MESSAGES_AND_FIELDS).thenReturn(imageFieldDetector);
List<Label> labels = new ArrayList<>();
labels.add(new Label(0, 100, 20, 50, "il y a eu un gros problème"));
labels.add(new Label(0, 100, 100, 120, "some error"));
List<Field> fields = new ArrayList<>();
fields.add(new Field(0, 100, 0, 20, "", "field"));
fields.add(new Field(0, 100, 200, 220, "", "field"));
when(imageFieldDetector.detectFields()).thenReturn(fields);
when(imageFieldDetector.detectLabels()).thenReturn(labels);
TestNGResultUtils.setErrorCauseSearchedInLastStep(testResult, true);
List<ErrorCause> causes = new ErrorCauseFinder(testResult).findErrorInLastStepSnapshots();
Assert.assertEquals(causes.size(), 0);
}
Aggregations