【发布时间】: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