Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the data-id in watir?

I am new to watir testing. Would somebody help me to find the following element.

<div class="location_picker_type_level" data-loc-type="1">
  <table></table>
</div>

I like to find this div, data-loc-type with table exists.

ex:

browser.elements(:xpath,"//div[@class='location_picker_type_section[data-loc-type='1']' table ]").exists?
like image 502
Sri Avatar asked Mar 22 '13 13:03

Sri


1 Answers

Watir supports using data- type attributes as locators (ie no need to use xpath). Simply replace the dashes with underscores and add a colon to the start.

You can get the div using the following (note the format of the locator for the attribute: data-loc-type -> :data_loc_type ):

browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')

If it is only expected that there is one div of this type, you can check that it has a table by doing:

div = browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')
puts div.table.exists?
#=> true

If there are multiple divs that match and you want to check that at least one of them has the table, use the any? method for a divs collection:

#Get a collection of all div tags matching the criteria
divs = browser.divs(:class => 'location_picker_type_level', :data_loc_type => '1')

#Check if the table exists in any of the divs
puts divs.any?{ |d| d.table.exists? }
#=> true

#Or if you want to get the div that contains the table, use 'find'
div = divs.find{ |d| d.table.exists? }
like image 78
Justin Ko Avatar answered Oct 11 '22 15:10

Justin Ko