use of com.seleniumtests.connectors.selenium.fielddetector.Field in project seleniumRobot by bhecquet.
the class TestStepReferenceComparator method testComparePartialMatch.
@Test(groups = { "ut" })
public void testComparePartialMatch() throws Exception {
List<Label> stepLabels = new ArrayList<>();
stepLabels.add(new Label(0, 100, 20, 50, "a text"));
stepLabels.add(new Label(0, 100, 100, 120, "other text"));
List<Label> referenceLabels = new ArrayList<>();
referenceLabels.add(new Label(0, 100, 0, 20, "a text"));
referenceLabels.add(new Label(5, 105, 100, 120, "other text"));
List<Field> stepFields = new ArrayList<>();
stepFields.add(new Field(0, 100, 0, 20, "", "field"));
stepFields.add(new Field(0, 100, 200, 220, "", "field"));
List<Field> referenceFields = new ArrayList<>();
referenceFields.add(new Field(0, 100, 0, 20, "", "field"));
referenceFields.add(new Field(5, 105, 100, 120, "", "field"));
PowerMockito.whenNew(ImageFieldDetector.class).withAnyArguments().thenReturn(stepImageFieldDetector, refImageFieldDetector);
when(stepImageFieldDetector.detectLabels()).thenReturn(stepLabels);
when(refImageFieldDetector.detectLabels()).thenReturn(referenceLabels);
when(stepImageFieldDetector.detectFields()).thenReturn(stepFields);
when(refImageFieldDetector.detectFields()).thenReturn(referenceFields);
StepReferenceComparator comparator = new StepReferenceComparator(File.createTempFile("img", ".png"), File.createTempFile("img", ".png"));
Assert.assertEquals(comparator.compare(), 50);
}
use of com.seleniumtests.connectors.selenium.fielddetector.Field in project seleniumRobot by bhecquet.
the class TestStepReferenceComparator method testCompareNoLabel.
/**
* List of fields are the same, comparison is successful (no labels)
* @throws Exception
*/
@Test(groups = { "ut" })
public void testCompareNoLabel() throws Exception {
List<Field> stepFields = new ArrayList<>();
stepFields.add(new Field(0, 100, 0, 20, "", "field"));
stepFields.add(new Field(0, 100, 100, 120, "", "field"));
List<Field> referenceFields = new ArrayList<>();
referenceFields.add(new Field(0, 100, 0, 20, "", "field"));
referenceFields.add(new Field(5, 105, 100, 120, "", "field"));
PowerMockito.whenNew(ImageFieldDetector.class).withAnyArguments().thenReturn(stepImageFieldDetector, refImageFieldDetector);
when(stepImageFieldDetector.detectFields()).thenReturn(stepFields);
when(refImageFieldDetector.detectFields()).thenReturn(referenceFields);
StepReferenceComparator comparator = new StepReferenceComparator(File.createTempFile("img", ".png"), File.createTempFile("img", ".png"));
int matching = comparator.compare();
Assert.assertEquals(matching, 100);
PowerMockito.verifyNew(ImageFieldDetector.class, times(2)).withArguments(any(File.class), anyDouble(), eq(FieldType.ALL_FORM_FIELDS));
}
use of com.seleniumtests.connectors.selenium.fielddetector.Field in project seleniumRobot by bhecquet.
the class StepReferenceComparator method compare.
/**
* Compare each field of stepSnapshot with each field of the referenceSnapshot and compute a ratio
* A match is done on presence / position / text of fields between referenceSnapshot and stepSnapshot
* @return the ratio (100 means best matching, 0 no matching)
*/
public int compare() {
ImageFieldDetector stepSnapshotImageFieldDetector = new ImageFieldDetector(stepSnapshot, 1, FieldType.ALL_FORM_FIELDS);
List<Field> stepSnapshotFields = stepSnapshotImageFieldDetector.detectFields();
List<Label> stepSnapshotLabels = stepSnapshotImageFieldDetector.detectLabels();
ImageFieldDetector referenceSnapshotImageFieldDetector = new ImageFieldDetector(referenceSnapshot, 1, FieldType.ALL_FORM_FIELDS);
List<Field> referenceSnapshotFields = referenceSnapshotImageFieldDetector.detectFields();
List<Label> referenceSnapshotLabels = referenceSnapshotImageFieldDetector.detectLabels();
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;
}
}
}
int totalFields = 0;
int matchedFields = 0;
for (Field referenceSnapshotField : referenceSnapshotFields) {
totalFields++;
for (Field stepSnapshotField : stepSnapshotFields) {
if (stepSnapshotField.match(referenceSnapshotField)) {
missingFields.remove(referenceSnapshotField);
matchedFields++;
break;
}
}
}
if (totalFields + totalLabels == 0) {
return 100;
}
return 100 * (matchedFields + matchedLabels) / (totalFields + totalLabels);
}
use of com.seleniumtests.connectors.selenium.fielddetector.Field in project seleniumRobot by bhecquet.
the class UiElement method findElement.
public void findElement() {
LocalDateTime start = LocalDateTime.now();
checkElementToSearch();
if (resetSearch) {
resetPageInformation(origin);
}
// search fields if we do not have one for this page
if (!fieldsPerPage.containsKey(origin)) {
File screenshotFile = getScreenshotFile();
if (screenshotFile == null) {
throw new ScreenshotException("Screenshot does not exist");
}
// TODO: handle other cases than browser
// try to find viewport position so that we can match a position on browser capture with the same position on screen
// we assume that browser is started on main screen in a multi-screen environment
Rectangle viewportPosition = detectViewPortPosition(screenshotFile);
offsetPerPage.put(origin, new Point(viewportPosition.x, viewportPosition.y));
ImageFieldDetector detector = getImageFieldDetector(screenshotFile);
List<Field> fields = detector.detectFields();
for (Field field : fields) {
field.changePosition(viewportPosition.x, viewportPosition.y);
}
fieldsPerPage.put(origin, fields);
List<Label> labels = detector.detectLabels();
for (Label lbl : labels) {
lbl.changePosition(viewportPosition.x, viewportPosition.y);
}
labelsPerPage.put(origin, labels);
}
findElementByPosition();
actionDuration = Duration.between(start, LocalDateTime.now()).toMillis();
}
use of com.seleniumtests.connectors.selenium.fielddetector.Field in project seleniumRobot by bhecquet.
the class TestFieldDetectorConnector method testFieldDetection.
@Test(groups = "it", enabled = false)
public void testFieldDetection() throws IOException {
ImageFieldDetector imageFieldDetector;
try {
// imageFieldDetector = new ImageFieldDetector(createImageFromResource("tu/imageFieldDetection/browserCapture.png"), 0.75);
imageFieldDetector = new ImageFieldDetector(new File("D:\\Dev\\seleniumRobot\\seleniumRobot-core\\core\\test-output\\testSendKeysWithLabelOnTheLeft\\screenshots\\f5972501f7e893bfa3aa0281e4f647e1.png"), 0.75);
// imageFieldDetector = new ImageFieldDetector(createImageFromResource("ti/form_picture.png"), 0.75);
} catch (ConfigurationException e) {
throw new SkipException("no field detector server available");
}
List<Field> fields = imageFieldDetector.detectFields();
Assert.assertTrue(fields.size() > 10);
}
Aggregations