Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Watir 使用页面对象gem选中表中的框_Watir_Watir Webdriver_Pageobjects_Page Object Gem - Fatal编程技术网

Watir 使用页面对象gem选中表中的框

Watir 使用页面对象gem选中表中的框,watir,watir-webdriver,pageobjects,page-object-gem,Watir,Watir Webdriver,Pageobjects,Page Object Gem,我的页面有一个表格,第一列有一个复选框。我想勾选每一行中的复选框,直到给定的数字为止。 我可以在watir webdriver中实现这一点,但不能使用page对象;可能是由于HTML结构 HTML-针对相关性进行编辑(页面中有多个表;请注意,此表元素没有标识属性): 我的pageObject类(包括多次尝试失败的代码,这些代码已被注释掉)。我试图直接选中该框,并首先在表中找到该行 class EditGroupPage include PageObject CHECKBOX_COL =

我的页面有一个表格,第一列有一个复选框。我想勾选每一行中的复选框,直到给定的数字为止。 我可以在watir webdriver中实现这一点,但不能使用page对象;可能是由于HTML结构

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

我的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
class-EditGroupPage
包含页面对象
复选框\u COL=0
div(:otherstudent,:id=>“otherstudent”)
#div(:studentList){:otherStudents_元素。div_元素(:index=>1)}失败
#div(:studentList){:otherStudents_元素。div(:index=>1)}失败
#div(:studentList){otherStudents_元素。div(:index=>1)}失败
div(:studentList){otherStudents_元素.div_元素(:index=>1)}表包装器div
table(:students){studentList_element.table_element}#并没有完全失败,但有一个警告,即不能像那样使用table。
#表(:students,studentList_element.table)#失败
#divs(:allDivs)=其他学生\u元素。div\u元素
复选框(:selectStudent){students\u element[checkbox\u COL].checkbox\u element}
复选框(:chooseStudent,:id=>/student-widget-0/)
学生(iNumberOfStudents)
cCurrentRow=0
当cCurrentRow
我已经做了很多尝试来让它工作。如何使用page object来选中该框?

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

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
复选框(:student\u widget,id:/student widget/)
def tick_学生(n)
self.student_widget_elements.each_带有_索引do | cb,i|
检查i是否
我可以删除一些HTML或未使用的代码,如果它们令人困惑;我想最好是尽可能地包含所有的上下文。除了应该是
cb之外,这简直太棒了。检查
而不是
。设置
我更新了答案来解决这个问题。谢谢你让我知道,很高兴我能帮上忙。嗨,我是这个pageobject宝石的新手,这个可以用于WATIR吗?因为当我使用WATIR时,它会为我抛出错误。错误:已初始化常量Watir::VERSION C:/Ruby21/lib/ruby/gems/2.1.0/gems/Watir-webdriver-0.7.0/lib/Watir-webdriver/VERSION。rb:2:警告:以前的版本定义是hereRAJ,页面对象用于Watir-webdriver,而不是Watir。如果我的回答不够,试着把这个问题作为一个新问题来问,因为你的评论与原来的问题不相关。
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
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