分页集成测试(Ruby on Rails)

关键

我对我的Ruby on Rails代码进行了以下集成测试,该测试检查以确保所有学校都显示在页面上,然后将其过滤为仅具有美国/加拿大学校,并再次确保正确的学校位于这页纸。

test "Can view and search schools" do
visit logout_path
visit schools_path
School.all.each do |school|
  page.must_have_content(school.name)
end

find(:xpath, '//*[@name="filter[USA]"]/..').click
School.all.each do |school|
  page.must_have_content(school.name) if school.country == 'Canada'
  page.wont_have_content(school.name) if school.country == 'USA'
end

find(:xpath, '//*[@name="filter[USA]"]/..').click
School.all.each do |school|
  page.must_have_content(school.name)
end

find(:xpath, '//*[@name="filter[Canada]"]/..').click
School.all.each do |school|
  page.wont_have_content(school.name) if school.country == 'Canada'
  page.must_have_content(school.name) if school.country == 'USA'
end
end

问题是,现在我对学校进行了分页,因此所有学校都不在同一页面上,从而导致测试用例失败。我需要修改集成测试,以使测试用例通过,即使现在学校分布在不同的页面上,每页有9所学校。对于测试的第一部分,我尝试了以下操作,但均无济于事:

visit logout_path
school_found = 0
per_page = 9
visit schools_path
School.all.each do |school|
  page.must_have_content(school.name)
  school_found += 1
  if (school_found == per_page)
    click_on('Next ›')
  end
end

对于其他部分,我不知道从哪里开始。

关键

该问题没有提供足够的信息。我应该添加有关正在使用的宝石的更多详细信息,并包括图片。同样,对于Q + A网站来说,这是一个非常棘手的问题。

为此,只需检查每9所学校,然后单击分页部分中的“下一步”按钮,即可轻松浏览学校。对于仅检查美国和加拿大的学校,您应该仅在分别找到美国或加拿大的学校时才进行计数。在每种情况下,将计数重置为0,然后继续进行下一个测试。另外,如果您确实必须单击“下一步”按钮,则在进行下一个测试之前,请先单击“第一”按钮以返回到起始页面。

test "Can view and search schools" do

visit logout_path

visit schools_path
count = 0
nextClicked = false
select "Alphabetical", :from => "sort_by"
School.all.order('name ASC').each do |school|
  page.must_have_content(school.name)
  count += 1
  if count == 9
    click_on("Next ›", :match => :first)
    nextClicked = true if !nextClicked
  end
end

click_on("« First", :match => :first) if nextClicked
count = 0
nextClicked = false

find(:xpath, '//*[@name="filter[Canada]"]/..').click
School.all.each do |school|
  page.must_have_content(school.name) if school.country == 'USA'
  page.wont_have_content(school.name) if school.country == 'Canada'
  count += 1 if school.country == 'USA'
  if count == 9
    click_on("Next ›", :match => :first)
    nextClicked = true if !nextClicked
  end
end

click_on("« First", :match => :first) if nextClicked
count = 0
nextClicked = false

find(:xpath, '//*[@name="filter[Canada]"]/..').click
School.all.order('name ASC').each do |school|
  page.must_have_content(school.name)
  count += 1
  if count == 9
    click_on("Next ›", :match => :first)
    nextClicked = true if !nextClicked
  end
end

count = 0
click_on("« First", :match => :first) if nextClicked
nextClicked = false

find(:xpath, '//*[@name="filter[USA]"]/..').click
School.all.each do |school|
  page.must_have_content(school.name) if school.country == 'Canada'
  page.wont_have_content(school.name) if school.country == 'USA'
  count += 1 if school.country == 'Canada'
  if count == 9
    click_on("Next ›", :match => :first)
    nextClicked = true if !nextClicked
  end
end

count = 0
click_on("« First", :match => :first) if nextClicked
nextClicked = false

end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章