We have n-number of divs on the page with the same class name.
Foo is invalid
It's easy to get bogged down with a complex solution. Start with general (even inefficient) steps first and then streamline the code once the basic solution is working. This will work in Watir 1.9.2:
@divs_with_error = [] #create a new array for the divs with class == "error"
@divs_invalid = [] #create a new array for the divs with text == "Foo is invalid"
browser.divs.each do |div| #for each div on the page
if div.class == "error" #if the class == "error"
@divs_with_error << div #add it to our array
end
end
@divs_with_error.each do |div| #for each div in the @divs_with_error array
if div.text == "Foo is invalid" #if the text == "Foo is invalid"
@divs_invalid << div #add it to our next array
end
end
If this collects the data you are looking for, you can simplify it into a single block:
@divs_with_error = [] #create a new array
browser.divs.each do |div|
if ((div.text == "Foo is invalid") && (div.class == "error"))
@divs_with_error << div
end
end
You may need to use div.text.include? if there is other text in the failing div elements. You can also use the collect! method with the arrays to save some more space. As always, rather than collecting the entire div element, you can collect certain attributes only:
@divs_with_error << [div.text, div.index]
@divs_with_error << div.class
Hope that helps!