The Issue
When we need to check that an exact string is found on a page or in the text of an element, we can use the ruby method ‘include?’. This is incorporated with our Watir Webdriver/Selenium scripts, and is commonly used in the last lines of a test script to validate that the script has reached a certain page, or produced a desired output. Lets take a look at an example of how to use ‘include?’.
The Code
1 2 3 4 5 6 7 8 9 |
target = 'Korea' puts puts("Include?: Does the story list contain \"#{target}\"?") stream = @browser.ul(:id, 'Stream') if stream.text.<strong>include?</strong>(target) puts("Found #{target}") else puts("No #{target} at the moment") end |
We place the include to check if there is any text that is a match for the defined target. If there is a match, we may see an output like this:
1 2 |
Include?; Does the story list contain "Korea"? Found Korea |
The Takeaway
The include? Ruby method is a commonly used, foundational tool that is essential to validation in your test automation. Become comfortable incorporating it into your Watir Webdriver/Selenium scripts, as you’ll soon find out it is used almost everywhere!