1
votes

I am using Capybara and Minitest for testing a rails 4 app. I have test lines like these that do work:

page.body.must_match(/word/)
page.body.must_match(/word2/)

but this does NOT work:

page.body.must_match(/(word).*(word2)/)

If I paste that regex into something like Regexr.com and regex101.com, that regex works perfectly against the page text being tested.

So is there some constraints as to what Regex is supported with Rails4/minitest? I really just want to confirm that word2 comes after word in the text. If this can't be done with regex is there a better way to do that in minitest?

1

1 Answers

1
votes

You don't explain what you mean by "does NOT work" but I'm going to assume you mean the regex doesn't match the page rather than you're getting an error about an invalid regex. If you're using any of the drivers in Capybara that support JS, then your failure is likely because the content isn't actually on the page when you're calling must_match and that is because you're using the wrong methods. To check for text on a page when using Capybara w/ minitest you should be using assert_text

page.assert_text(/word.*word2/)

When you call page.body.must_match... you're getting the body html into a static string and then checking for match on it once, whereas assert_text incorporates waiting/retrying behavior to handle pages that aren't finished loading, dynamic changes, etc.