Search in sources :

Example 6 with Field

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);
}
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 Field

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

Example 8 with Field

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

Example 9 with Field

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();
}
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 10 with Field

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);
}
Also used : Field(com.seleniumtests.connectors.selenium.fielddetector.Field) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) ImageFieldDetector(com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector) SkipException(org.testng.SkipException) File(java.io.File) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest)

Aggregations

Field (com.seleniumtests.connectors.selenium.fielddetector.Field)35 Test (org.testng.annotations.Test)29 GenericTest (com.seleniumtests.GenericTest)23 Label (com.seleniumtests.connectors.selenium.fielddetector.Label)22 File (java.io.File)17 MockitoTest (com.seleniumtests.MockitoTest)14 JSONObject (kong.unirest.json.JSONObject)14 ArrayList (java.util.ArrayList)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 ImageFieldDetector (com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector)9 ErrorCause (com.seleniumtests.core.testanalysis.ErrorCause)8 ErrorCauseFinder (com.seleniumtests.core.testanalysis.ErrorCauseFinder)8 ITestResult (org.testng.ITestResult)8 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)5 Rectangle (java.awt.Rectangle)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 BrowserInfo (com.seleniumtests.browserfactory.BrowserInfo)1