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");
            }
7
  • Well, what does the API say? Commented Jul 13, 2013 at 17:56
  • Well, this whole string isn't there, and the separate words are also not there, so it says nothing. But I dont know whether it reads it as a whole string or it is looking for all the words described... Commented Jul 13, 2013 at 18:03
  • Link to javadoc, please. What you seem to be unsure about is whether the content is treated as a string or a regex. Commented Jul 13, 2013 at 18:04
  • I dont know what you mean by "link to javadoc" and I agree about what you last said - thats my question. Commented Jul 13, 2013 at 18:08
  • I mean a link to that ExpectedConditions class Commented Jul 13, 2013 at 18:19

2 Answers 2

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:");
    
Sign up to request clarification or add additional context in comments.

1 Comment

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
-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");
}

1 Comment

How does this have anything to do woth regular expressions? This checks whether the <body> 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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.