keyan.r keyan.r - 4 months ago 18
Ruby Question

Integration Testing with Pagination (Ruby on Rails)

I have the following integration test for my Ruby on Rails code, which checks to make sure that all of the schools are present on a page, and then filters them to have only US/Canadian schools, and again makes sure the correct schools are on the page.

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


The issue is that now I have paginated the schools, and so all of the schools are not on the same page, making the test case fail. I need to modify the integration test so that the test cases pass, even though the schools are now spread out on different pages, with 9 schools per page. I tried the following for the first part of the test to no avail:

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


For the other parts I haven't a clue where to start.

Answer

The question does not provide enough information. I should have added more detail about the gem I was using, and included pictures. Also, this is too much of a loaded question for a Q+A site.

To do this, one need simply flip through the schools by clicking the Next button in the pagination section after checking for every 9 schools. For checking only US and Canada schools, you should only count when you find a US or Canada school, respectively. After each case, reset your count to 0 and go on to the next test. Also, if you did have to click on the Next button, then click on the First button to revert to the starting page, before going to the next test.

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
Comments