使用页面对象 gem 选中 table 中的框

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

我的页面有一个 table,第一列中有一个复选框。我想选中每行中的框,直到给定的数字。 我可以在 watir-webdriver 中执行此操作,但不能使用 page-object;可能是由于 HTML 结构。

HTML - 针对相关性进行了编辑(页面中有多个 table;请注意此 table 元素没有标识属性):

<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 -->

我可以使用 watir-webdriver 单击正确的控件:

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

我的 pageObject class(包括多次失败的代码尝试,已被注释掉)。我试图直接选中该框,并首先在 table 中找到该行。

class EditGroupPage 
  include PageObject
  CHECKBOX_COL = 0
  div(:otherStudents, :id => 'other-students')
  #div(:studentList) {:otherStudents_element.div_element(:index => 1) }    #fails
  #div(:studentList) {:otherStudents_element.div(:index => 1) }  #fails
  #div(:studentList) {otherStudents_element.div(:index => 1) }  #fails
div(:studentList){otherStudents_element.div_element(:index => 1) }  #table-wrapper div
table(:students){studentList_element.table_element}  #doesn't quite fail, but has a warning that table can't be used like that.
#table(:students, studentList_element.table)   #fails
#divs(:allDivs) = otherStudents_element.div_elements
checkbox(:selectStudent){students_element[CHECKBOX_COL].checkbox_element}
checkbox(:chooseStudent, :id => /student-widget-0/)

def tickStudents(iNumberOfStudents)
    cCurrentRow = 0
    while cCurrentRow < iNumberOfStudents #Check the box for cCurrentRow
        #otherStudents.tableWrapper.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click     #fails
        #allDivs[1].div_element.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails 
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox.checkbox_element.click  #fails
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox.check_checkbox_element #fails
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
        #studentList_element[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
        #students[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
    #   students[cCurrentRow][CHECKBOX_COL].checkbox_element #fails
    #   students[cCurrentRow][CHECKBOX_COL].check_checkbox_element      #fails
    #   students[cCurrentRow][CHECKBOX_COL].check #fails
    #   students[cCurrentRow][CHECKBOX_COL].click #fails
    #   students[cCurrentRow][CHECKBOX_COL].checkbox.checkbox_element.click  #fails
        #students[cCurrentRow].checkbox_element.check  #fails
    #   students[cCurrentRow].element.checkbox.set  #fails
        #students[cCurrentRow][CHECKBOX_COL].checkbox.click     #fails
#studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox.chooseStudent.click  #fails
        cCurrentRow += 1 #we should have checked the box in current row by now, so move on to next row
    end
end

我已经做了很多尝试来让它工作。如何使用页面对象来选中复选框?

您可以使用元素名称的 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