calabash-android 滚动到元素

calabash-android scroll to element

我正在使用 calabash-android.

为 android 应用编写一些测试用例

我找到元素的第一个想法是向下滚动直到找到元素,如下所示:

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

但是,如果页面发生变化并且我已经以这种方式滚动过该元素,我将找不到我正在搜索的元素。所以第二个想法是用这种方式进行搜索:

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

但是我不知道如何检查页面的末尾,我希望有更好的方法来搜索元素然后向下滚动到页面底部然后再次向上滚动。

所以我的两个问题是:是否有更好的选择,如果没有更好的选择,我是否可以找到我在页面的 bottom/top?

编辑: 谢谢你的提醒,我几乎同意你的想法 jmoody。

我要这样做:

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

我没有 Calabash Android 的示例,但这里有一个来自 Calabash iOS 的示例 - 概念是相同的。这不是一个理想的解决方案。

  • 如果页面增长,则需要增加计数器。
  • 我们无法确定我们是从哪里开始滚动的。我们的测试假设我们总是从头开始。也许你也可以这样做?

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

如果您控制页面上的html,您可以添加隐藏元素来标记页面的顶部和底部。

更新

我喜欢 Aravin 的回答并在 CalWebApp.

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

您必须使用 javascriptjquery 方法来执行此操作。

使用 jquery

滚动到元素
evaluate_javascript(query_string, javascript)

示例:

evaluate_javascript('EditText', '#{id}.ScrollTO()')

滚动到顶部

evaluate_javascript('EditText', 'scrollTop()')

您可以在此处找到更多详细信息:http://www.rubydoc.info/gems/calabash-android/0.5.8/Calabash/Android/Operations:evaluate_javascript