diff --git a/core/src/main/java/com/seleniumtests/connectors/selenium/fielddetector/FieldDetectorConnector.java b/core/src/main/java/com/seleniumtests/connectors/selenium/fielddetector/FieldDetectorConnector.java index 5efccee1d..8485d66cd 100644 --- a/core/src/main/java/com/seleniumtests/connectors/selenium/fielddetector/FieldDetectorConnector.java +++ b/core/src/main/java/com/seleniumtests/connectors/selenium/fielddetector/FieldDetectorConnector.java @@ -1,8 +1,6 @@ package com.seleniumtests.connectors.selenium.fielddetector; import java.io.File; -import java.util.ArrayList; -import java.util.List; import com.seleniumtests.customexception.ConfigurationException; import com.seleniumtests.customexception.ScenarioException; @@ -24,13 +22,14 @@ public class FieldDetectorConnector { private String url; private static final String STATUS_URL = "/status"; private static final String DETECT_URL = "/detect"; + private static final String DETECT_ERROR_URL = "/detectError"; /** * * @param url URL of the service */ public FieldDetectorConnector(String url) { - this.url = url + DETECT_URL; + this.url = url; try { HttpResponse response = Unirest.get(url + STATUS_URL).asString(); @@ -42,10 +41,50 @@ public FieldDetectorConnector(String url) { } } + /** + * Detect fields and labels + * @param imageFile + * @return + */ public JSONObject detect(File imageFile) { return detect(imageFile, 1); } + + /** + * Detect fields and labels + * @param imageFile + * @return + */ public JSONObject detect(File imageFile, double resizeFactor) { + return detect(imageFile, resizeFactor, DETECT_URL); + } + + /** + * Detect error message and fieids in error + * @param imageFile + * @return + */ + public JSONObject detectError(File imageFile) { + return detectError(imageFile, 1); + } + + /** + * Detect error message and fieids in error + * @param imageFile + * @return + */ + public JSONObject detectError(File imageFile, double resizeFactor) { + return detect(imageFile, resizeFactor, DETECT_ERROR_URL); + } + + /** + * Send image to image field detector and retrieve the box and text + * @param imageFile + * @param resizeFactor + * @param urlPath + * @return + */ + private JSONObject detect(File imageFile, double resizeFactor, String urlPath) { if (imageFile == null) { throw new ScenarioException("Image file is null"); } @@ -53,7 +92,7 @@ public JSONObject detect(File imageFile, double resizeFactor) { throw new ScenarioException(String.format("Image file %s not found", imageFile.getAbsolutePath())); } - HttpResponse fieldDefinition = Unirest.post(url) + HttpResponse fieldDefinition = Unirest.post(url + urlPath) .field("resize", resizeFactor) .field("image", imageFile) .asJson(); @@ -69,3 +108,6 @@ public JSONObject detect(File imageFile, double resizeFactor) { } } + + + diff --git a/core/src/main/java/com/seleniumtests/connectors/selenium/fielddetector/ImageFieldDetector.java b/core/src/main/java/com/seleniumtests/connectors/selenium/fielddetector/ImageFieldDetector.java index f8ca062a5..c19dedf5a 100644 --- a/core/src/main/java/com/seleniumtests/connectors/selenium/fielddetector/ImageFieldDetector.java +++ b/core/src/main/java/com/seleniumtests/connectors/selenium/fielddetector/ImageFieldDetector.java @@ -25,34 +25,60 @@ public class ImageFieldDetector { private double resizeFactor; private FieldDetectorConnector fieldDetectorInstance; private JSONObject detectionJsonData; + private FieldType fieldTypeToDetect; + + public enum FieldType { + ALL_FORM_FIELDS, // all fields: radio, text fields, buttons, ... + ERROR_MESSAGES_AND_FIELDS; // error messages and fields in error + } public ImageFieldDetector(File image) { this(image, 1); } public ImageFieldDetector(File image, double resizeFactor) { + this(image, resizeFactor, FieldType.ALL_FORM_FIELDS); + } + + /** + * Initialize the image field detector + * Depending on fieldTypeToDetect, it will use either a yolo model or an other + * @param image the image to detect fields in + * @param resizeFactor resizing of input image. With a factor > 1, detection will be better but longer + * @param fieldTypeToDetect ALL_FORM_FIELDS: all fields: radio, text fields, buttons, ... + * ERROR_MESSAGE_AND_FIELDS: error messages and fields in error + */ + public ImageFieldDetector(File image, double resizeFactor, FieldType fieldTypeToDetect) { this.image = image; this.resizeFactor = resizeFactor; + this.fieldTypeToDetect = fieldTypeToDetect; fieldDetectorInstance = SeleniumTestsContextManager.getThreadContext().getFieldDetectorInstance(); if (fieldDetectorInstance == null) { throw new ConfigurationException("Image Field detector has not been properly configured"); } } + private void callDetector() { + if (detectionJsonData == null) { + if (fieldTypeToDetect == FieldType.ALL_FORM_FIELDS) { + detectionJsonData = fieldDetectorInstance.detect(image, resizeFactor); + } else if (fieldTypeToDetect == FieldType.ERROR_MESSAGES_AND_FIELDS) { + detectionJsonData = fieldDetectorInstance.detectError(image, resizeFactor); + } + } + } + /** * get list of fields for the image * if detection has not already been done, do it * @return */ public List detectFields() { - if (detectionJsonData == null) { - detectionJsonData = fieldDetectorInstance.detect(image, resizeFactor); - } + callDetector(); List fields = new ArrayList<>(); try { List jsonFields = detectionJsonData.getJSONArray("fields").toList(); - for (JSONObject jsonNode: jsonFields) { fields.add(Field.fromJson(jsonNode)); } @@ -69,9 +95,7 @@ public List detectFields() { * @return */ public List