0

I'm using Watir WebDriver and test/unit with Firefox.

The following code works:

assert(@browser.text.include?("Company employees"))

Why does the following code not work?

assert(@browser.text.include?(/Company employees/))
OldGrantonian
  • 597
  • 1
  • 8
  • 23

2 Answers2

0

The String#include? method only accepts strings or characters. It does not accept regular expressions.

You can use either of the following to assert against the regexp:

assert(@browser.text.match(/Company employees/))
assert(@browser.text =~ /Company employees/)
assert(@browser.text[/Company employees/])

However, I think it is more clear (reading the code and the error message) if you use the assert_match:

assert_match(/Company employees/, @browser.text)
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • All three suggestions work. I agree that third suggestion is clearer. Forcing a failure is an easy way to print out all the text on the page :) – OldGrantonian Oct 17 '13 at 16:03
  • 1
    The recommendation is usually to check a specific element's text rather than the whole page. It reduces chances of false positives and saves your error message from printing the whole page. – Justin Ko Oct 17 '13 at 16:21
  • Agreed. For laziness, I used `text` for 6 asserts on a page. When I changed each `text` to each of the 6 specific elements, the asserts took only a fraction of the original time. – OldGrantonian Oct 18 '13 at 06:39
0

try assert(@browser.text.include?("/Company employees/"))

NIck F
  • 1
  • the / should be escaped by a preceding \ . This works for my include? tests with regular expressions – NIck F Jan 07 '15 at 20:25