Home > other >  unable to validate the string using equalsIgnoreCase returning false
unable to validate the string using equalsIgnoreCase returning false

Time:09-24

this is how my data looks

expectedResult= Predictive
sourceVal = PEDP
actual value is  Enforcement Type Predictive PEDP

Here if i use equalsIgnoreCase it is failing because it couldn't match the string . Please suggest on how i can effectively validate the second and third column values

 public void validateKeyInfo_1(String expectedResult, String elementId, String sourceVal) {
    String actualValue = getElement(elementId).getText();
    boolean result = false;
    result = (expectedResult == null && StringUtils.isBlank(actualValue)) || (expectedResult != null && expectedResult.equalsIgnoreCase(actualValue));
    if (result) {
      scenario.log(LogStatus.INFO, "Value for element id : "   expectedResult   " is "   actualValue);
      if (actualValue.contains(sourceVal)) {
        scenario.log(LogStatus.INFO, "Value for source is : "   sourceVal   " is "   sourceVal);
      } else {
        reportFailure("Element value did not match with expected result");
      }
    } else {
      reportFailure("Element value did not match with expected result");
    }
  }

CodePudding user response:

I have bit modification and replaced that .isBlank method, while testing in my local I have hardcoded the values to replicate the same issue, Please try :

public static void validateKeyInfo_1(String expectedResult, String elementId, String sourceVal) {
    String actualValue = "Enforcement Type Predictive PEDP";
    expectedResult = "Predictive";
    boolean result = false;
    result = (expectedResult.trim() == null && actualValue.trim().isEmpty()) || (expectedResult.trim() != null && expectedResult.trim().equalsIgnoreCase(actualValue));
    if (result) {
      scenario.log(LogStatus.INFO, "Value for element id : "   expectedResult   " is "   actualValue);
      if (actualValue.contains(sourceVal)) {
        scenario.log(LogStatus.INFO, "Value for source is : "   sourceVal   " is "   sourceVal);
      } else {
        reportFailure("Element value did not match with expected result");
      }
    } else {
      reportFailure("Element value did not match with expected result");
    }
  }

basically this expectedResult.trim() == null && actualValue.trim().isEmpty() will help you go inside. Also please remove or comment these two lines while executing in your local.

String actualValue = "Enforcement Type Predictive PEDP";
expectedResult = "Predictive";

CodePudding user response:

Since actualValue is Enforcement Type Predictive PEDP and expectedResult= Predictive you definitely have to use contains() or containsIgnoreCase() methods, not equalsIgnoreCase() method and you should validate that actualValue contains expectedResult as following:

String actualValue = getElement(elementId).getText();
boolean result = false;
result = (expectedResult == null && StringUtils.isBlank(actualValue)) || (expectedResult != null && actualValue.containsIgnoreCase(expectedResult));
  • Related