diff --git a/source/src/main/java/org/cerberus/core/engine/gwt/impl/ControlService.java b/source/src/main/java/org/cerberus/core/engine/gwt/impl/ControlService.java index 278699069..95fa65335 100644 --- a/source/src/main/java/org/cerberus/core/engine/gwt/impl/ControlService.java +++ b/source/src/main/java/org/cerberus/core/engine/gwt/impl/ControlService.java @@ -568,7 +568,8 @@ private MessageEvent evaluateControlIfNumericXXX(String control, String controlV } catch (NumberFormatException nfe) { mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC); mes.resolveDescription("COND", control); - mes.resolveDescription("STRINGVALUE", newControlValue1); + mes.resolveDescription("NEWSTRING", newControlValue1); + mes.resolveDescription("STRINGVALUE", controlValue1); return mes; } @@ -579,7 +580,8 @@ private MessageEvent evaluateControlIfNumericXXX(String control, String controlV } catch (NumberFormatException nfe) { mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC); mes.resolveDescription("COND", control); - mes.resolveDescription("STRINGVALUE", newControlValue2); + mes.resolveDescription("NEWSTRING", newControlValue2); + mes.resolveDescription("STRINGVALUE", controlValue2); return mes; } @@ -1204,22 +1206,26 @@ private MessageEvent switchControl(String control, String path, String actual, S case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICMINOR: case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICMINOROREQUAL: double value1; + String actualCleaned = StringUtil.prepareToNumeric(actual); try { - value1 = Double.parseDouble(actual); + value1 = Double.parseDouble(actualCleaned); } catch (NumberFormatException nfe) { mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC); mes.resolveDescription("COND", control); + mes.resolveDescription("NEWSTRING", actualCleaned); mes.resolveDescription("STRINGVALUE", actual); return mes; } // We try to convert the strings value2 to numeric. double value2; + String expectedCleaned = StringUtil.prepareToNumeric(expected); try { - value2 = Double.parseDouble(expected); + value2 = Double.parseDouble(expectedCleaned); } catch (NumberFormatException nfe) { mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC); mes.resolveDescription("COND", control); + mes.resolveDescription("NEWSTRING", expectedCleaned); mes.resolveDescription("STRINGVALUE", expected); return mes; } diff --git a/source/src/main/java/org/cerberus/core/util/StringUtil.java b/source/src/main/java/org/cerberus/core/util/StringUtil.java index b191241c3..80315fcc6 100644 --- a/source/src/main/java/org/cerberus/core/util/StringUtil.java +++ b/source/src/main/java/org/cerberus/core/util/StringUtil.java @@ -131,10 +131,24 @@ public static boolean isBoolean(String str) { * get converted to float. For ex, it replace , with . */ public static String prepareToNumeric(String str) { - if (str.contains(",")) { - return str.replace(",", "."); + String result = str.replaceAll("[^0-9.,]", ""); + if (result.contains(",")) { + result = result.replace(",", "."); } - return str; + int i = 0; + while (nbChars(result, ".") > 1 && i++ < 100) { + result = result.replaceFirst("\\.", ""); + LOG.debug("replaced " + result); + } + LOG.debug("Cleaned string from {} to {}", str, result); + + return result; + } + + public static int nbChars(String str, String substr) { + LOG.debug(str.length() - str.replace(substr, "").length()); + return str.length() - str.replace(substr, "").length(); + } /** diff --git a/source/src/main/resources/documentation/D2/include/en/changelog_4.18_en.adoc b/source/src/main/resources/documentation/D2/include/en/changelog_4.18_en.adoc index 1f78d57b1..8178741a8 100644 --- a/source/src/main/resources/documentation/D2/include/en/changelog_4.18_en.adoc +++ b/source/src/main/resources/documentation/D2/include/en/changelog_4.18_en.adoc @@ -24,6 +24,7 @@ * Allow multiple extraParameters on robot chrome (space seperator). * Added %system.ROBOTSESSIONID% and %system.ROBOTPROVIDERSESSIONID% system variable. * Allow to have optional subdata and feed cerberus_testdatalib_subdataDefaultValue parameter to defined its default value (Thanks to Promod team ;-)). +* Make Numeric conversion more robust (ex : 'EUR 123 123.5 €' will be converted to 123123.5 before performing the control) *Warning to be considered before applying the version (deprecated features)* [square]