nzsquall nzsquall - 4 months ago 17
Ruby Question

Webdriver in Ruby, how to check elements exist

I am using Webdriver in Ruby and I want to verify three text exists on a page.

Here is the piece of html I want to verify:

<table class="c1">
<thead>many subtags</thead>
<tbody id="id1">
<tr class="c2">
<td class="first-child">
<span>test1</span>
</td>
manny other <td></td>
</tr>
<tr class="c2">
<td class="first-child">
<span>test2</span>
</td>
manny other <td></td>
</tr>
<tr class="c2">
<td class="first-child">
<span>test3</span>
</td>
manny other <td></td>>
</tr>
</tbody>
</table>


How do I verify "test1", "test2" and "test3" presents on this page using


  • find_element

  • find_elements

  • getPageSource?



What is the best approach for it?

Thank you very much.

Answer

I would go with #find_elements method,because with other 2 options there will be a chance to get no such element exception.

First collect it in an array -

array = driver.find_elements(:xpath,"//table[@class='c1']//tr[@class='c2']//span[text()='test1']")

Now check the array size

"test1 is present" unless array.empty?

The same way we can test for test2 and test3.

Comments