Skip to content

Commit

Permalink
issue #467: add details when we see that application changed
Browse files Browse the repository at this point in the history
  • Loading branch information
bhecquet committed Apr 6, 2022
1 parent 21fed7f commit 681800d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public List<ErrorCause> compareStepInErrorWithReference() {

// 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
int matching = compareReferenceToStepSnapshot(stepSnapshotFile, referenceSnapshot);
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) {
Expand All @@ -125,7 +127,7 @@ public List<ErrorCause> compareStepInErrorWithReference() {
// 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) {
causes.add(new ErrorCause(ErrorType.APPLICATION_CHANGED, null, testStep));
causes.add(new ErrorCause(ErrorType.APPLICATION_CHANGED, formatApplicationChangedDescription(missingLabels, missingFields), testStep));
}

// else, very good matching: we are on the same web page, error does not come from there
Expand All @@ -147,6 +149,24 @@ public List<ErrorCause> compareStepInErrorWithReference() {
return causes;
}

private String formatApplicationChangedDescription(List<Label> missingLabels, List<Field> missingFields) {
StringBuilder description = new StringBuilder();
if (!missingLabels.isEmpty()) {
description.append(String.format("%d Labels are missing: \n", missingLabels.size()));
for (Label label: missingLabels) {
description.append(label.getText() + "\n");
}
}
if (!missingFields.isEmpty()) {
description.append(String.format("%d fields are missing: \n", missingFields.size()));
for (Field field: missingFields) {
description.append(field.toString() + "\n");
}
}

return description.toString();
}

/**
* Search a test step before 'testStep' which is matching the current failed step
* e.g: we failed on step 3 so we search in 'step 2', then 'step 1' to see if one of them matches our 'step 3' visually
Expand Down Expand Up @@ -191,7 +211,16 @@ private void searchMatchingInPreviousStep(TestStepManager testStepManager, TestS
* @return an integer representing the matching (0: no matching, 100 very good matching)
*/
private int compareReferenceToStepSnapshot(File stepSnapshot, File referenceSnapshot) {
return new StepReferenceComparator(stepSnapshot, referenceSnapshot).compare();
return compareReferenceToStepSnapshot(stepSnapshot, referenceSnapshot, new ArrayList<>(), new ArrayList<>());
}
private int compareReferenceToStepSnapshot(File stepSnapshot, File referenceSnapshot, List<Label> missingLabels, List<Field> missingFields) {
StepReferenceComparator stepReferenceComparator = new StepReferenceComparator(stepSnapshot, referenceSnapshot);
int matching = stepReferenceComparator.compare();

missingLabels.addAll(stepReferenceComparator.getMissingLabels());
missingFields.addAll(stepReferenceComparator.getMissingFields());

return matching;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seleniumtests.util.imaging;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.seleniumtests.connectors.selenium.fielddetector.Field;
Expand All @@ -17,6 +18,8 @@ public class StepReferenceComparator {

private File stepSnapshot;
private File referenceSnapshot;
private List<Label> missingLabels = new ArrayList<>();
private List<Field> missingFields = new ArrayList<>();

public StepReferenceComparator(File stepSnapshot, File referenceSnapshot) {
this.stepSnapshot = stepSnapshot;
Expand All @@ -40,12 +43,16 @@ public int compare() {

int totalLabels = 0;
int matchedLabels = 0;

missingLabels = new ArrayList<>(referenceSnapshotLabels);
missingFields = new ArrayList<>(referenceSnapshotFields);

for (Label referenceSnapshotLabel: referenceSnapshotLabels) {

totalLabels++;
for (Label stepSnapshotLabel: stepSnapshotLabels) {
if (stepSnapshotLabel.match(referenceSnapshotLabel)) {
missingLabels.remove(referenceSnapshotLabel);
matchedLabels++;
break;
}
Expand All @@ -60,6 +67,7 @@ public int compare() {
totalFields++;
for (Field stepSnapshotField: stepSnapshotFields) {
if (stepSnapshotField.match(referenceSnapshotField)) {
missingFields.remove(referenceSnapshotField);
matchedFields++;
break;
}
Expand All @@ -72,4 +80,12 @@ public int compare() {

return 100 * (matchedFields + matchedLabels) / (totalFields + totalLabels);
}

public List<Label> getMissingLabels() {
return missingLabels;
}

public List<Field> getMissingFields() {
return missingFields;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,34 @@ public void testCompareStepInErrorWithReferenceMediumMatch() throws Exception {
// 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.getMissingFields()).thenReturn(Arrays.asList(new Field(0, 100, 0, 20, "", "field")));

List<ErrorCause> causes = new ErrorCauseFinder(testResult).compareStepInErrorWithReference();

Assert.assertEquals(causes.size(), 1);
Assert.assertEquals(causes.get(0).getType(), ErrorType.APPLICATION_CHANGED);
Assert.assertNull(causes.get(0).getDescription());
Assert.assertEquals(causes.get(0).getDescription(), "1 fields are missing: \n"
+ "field[text=]: java.awt.Rectangle[x=0,y=0,width=100,height=20]\n");
Assert.assertTrue(TestNGResultUtils.isErrorCauseSearchedInReferencePicture(testResult));
}
@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 Labels are missing: \n"
+ "some label\n");
Assert.assertTrue(TestNGResultUtils.isErrorCauseSearchedInReferencePicture(testResult));
}

Expand Down

0 comments on commit 681800d

Please sign in to comment.