Search code examples
rubycalabash-android

How to keep panning only if id exists?


I'm trying to pan through a list of elements to confirm that the set minimum number of elements is there on the screen. The problem I'm running into is that 'pan' will keep going and the test will pass even if I set the minimum to a high number (greater than what's actually there on the page).

When /^I swipe through my list of chat suggestion cards I should see the minimum$/ do
i = 0
while i < 12

    i += 1
    wait_for_element_exists("* id:'itemContainer'")
    pan("* id:'itemContainer'", :left)
end
end

Is there anyway to check if the number of id: 'itemContainer' is actually there, to make it fail if the minimum number doesn't exist?


Solution

  • Query will only return the elements that are currently on the screen, so you will have to swipe across to make all of the cards visible to calabash and check for them. Assuming that query("* id:'itemContainer'") would only find one at a time, i.e. the cards are a whole screen in size,

      12.times do
        card_before_panning = query("* id:'itemContainer'")
        pan("* id:'itemContainer'", :left)
        card_after_panning = query("* id:'itemContainer'")
        assert_not_equal card_before_panning, card_after_panning
      end
    

    If more than one of the cards can be visible on the screen at a time then you will have to do an additional check. Assuming that there could be two visible at the time

      11.times do
        card_before_panning = query("* id:'itemContainer' index:0")
        pan("* id:'itemContainer'", :left)
        card_after_panning = query("* id:'itemContainer' index:0")
        assert_not_equal card_before_panning, card_after_panning
      end
    
      # Then check that there are two visible on the final screen.
      assert_equal 2, query("* id:'itemContainer'").size
    

    Note that this will only work if the cards appear differently in your app, i.e. they have different content. If query("* id:'itemContainer'") has identical results for each of the cards then calabash will not be able to tell them apart to see if anything has changed.