just_add_watir | Advanced recipes for Watir
17Jul/08Off

Q. How do I get text from a browser?

A. Use a regex match on the .text method ...
Example html:

<p>
  The Employee Code assigned is 10000671
</p>

Example watir:

puts /(1\d+)/.match(@b.text)
Tagged as: , No Comments
17Jul/08Off

Q. How do I check if text exists in browser?

A. Use the .include? method or a regular expression ...
Example html:

<p>
  watir does not go well with furry little animals
</p>

Example watir:

puts "Text 'watir' DOES exist" if @b.text.include? 'watir'
puts "Text 'regex' DOES NOT exist" unless @b.text =~ /regex/
puts "Text 'furry little animals' DOES exist" if @b.text.include? 'furry little animals'
puts "Text 'furry animals' DOES NOT exist" unless @b.text.include? 'furry animals'

Any character(s) placed between the apostrophe's will be searched for. The response will return true if found, and false if not found.

Tagged as: , , 1 Comment