【问题标题】:Use page-object gem to check a box in a table使用 page-object gem 检查表格中的框
【发布时间】:2015-05-12 23:26:54
【问题描述】:

我的页面有一个表格,第一列中有一个复选框。我想选中每一行中的框,直到给定的数字。 我可以在 watir-webdriver 中执行此操作,但不能使用页面对象;可能是由于 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

我已经做了很多尝试来让它发挥作用。如何使用 page-object 来勾选?

【问题讨论】:

  • 如果有混淆,我可以删除一些 HTML 或未使用的代码;我认为最好包括所有我能做到的上下文。

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


【解决方案1】:

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

【讨论】:

  • 这简直太棒了,除了应该是 cb.check 而不是 .set
  • 我更新了答案来解决这个问题。感谢您让我知道,很高兴我能提供帮助。
  • 嗨,我是这个 pageobject gem 的新手,这可以用于 WATIR 吗?因为当我使用 WATIR 时它会为我抛出错误。错误:已初始化常量 Watir::VERSION C:/Ruby21/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.7.0/lib/watir-webdriver/version.rb:2:警告:先前的定义VERSION 的版本在这里
  • RAJ,页面对象旨在与 watir-webdriver 一起使用,而不是 watir。如果我的回答不充分,请尝试将此作为新问题提出,因为您的评论与此处的原始问题无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
  • 2016-07-31
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
相关资源
最近更新 更多