Search in sources :

Example 6 with Label

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);
}
Also used : Field(com.seleniumtests.connectors.selenium.fielddetector.Field) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) ArrayList(java.util.ArrayList) StepReferenceComparator(com.seleniumtests.util.imaging.StepReferenceComparator) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with Label

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);
}
Also used : Field(com.seleniumtests.connectors.selenium.fielddetector.Field) ImageFieldDetector(com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector) Label(com.seleniumtests.connectors.selenium.fielddetector.Label)

Example 8 with Label

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();
}
Also used : LocalDateTime(java.time.LocalDateTime) Field(com.seleniumtests.connectors.selenium.fielddetector.Field) ImageFieldDetector(com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector) Rectangle(java.awt.Rectangle) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) ScreenshotException(org.openqa.selenium.remote.ScreenshotException) Point(org.openqa.selenium.Point) File(java.io.File)

Example 9 with Label

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);
}
Also used : Field(com.seleniumtests.connectors.selenium.fielddetector.Field) JSONObject(kong.unirest.json.JSONObject) ImageFieldDetector(com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) File(java.io.File) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest)

Example 10 with Label

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);
}
Also used : JSONObject(kong.unirest.json.JSONObject) ImageFieldDetector(com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) File(java.io.File) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest)

Aggregations

Label (com.seleniumtests.connectors.selenium.fielddetector.Label)37 Test (org.testng.annotations.Test)31 GenericTest (com.seleniumtests.GenericTest)26 Field (com.seleniumtests.connectors.selenium.fielddetector.Field)22 File (java.io.File)14 MockitoTest (com.seleniumtests.MockitoTest)13 JSONObject (kong.unirest.json.JSONObject)13 ArrayList (java.util.ArrayList)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 ErrorCause (com.seleniumtests.core.testanalysis.ErrorCause)8 ErrorCauseFinder (com.seleniumtests.core.testanalysis.ErrorCauseFinder)8 ITestResult (org.testng.ITestResult)8 ImageFieldDetector (com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector)6 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)4 SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)2 Snapshot (com.seleniumtests.reporter.logger.Snapshot)2 TestStep (com.seleniumtests.reporter.logger.TestStep)2 StepReferenceComparator (com.seleniumtests.util.imaging.StepReferenceComparator)2 Rectangle (java.awt.Rectangle)2 BrowserInfo (com.seleniumtests.browserfactory.BrowserInfo)1