Best Cerberus-source code snippet using org.cerberus.engine.gwt.impl.ControlService.verifyElementNumericArrayContains
Source:ControlService.java
...954 mes = this.verifyElementTextArrayContains(actual, expected, isCaseSensitive);955 break;956 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICARRAYCONTAINS:957 //We use verifyStringArrayContains because it's the same behaviour. Difference is that here we retrieve array using json path or xpath958 mes = this.verifyElementNumericArrayContains(actual, expected);959 break;960 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICEQUAL:961 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICDIFFERENT:962 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICGREATER:963 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICGREATEROREQUAL:964 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICMINOR:965 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICMINOROREQUAL:966 double value1;967 try {968 value1 = Double.parseDouble(actual);969 } catch (NumberFormatException nfe) {970 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC);971 mes.resolveDescription("COND", control);972 mes.resolveDescription("STRINGVALUE", actual);973 return mes;974 }975 // We try to convert the strings value2 to numeric.976 double value2;977 try {978 value2 = Double.parseDouble(expected);979 } catch (NumberFormatException nfe) {980 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC);981 mes.resolveDescription("COND", control);982 mes.resolveDescription("STRINGVALUE", expected);983 return mes;984 }985 mes = checkNumericVerifyElement(control, value1, value2);986 break;987 }988 mes.resolveDescription("ELEMENT", path);989 mes.resolveDescription("ELEMENTVALUE", actual);990 mes.resolveDescription("VALUE", expected);991 mes.resolveDescription("CASESENSITIVE", caseSensitiveMessageValue(isCaseSensitive));992 return mes;993 }994 private MessageEvent verifyElementTextEqualCaseSensitiveCheck(String actual, String expected, String isCaseSensitive) {995 MessageEvent mes;996 if (ParameterParserUtil.parseBooleanParam(isCaseSensitive, false)) {997 mes = actual.equals(expected) ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTTEXTEQUAL) : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTTEXTEQUAL);998 } else {999 mes = actual.equalsIgnoreCase(expected) ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTTEXTEQUAL) : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTTEXTEQUAL);1000 }1001 return mes;1002 }1003 private MessageEvent verifyElementTextDifferentCaseSensitiveCheck(String actual, String expected, String isCaseSensitive) {1004 MessageEvent mes;1005 if (ParameterParserUtil.parseBooleanParam(isCaseSensitive, false)) {1006 mes = actual.equals(expected) ? new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTTEXTDIFFERENT) : new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTTEXTDIFFERENT);1007 } else {1008 mes = actual.equalsIgnoreCase(expected) ? new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTTEXTDIFFERENT) : new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTTEXTDIFFERENT);1009 }1010 return mes;1011 }1012 private MessageEvent verifyElementTextContainsCaseSensitiveCheck(String text, String textToSearch, String isCaseSensitive) {1013 MessageEvent mes;1014 if (ParameterParserUtil.parseBooleanParam(isCaseSensitive, false)) {1015 mes = text.contains(textToSearch) ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTTEXTCONTAINS) : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTTEXTCONTAINS);1016 } else {1017 mes = text.toLowerCase().contains(textToSearch.toLowerCase()) ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTTEXTCONTAINS) : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTTEXTCONTAINS);1018 }1019 return mes;1020 }1021 private MessageEvent checkNumericVerifyElement(String control, Double actual, Double expected) {1022 switch (control) {1023 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICEQUAL:1024 return actual.equals(expected)1025 ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTNUMERICEQUAL)1026 : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTNUMERICEQUAL);1027 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICDIFFERENT:1028 return !actual.equals(expected)1029 ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTNUMERICDIFFERENT)1030 : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTNUMERICDIFFERENT);1031 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICGREATER:1032 return actual > expected1033 ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTNUMERICGREATER)1034 : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTNUMERICGREATER);1035 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICGREATEROREQUAL:1036 return actual >= expected1037 ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTNUMERICGREATEROREQUAL)1038 : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTNUMERICGREATEROREQUAL);1039 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICMINOR:1040 return actual < expected1041 ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTNUMERICMINOR)1042 : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTNUMERICMINOR);1043 case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICMINOROREQUAL:1044 return actual <= expected1045 ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_ELEMENTNUMERICMINOROREQUAL)1046 : new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENTNUMERICMINOROREQUAL);1047 default:1048 MessageEvent mes = new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_APPLICATION);1049 return mes.resolveDescription("CONTROL", "checkNumericVerifyElement-" + control);1050 }1051 }1052 private MessageEvent verifyElementTextMatchRegex(TestCaseExecution tCExecution, String path, String regex) {1053 LOG.debug("Control: VerifyElementTextMatchRegex on: {} element against value: {}", path, regex);1054 MessageEvent mes;1055 String pathContent;1056 try {1057 Identifier identifier = identifierService.convertStringToIdentifier(path);1058 String applicationType = tCExecution.getAppTypeEngine();1059 // Get value from the path element according to the application type1060 if (Application.TYPE_GUI.equalsIgnoreCase(applicationType)1061 || Application.TYPE_APK.equalsIgnoreCase(applicationType)1062 || Application.TYPE_IPA.equalsIgnoreCase(applicationType)) {1063 pathContent = this.webdriverService.getValueFromHTML(tCExecution.getSession(), identifier);1064 } else if (Application.TYPE_SRV.equalsIgnoreCase(applicationType)) {1065 if (tCExecution.getLastServiceCalled() != null) {1066 String responseBody = tCExecution.getLastServiceCalled().getResponseHTTPBody();1067 switch (tCExecution.getLastServiceCalled().getResponseHTTPBodyContentType()) {1068 case AppService.RESPONSEHTTPBODYCONTENTTYPE_XML:1069 if (!xmlUnitService.isElementPresent(responseBody, path)) {1070 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_ELEMENT_NOSUCHELEMENT);1071 mes.resolveDescription("ELEMENT", path);1072 return mes;1073 }1074 String newPath = StringUtil.addSuffixIfNotAlready(path, "/text()");1075 pathContent = xmlUnitService.getFromXml(responseBody, newPath);1076 break;1077 case AppService.RESPONSEHTTPBODYCONTENTTYPE_JSON:1078 try {1079 pathContent = jsonService.getFromJson(responseBody, null, path);1080 } catch (Exception ex) {1081 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC);1082 mes.resolveDescription("ERROR", ex.toString());1083 return mes;1084 }1085 break;1086 default:1087 mes = new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_MESSAGETYPE);1088 mes.resolveDescription("TYPE", tCExecution.getLastServiceCalled().getResponseHTTPBodyContentType());1089 mes.resolveDescription("CONTROL", TestCaseStepActionControl.CONTROL_VERIFYELEMENTTEXTMATCHREGEX);1090 return mes;1091 }1092 // TODO Give the actual element found into the description.1093 } else {1094 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_NOOBJECTINMEMORY);1095 return mes;1096 }1097 } else {1098 mes = new MessageEvent(MessageEventEnum.CONTROL_NOTEXECUTED_NOTSUPPORTED_FOR_APPLICATION);1099 mes.resolveDescription("CONTROL", TestCaseStepActionControl.CONTROL_VERIFYELEMENTTEXTMATCHREGEX);1100 mes.resolveDescription("APPLICATIONTYPE", tCExecution.getAppTypeEngine());1101 return mes;1102 }1103 LOG.debug("Control: VerifyElementMatchRegex element: {} has value: {}", path, StringUtil.sanitize(pathContent));1104 if (path != null && pathContent != null) {1105 try {1106 Pattern pattern = Pattern.compile(regex);1107 Matcher matcher = pattern.matcher(pathContent);1108 if (matcher.find()) {1109 mes = new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_REGEXINELEMENT);1110 } else {1111 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_REGEXINELEMENT);1112 }1113 mes.resolveDescription("STRING1", path);1114 mes.resolveDescription("STRING2", StringUtil.sanitize(pathContent));1115 mes.resolveDescription("STRING3", regex);1116 } catch (PatternSyntaxException e) {1117 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_REGEXINELEMENT_INVALIDPATTERN);1118 mes.resolveDescription("PATTERN", regex);1119 mes.resolveDescription("ERROR", e.getMessage());1120 }1121 } else if (pathContent != null) {1122 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_REGEXINELEMENT_NULL);1123 } else {1124 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_REGEXINELEMENT_NO_SUCH_ELEMENT);1125 mes.resolveDescription("ELEMENT", path);1126 }1127 } catch (NoSuchElementException exception) {1128 LOG.debug(exception.toString());1129 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_REGEXINELEMENT_NO_SUCH_ELEMENT);1130 mes.resolveDescription("ELEMENT", path);1131 } catch (WebDriverException exception) {1132 return parseWebDriverException(exception);1133 }1134 return mes;1135 }1136 private MessageEvent verifyStringArrayContains(String array, String valueToSearch, String isCaseSensitive) {1137 MessageEvent mes;1138 try {1139 List<String> strings = StringUtil.convertStringToStringArray(array);1140 //When user choose case sensitive option1141 boolean isContained = ParameterParserUtil.parseBooleanParam(isCaseSensitive, false)1142 ? strings.stream().anyMatch(valueToSearch::equals)1143 : strings.stream().anyMatch(valueToSearch::equalsIgnoreCase);1144 mes = isContained1145 ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_STRINGARRAYCONTAINS)1146 : new MessageEvent(MessageEventEnum.CONTROL_FAILED_STRINGARRAYCONTAINS);1147 mes.resolveDescription("ELEMENT", array);1148 mes.resolveDescription("VALUE", valueToSearch);1149 mes.resolveDescription("CASESENSITIVE", caseSensitiveMessageValue(isCaseSensitive));1150 } catch (JsonProcessingException exception) {1151 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC);1152 mes.resolveDescription("ERROR", "Incorrect array structure.");1153 }1154 return mes;1155 }1156 private MessageEvent verifyNumericArrayContains(String array, String numberToSearch) {1157 MessageEvent mes;1158 try {1159 List<Double> doubles = StringUtil.convertStringToDoubleArray(array);1160 Double number = Double.parseDouble(numberToSearch);1161 mes = doubles.stream().anyMatch(number::equals)1162 ? new MessageEvent(MessageEventEnum.CONTROL_SUCCESS_NUMERICARRAYCONTAINS)1163 : new MessageEvent(MessageEventEnum.CONTROL_FAILED_NUMERICARRAYCONTAINS);1164 mes.resolveDescription("ELEMENT", array);1165 mes.resolveDescription("VALUE", numberToSearch);1166 } catch (JsonProcessingException exception) {1167 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC);1168 mes.resolveDescription("ERROR", "Incorrect array structure.");1169 } catch (NumberFormatException exception) {1170 mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_GENERIC)1171 .resolveDescription("ERROR", "Value to search must be a number and the array must be an array of numbers.");1172 }1173 return mes;1174 }1175 private MessageEvent verifyElementTextArrayContains(String array, String valueToSearch, String isCaseSensitive) {1176 //We use verifyStringArrayContains because it's the same behaviour. The difference is that here we have an array retrieved using json path or xpath.1177 MessageEvent mes = this.verifyStringArrayContains(array, valueToSearch, isCaseSensitive);1178 //Change the message event to adapt to this control.1179 if (!mes.getSource().equals(MessageEventEnum.CONTROL_FAILED_GENERIC)) {1180 if (mes.getCodeString().equals("OK")) {1181 mes.setDescription(MessageEventEnum.CONTROL_SUCCESS_ELEMENTTEXTARRAYCONTAINS.getDescription());1182 } else {1183 mes.setDescription(MessageEventEnum.CONTROL_FAILED_ELEMENTTEXTARRAYCONTAINS.getDescription());1184 }1185 }1186 return mes;1187 }1188 private MessageEvent verifyElementNumericArrayContains(String array, String numberToSearch) {1189 //We use verifyStringArrayContains because it's the same behaviour. The difference is that here we have an array retrieved using json path or xpath.1190 MessageEvent mes = this.verifyNumericArrayContains(array, numberToSearch);1191 //Change the message event to adapt to this control.1192 if (!mes.getSource().equals(MessageEventEnum.CONTROL_FAILED_GENERIC)) {1193 if (mes.getCodeString().equals("OK")) {1194 mes.setDescription(MessageEventEnum.CONTROL_SUCCESS_ELEMENTNUMERICARRAYCONTAINS.getDescription());1195 } else {1196 mes.setDescription(MessageEventEnum.CONTROL_FAILED_ELEMENTNUMERICARRAYCONTAINS.getDescription());1197 }1198 }1199 return mes;1200 }1201 private MessageEvent verifyTextInDialog(TestCaseExecution tCExecution, String property, String value) {1202 LOG.debug("Control: verifyTextInDialog against value: {}", value);...
verifyElementNumericArrayContains
Using AI Code Generation
1import org.cerberus.engine.gwt.impl.ControlService;2ControlService control = new ControlService();3String[] data = {"1","2","3","4","5","6","7","8","9","10"};4boolean result = control.verifyElementNumericArrayContains("9", data);5assert result == true;6log("verifyElementNumericArrayContains", result);7screenshot("verifyElementNumericArrayContains");8endTestCase();9setTestCase("verifyElementNumericArrayContains");10startTestCase();11setDescription("Verify if a numeric array contains a value");12log("verifyElementNumericArrayContains", "Verify if a numeric array contains a value");13screenshot("verifyElementNumericArrayContains");14String[] data = {"1","2","3","4","5","6","7","8","9","10"};15boolean result = control.verifyElementNumericArrayContains("11", data);16assert result == false;17log("verifyElementNumericArrayContains", result);18screenshot("verifyElementNumericArrayContains");19endTestCase();20setTestCase("verifyElementNumericArrayContains");21startTestCase();22setDescription("Verify if a numeric array contains a value");23log("verifyElementNumericArrayContains", "Verify if a numeric array contains a value");24screenshot("verifyElementNumericArrayContains");25String[] data = {"1","2","3","4","5","6","7","8","9","10"};26boolean result = control.verifyElementNumericArrayContains("1", data);27assert result == true;28log("verifyElementNumericArrayContains", result);29screenshot("verifyElementNumericArrayContains");30endTestCase();31setTestCase("verifyElementNumericArrayContains");32startTestCase();33setDescription("Verify if a numeric array contains a value");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!