Use page-object gem to check a box in a table

时间:2015-05-12 23:26:55

标签: watir watir-webdriver pageobjects page-object-gem

My page has a table with a checkbox in the first column. I want to check the box in each row up to a given number. I can do this in watir-webdriver, but not with page-object; possibly due to the HTML structure.

HTML - edited for relevancy (there is more than one table in the page; note this table element has no identifying attributes):

Composite

I can click the correct control using watir-webdriver:

<div id="other-students">
<div class="table-wrapper">
<table>
<thead>
  <tr>
    <th>
      <input id="select-all" class="ng-pristine ng-untouched ng-valid" type="checkbox" ng-click="otherStudentsWidget.toggleAll(selectAll)" ng-model="selectAll">
    </th>
    <th>
      ...
    </th>
    <th>
      ...
    </th>
    <th>
      ...
    </th>
  </tr>
</thead>
<tbody class="ng-hide" ng-show="!studentsFetched || !otherStudents.length">
  <tr class="loading-row ng-hide" ng-show="!studentsFetched">
    <td colSpan="4">Loading students...</td>
  </tr>
  <tr class="loading-row" ng-show="studentsFetched">
     <td colSpan="4">No students</td>
  </tr>
</tbody>
<tbody style="display: table-row-group;" class="ng-isolate-scope" student-widget="" students="otherStudents" show-highlighting="true" checked-students="checkedOtherStudents" api="otherStudentsWidget">
   <tr>
      <td>
        <input id="student-widget-0-13233" type="checkbox">
      </td>
      <td>
        <label for="student-widget-0-13233">Test</label></td>
      <td>
        <label for="student-widget-0-13233">Student</label></td>
    </tr>
    <!-- rest of the table rows omitted for brevity -->

My pageObject class (including numerous attempts of failing code, which have been commented out). I have tried to check the box directly, and by first finding the row in the table.

@browser.div(:id => 'other-students').div(:class => 'table-wrapper').table[3][0].checkbox.set

I've made many attempts to get this working. How can I use page-object to check the box?

1 个答案:

答案 0 :(得分:2)

您可以使用元素名称的plural版本来获取匹配元素的数组。因此,定义足够通用的东西来捕获所有复选框,然后创建一个方法来勾选它们的正确数量。我没有对此进行测试,但我认为它看起来像是:

checkboxes(:student_widget, id: /student-widget/)

def tick_students(n)
  self.student_widget_elements.each_with_index do |cb, i|
    cb.check if i < n
  end
end