watir - 使用页面对象 gem 选中表中的复选框

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

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

HTML - 进行相关性编辑(页面中有多个表格;请注意,该表格元素没有标识属性):

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

关于watir - 使用页面对象 gem 选中表中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30203141/

相关文章:

ruby - 如何使用 watir 打开 tor 浏览器?

Ruby Watir WebDriver Net::ReadTimeout

ruby - 如何使用页面对象执行拖放?

java - 无法定位元素 - 使用 selenium webdriver 在不同区域设置中自动化网站

python - 页面对象模型不工作 Selenium Python

ruby - 我怎样才能得到 Browser.text.include?搜索变量? (使用 Watir 和 Ruby)

javascript - Firefox:禁用退出警告框?

watir - 将 watir-webdriver 附加到现有窗口

ruby - 如何使用 watir-webdriver 和 ruby​​ 自动化 slider ?

ruby - 使用 watir webdriver 以 headless 模式驱动浏览器时无法清除多选列表中的选定选项