tags:

views:

305

answers:

4

Does watir's browser.text.include? count text inside invisible divs? If so, how to search only for visible text?

I put all the instructions into the html from the beginning and use jQuery to hide and unhide the relevant parts.

How can I use watir's waiter to wait for only text that is visible?

My problem is, that the waiter always returns true, even before I have made visible a certain text.

A: 

include? sees all text, not just visible text. See WTR-433 ticket for details.

There is Element#visible? method that is not officially supported (as far as I know).

What are you trying to do? If you have a lot of text on the page, and want to show only some of it, you could put the text in a few divs and then see if the div is visible.

Željko Filipin
thank you. i will try and write my own visible function that uses Element#visible. karlthorwald - aka
i use hidden divs heavily, imagine something like a "wizard" with several stages, but each stage is a hidden div on the same html page. i make the divs visible one after another. so i do not have to destroy the content and later recreate it. (if the user goes back one stage!) i hought this is common practice. but to verify in watir if one stage has finished and the next is displaying i need to check the content that is actually displayed. i wonder why the ticket WTR 433 was closed. i believe for a browser testing tool it is a good idea to check visible text. karlthorwald - aka
checking invisible text would be a nice addition, but checking visible text would be the the first thing I expect. from a user perspective. karlthorwald - aka
if i recall corectly there is a jquery method that detaches elements (like whole div trees) from the dom and can later reattach them. maybe this would be cleaner coding with also other advantages. (though i am afraid it could produce memory holes in one or another browser) karlthorwald – aka
the longer i think about it the more i doubt the argumentation "text is text, visible or not; the #text method should return it" - but watir is not a tool for dom synthesis, it is a tool to use the browser like a human. a hidden div is acutally NOT THERE for the user.
Feel free to comment the ticket.
Željko Filipin
Thanks for your comments. I have reopened the ticket.
Bret Pettichord
+3  A: 

Don't use Watir's text method. It is very quick and dirty, and has lots of misgivings, many not even related to this issue about visible text. Solid Watir test developers will avoid it. I almost never use it myself, and only when I am doing something extremely quick and dirty. Also, it is implemented differently with different browsers.

Instead you should access the text you care about in terms of the actual element that it is in. You should reference the div or whatever.

Bret Pettichord
thank you for this valuable information
+1  A: 

What's wrong with:

text_from_all_my_visible_divs = br.div(:id, 'divs I care about').divs.select do |div|
  text_i_care_about(div) && div.visible?
end

def text_i_care_about(div)
  div.text =~ /regexp/
end

.visible? is in the unit tests, it's official, don't be scared of it :)

Alan

Alan Baird
Thank you. Looks like a good solution. It does not yet scan for some text, so I would search for the text that I am looking for inside text_from_all_my_visible_divs in the next line? karlthorwald – aka
Alan Baird
ok, I don't know how to do code formatting and it won't let me edit the above comment, let me know if it's not clear
Alan Baird
ok, figured it out...I edited the original comment
Alan Baird
A: 

This works for ie watir but doesn't account for visibility:hidden; and it's slow but try it out!

def visible_text

  text = ""

  self.elements_by_xpath(%~//*[not(contains(@style,\"DISPLAY: none\") and ancestor::*[not(contains(@style,'DISPLAY: none'))])]~).each do |e|

  text << Watir::Element.new(e).text

  end

  return text

end
Moriarty