2
wait.until(ExpectedConditions.textToBePresentInElement(By.xpath("path"), "Text"));

The above command executes successfully only if the text in the locator is exactly same as "Text" but fails when the text in the locator is Case Insensitive . For Example Found text is "TEXT" or "tEXT" or etc...

I want my condition to compare it ignoring case sensitive. Any suggestions... please.

3

We have simple simple solution for this

Either you can convert your Expected variable and Actual variable to Lowercase or Uppercase before asserting them, so your code should like below.

String expectedValue = "abcd";
String actualValue = driver.findElement(By.xpath("your xpath")).getText().toLowerCase()

wait.until(ExpectedConditions.textToBePresentInElement(By.xpath("path"), actualValue));
  • 1
    How does this compare without case sensitivity? If the text in the "path" Element is anything other than lowercase it will return false and fail. – Paul Muir Sep 21 '16 at 11:05
3

The implementation of textToBePresentInElement is as follows:

  public static ExpectedCondition<Boolean> textToBePresentInElement(
    final WebElement element, final String text) {

    return new ExpectedCondition<Boolean>() {
      @Override
      public Boolean apply(WebDriver driver) {
        try {
          String elementText = element.getText();
          return elementText.contains(text);
        } catch (StaleElementReferenceException e) {
          return null;
        }
      }

      @Override
      public String toString() {
        return String.format("text ('%s') to be present in element %s", text, element);
      }
    };
  }

If you create a new implementation like this:

  public static ExpectedCondition<Boolean> loweredTextToBePresentInElement(
    final WebElement element, final String text) {

    return new ExpectedCondition<Boolean>() {
      @Override
      public Boolean apply(WebDriver driver) {
        try {
          String elementText = element.getText().toLowerCase();
          return elementText.contains(text.toLowerCase());
        } catch (StaleElementReferenceException e) {
          return null;
        }
      }

      @Override
      public String toString() {
        return String.format("text ('%s') to be present in element %s", text, element);
      }
    };
  }

And use the new implementation it will ignore the case of the text sent in and work as desired.

2

You can achieve your goal by converting text into small case first before comparing them.

For example, if you are using Python:

xpathString = "ABC"
newXpathString = (xpathString.lower()) //newXpathString = 'abc'

Your new script logic flow will be:

  • Convert TEXT, tEXT into text
  • Write your xpath expression with newly converted strings

But there IS a logical flaw here:

  • Xpath expression is supposed to be UNIQUE, converting texts into lower case would eliminate uniquess of a string; not to mention you are adding more lines to your code, why would you want to do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.