use of com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector in project seleniumRobot by bhecquet.
the class ErrorCauseFinder method findErrorInLastStepSnapshots.
/**
* Search in snapshots of the last step if there are any displayed errors (error messages or fields in error)
* @return
*/
public List<ErrorCause> findErrorInLastStepSnapshots() {
logger.info("Searching causes: find errors in last snapshot");
List<ErrorCause> causes = new ArrayList<>();
// do not seearch again
if (TestNGResultUtils.isErrorCauseSearchedInLastStep(testResult)) {
return causes;
}
TestStep lastTestStep = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getLastTestStep();
TestStep lastFailedStep = lastTestStep;
for (TestStep testStep : TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getTestSteps()) {
if (Boolean.TRUE.equals(testStep.getFailed()) && testStep != lastTestStep) {
lastFailedStep = testStep;
}
}
if (lastTestStep != null) {
for (Snapshot snapshot : lastTestStep.getSnapshots()) {
try {
ImageFieldDetector imageFieldDetector = new ImageFieldDetector(new File(snapshot.getScreenshot().getFullImagePath()), 1, FieldType.ERROR_MESSAGES_AND_FIELDS);
List<Field> fields = imageFieldDetector.detectFields();
List<Label> labels = imageFieldDetector.detectLabels();
// are some text considered as error messages (mainly in red on page)
parseFields(causes, fields, labels, lastFailedStep);
// do some label contain "error" or "problem"
parseLabels(causes, labels, lastFailedStep);
} catch (Exception e) {
logger.error("Error searching for errors in last snapshots: " + e.getMessage());
break;
}
}
TestNGResultUtils.setErrorCauseSearchedInLastStep(testResult, true);
}
return causes;
}
use of com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector 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.ImageFieldDetector 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.ImageFieldDetector 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);
}
use of com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector 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);
}
Aggregations