use of com.seleniumtests.connectors.selenium.fielddetector.Label 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.Label 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.Label 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.Label in project seleniumRobot by bhecquet.
the class TestImageFieldDetector method testDetectFieldsAndLabels.
/**
* Check detector is called only once event if we detect fields and labeld
* @throws IOException
*/
@Test(groups = { "ut" })
public void testDetectFieldsAndLabels() throws IOException {
JSONObject obj = new JSONObject("{" + " \"fields\": [{" + " \"class_id\": 3," + " \"top\": 674," + " \"bottom\": 696," + " \"left\": 20," + " \"right\": 141," + " \"class_name\": \"radio_with_label\"," + " \"text\": \"= Value 4\"," + " \"related_field\": null," + " \"with_label\": true," + " \"width\": 121," + " \"height\": 22" + " }," + " {" + " \"class_id\": 3," + " \"top\": 975," + " \"bottom\": 996," + " \"left\": 4," + " \"right\": 90," + " \"class_name\": \"radio_with_label\"," + " \"text\": \"an other text\"," + " \"related_field\": null," + " \"with_label\": true," + " \"width\": 86," + " \"height\": 21" + " }]," + " \"labels\": [{" + " \"top\": 24," + " \"left\": 8," + " \"width\": 244," + " \"height\": 16," + " \"text\": \"Test clicking a moving element\"," + " \"right\": 252," + " \"bottom\": 40" + " }," + " {" + " \"top\": 63," + " \"left\": 16," + " \"width\": 89," + " \"height\": 11," + " \"text\": \"Start Animation\"," + " \"right\": 105," + " \"bottom\": 74" + " }]" + "} ");
File image = createImageFromResource("ti/form_picture.png");
when(fieldDetectorConnector.detect(image, 1)).thenReturn(obj);
SeleniumTestsContextManager.getGlobalContext().setFieldDetectorInstance(fieldDetectorConnector);
ImageFieldDetector detector = new ImageFieldDetector(image);
List<Field> fields = detector.detectFields();
List<Label> labels = detector.detectLabels();
// detector is called only once
verify(fieldDetectorConnector).detect(image, 1);
}
use of com.seleniumtests.connectors.selenium.fielddetector.Label in project seleniumRobot by bhecquet.
the class TestImageFieldDetector method testDetectLabels.
@Test(groups = { "ut" })
public void testDetectLabels() throws IOException {
JSONObject obj = new JSONObject("{" + " \"fields\": [{" + " \"class_id\": 3," + " \"top\": 674," + " \"bottom\": 696," + " \"left\": 20," + " \"right\": 141," + " \"class_name\": \"radio_with_label\"," + " \"text\": \"= Value 4\"," + " \"related_field\": null," + " \"with_label\": true," + " \"width\": 121," + " \"height\": 22" + " }," + " {" + " \"class_id\": 3," + " \"top\": 975," + " \"bottom\": 996," + " \"left\": 4," + " \"right\": 90," + " \"class_name\": \"radio_with_label\"," + " \"text\": \"an other text\"," + " \"related_field\": null," + " \"with_label\": true," + " \"width\": 86," + " \"height\": 21" + " }]," + " \"labels\": [{" + " \"top\": 24," + " \"left\": 8," + " \"width\": 244," + " \"height\": 16," + " \"text\": \"Test clicking a moving element\"," + " \"right\": 252," + " \"bottom\": 40" + " }," + " {" + " \"top\": 63," + " \"left\": 16," + " \"width\": 89," + " \"height\": 11," + " \"text\": \"Start Animation\"," + " \"right\": 105," + " \"bottom\": 74" + " }]" + "} ");
File image = createImageFromResource("ti/form_picture.png");
when(fieldDetectorConnector.detect(image, 1)).thenReturn(obj);
SeleniumTestsContextManager.getGlobalContext().setFieldDetectorInstance(fieldDetectorConnector);
List<Label> labels = new ImageFieldDetector(image, 1, FieldType.ALL_FORM_FIELDS).detectLabels();
Assert.assertEquals(labels.size(), 2);
}
Aggregations