首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >calabash-android滚动到元素

calabash-android滚动到元素
EN

Stack Overflow用户
提问于 2016-01-19 09:49:27
回答 2查看 2.1K关注 0票数 0

我正在为一个使用calabash的android应用程序编写一些测试用例。

我找到元素的第一个想法是向下滚动,直到找到这样的元素:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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

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

因此,我的两个问题是:是否有更好的选择,如果不是如何,我是否发现我在页面的底部/顶部?

编辑:,谢谢你的提醒,我很赞同你的想法。

我要这样做:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-20 08:47:01

我没有卡拉巴斯安卓的例子,但这里有一个来自卡拉巴斯iOS的例子--这个概念是一样的。这不是一个理想的解决办法。

  • 如果页面增长,则需要增加计数器。
  • 我们不能确定我们从哪里开始滚动。我们的测试假设我们总是自上而下。也许你也能这么做?

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

代码语言:javascript
复制
  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,您可以添加隐藏元素来标记页面的顶部和底部。

更新

我喜欢阿拉文的回答,并在CalWebApp上试了一试。

代码语言:javascript
复制
js = "window.scrollTo(0,0)"
query(tab_name, {calabashStringByEvaluatingJavaScript:js})
wait_for_none_animating
票数 1
EN

Stack Overflow用户

发布于 2016-01-20 08:59:29

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

使用jquery滚动到元素

代码语言:javascript
复制
evaluate_javascript(query_string, javascript)

示例:

代码语言:javascript
复制
evaluate_javascript('EditText', '#{id}.ScrollTO()')

滚动到顶部

代码语言:javascript
复制
evaluate_javascript('EditText', 'scrollTop()')

您可以在这里找到更多详细信息:javascript

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34873453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档