1

I want to verify that certain text is not there, and I want to define that text by using a regular expression. I dont know how to insert the regex in the code, though, because I don't know if it picks up that what Im passing is a regex or it just assumes its a string of characters:

            if(wait.until(ExpectedConditions.not(ExpectedConditions.textToBePresentInElement(By.tagName("body"), "[Ee]rror:|[Ww]arning:|[Nn]otice:|[Ee]xception:"))))
            {
              System.out.println("Pass");
            }
            else
            {
              System.out.println("Fail");
            }
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

2 Answers2

3

You can't really do this, unfortunately. While Java does support regexpes very well, the WebDriver API does not. Every String parameter in the WebDriver API is taken literally, as a character string, not as a regexp.

You have probably confirmed this by running your code. And you have not missed anything, you can't use regexpes in searching for WebElements. If you're wondering why, the reason is that WebDriver has been primarily designed for test automation - you have a front-end test and you don't want to click everything in? Use WebDriver! Are you doing something else? Not sure if you got the right tool for it.

Reconsider your approach - why are you trying to match by a regexp? Are you running a website test? Are you not sure what should display there? Well, that's wrong! You should be testing whether you site is behaving like expected.

If you're testing after every page load whether the page contains some error message and you are sure you want to do this, consider a different approach. Can you look for a container with id="error-message" or something? Can you look at the URL or a page title whether it contains 404 not found or anything similar you need to assert?

What you can do?

  • Try not to search by text if you don't have to.
  • Dive into XPath 1.0 and see what it can do for you. While there are no regexp-enabling functions (those could be found in XPath 2.0, but we can't use that), there's a lot of strength in there. For example, there's no native lower-case() function, you can use translate(). Or simply use multiple contains() calls. And you can use | to union node-sets together. With these two, you could reconstruct your query with a looooooong XPath. But this is one of the last things you should do. It's bad. Very bad.
  • If you're still not convinced you shouldn't search for what you're searching, here's probably the easiest way. I'd still consider it bad since you don't know from where is the Error/Warning/Notice coming, some user might have "Warning: Bad user" as his username. But it does your thing.

    String bodyText = driver.findElement(By.tagName("body")).getText();
    boolean containsError = bodyText.matches("[Ee]rror:|[Ww]arning:|[Nn]otice:|[Ee]xception:");
    
Community
  • 1
  • 1
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • Hey, thank you a lot! Yes, I want to verify that when opening a page, no errors come up... And I can't figure another way of doing that. And Im willing to accept the risk for the user input probably providing one of these words... But what you said: "Can you look for a container with id="error-message" or something? Can you look at the URL or a page title whether it contains 404 not found or anything similar you need to assert?" is also a good approach, but I will have to do some research on the side handling and showing errors, before I implement that – Kaloyan Roussev Jul 16 '13 at 12:44
-1
String str1='[Ee]rror:|[Ww]arning:|[Nn]otice:|[Ee]xception:';

String str2=driver.findElement(By.tagname("body"));

if(str1.equals(str2))

{
     System.out.println("Fail");
}
else
{
    System.out.println("Pass");
}
  • How does this have anything to do woth regular expressions? This checks whether the `` text is _equal_ to a String "[Ee]rror:|[Ww]arning:|[Nn]otice:|[Ee]xception:". If you want to match the body text versus the regexp, you'd use `str2.matches(str1)` (see the last bit of my answer). – Petr Janeček Jul 15 '13 at 06:22