Reputation: 561
I want to repeat the same process in Watir with (word) as a different variable without writing out the whole code again in my .rb file. So without having to write this:
website = somewebsite.com
word = someword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
word = someotherword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
word = anotherword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
how can I do this?
Thanks.
Upvotes: 1
Views: 440
Reputation: 6660
While you could hardcode the data in the test code, a better idea might be to make a more data driven test by putting the data that changes into a file like a spreadsheet, CSV, or XML file, then read the file in as you loop through (you may need to require a ruby gem for some file formats like excel or xml)
There's examples of this sort of thing on the watir examples page in the wiki
=-=-=-=-=-=-=
I'm about to go way beyond what your question asks, but since it would seem you are just getting started, I'd like to direct you in what I view is an example of a 'right direction'
Another option is to use a tool like Cucumber as a testing framework. This allows for you to drive your tests via an executable specification where you specify what the test should do using a plain language format. More importantly to this discussion, it makes it VERY easy to repeat the same scenario multiple times with different data. A 'scenario' for a program 'feature' written in Cucumber looks something like this
Scenario outline: The expected text is found on page
Given I navigate the browser to <webpage>
Then I should see <phrase> on the page
| webpage | phrase |
| bandershatch | vorpal sword |
| mobyDick | heart I stab at thee |
| wookie | walking carpet |
Each of the text steps (the lines starting with Given and Then) maps to coded steps which you write in ruby/watir, and the tool will loop through the set of steps for every row of data in the table (three times in this case) passing in the values from the table to the steps.
The code for a step ends up looking something like this
Then /^I should see "([^\"]*)" on the page$/ do |expected_phrase|
browser.text.should include expected_phrase
end
The .should method is similar to assert if you've used a unit test framework, and it basically tells the system to 'look for this to be true'. If the .should method fails then the tool reports that step as failing, and hence the scenario as failing on that row of data.
It's actually a very elegant system in many ways, with benefits throughout the organization (not just in test) and happens to be my favorite way of driving my tests, especially since it deals so well with repeating steps or scenarios with different data, something most of us end up doing a lot.
This blog posting does a GREAT job of detailing the process, from the start of working with the PO to define the steps, to creating a page object (abstraction layer making it WAY easier to update your tests if the developers alter ID's or Names etc) to coding up the actual cucumber steps using ruby/Watir.
Personally that's the direction I would head, if for no other reason than that it makes it easy to run tests in groups, and provides ready reporting of results.
Here is a great video of a session where Cucumber is explained in more depth. If you search that site for cucumber, you will find a bunch of good stuff showing how to best use the tool.
Upvotes: 3
Reputation: 561
better answers above but this is what i used in the end:
Array1 = [word1, word2, word3,]
for x in array1.each do
if browser.text.include?(x)
puts(x found on website)
else puts "not found"
end
end
Upvotes: 0
Reputation: 37517
The website is the same? Leave it outside the loop.
browser.goto(website)
content = browser.text
%w(some_word some_other_word another_word).each do |word|
puts(website) if content.include?(word)
end
If you want better performance, omit the loop altogether:
words = %w(some_word some_other_word another_word)
browser.goto(website)
puts website if browser.text.match(Regexp.union(words))
Upvotes: 6
Reputation: 66857
%w(some_word some_other_word another_word).each do |word|
browser.goto(website)
puts(website) if browser.text.include?(word)
end
Upvotes: 3