Search code examples
androidcalabash-android

calabash-android scroll to element


I am writing some test cases for an android app using calabash-android.

My first idea to find the elements was to just scroll down until the element is found like this:

Then /^I enter "([^\"]*)" into the input field with id "([^\"]*)"$/ do |text, id|
    q = query("EditText id:'#{id}'")
    while q.empty?
        scroll_down
        q = query("EditText id:'#{id}'")
    end
    enter_text("android.widget.EditText id:'#{id}'", text)
end

However should the page change and I already scrolled past the element in this way, I won't find the element i'm searching for. So the second idea was to do the search in this way:

Then /^I enter "([^\"]*)" into the input field with id "([^\"]*)"$/ do |text, id|
    q = query("EditText id:'#{id}'")
    while q.empty?
        scroll_down
        q = query("EditText id:'#{id}'")
    end
    while q.empty?
        scroll_up
        q = query("EditText id:'#{id}'")
    end
    enter_text("android.widget.EditText id:'#{id}'", text)
end

However I don't know how to check for the end of the page and i hope there is a better way to search for the element then to scroll down to the bottom of the page and then scroll up again.

So my two questions are: is there a better option and, if not how, do I find out that I'm at the bottom/top of the page?

EDIT: Thanks for the reminder and i pretty much went with your idea jmoody.

I'm going to do it this way:

Then /^I enter "([^\"]*)" into the input field with id "([^\"]*)"$/ do |text, id|
    q = query("EditText id:'#{id}'")
    counter = 0
    while q.empty?
        break if counter == 5
        scroll_down
        q = query("EditText id:'#{id}'")
        counter = counter + 1
    end
    if counter == 5
        fail("The button with the id:'#{id}' could not be found")
    else
        enter_text("EditText id:'#{id}'", text)
    end
end

Solution

  • I don't have an example for Calabash Android, but here is an example from Calabash iOS - the concept is the same. This is not an ideal solution.

    • If the page grows, the counter needs to be increased.
    • We can't be sure where we started scrolling. Our tests assume we are always starting from the top. Maybe you could do the same?

    https://github.com/calabash/ios-webview-test-app/tree/master/CalWebViewApp/features


      Scenario: Query UIWebView with css
        Given I am looking at the UIWebView tab
        And I can query for the body with css
    
      Then(/^I can query for the body with css$/) do
        page(WebViewApp::TabBar).with_active_page do |page|
        qstr = page.query_str("css:'body'")
        visible = lambda {
          query(qstr).count == 1
        }
    
        counter = 0
        loop do
          break if visible.call || counter == 6
          scroll(page.query_str, :down)
          step_pause
          counter = counter + 1
        end
        res = query(qstr)
        expect(res.count).to be == 1
      end
    end
    

    If you control the html on the page, you could add hidden elements to mark the top and bottom of the page.

    UPDATE

    I liked Aravin's answer and tried it out in the CalWebApp.

    js = "window.scrollTo(0,0)"
    query(tab_name, {calabashStringByEvaluatingJavaScript:js})
    wait_for_none_animating