我正在为一个使用calabash的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但是,我不知道如何检查页面的末尾,我希望有更好的方法来搜索元素,然后向下滚动到页面的底部,然后再向上滚动。
因此,我的两个问题是:是否有更好的选择,如果不是如何,我是否发现我在页面的底部/顶部?
编辑:,谢谢你的提醒,我很赞同你的想法。
我要这样做:
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发布于 2016-01-20 08:47:01
我没有卡拉巴斯安卓的例子,但这里有一个来自卡拉巴斯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,您可以添加隐藏元素来标记页面的顶部和底部。
更新
我喜欢阿拉文的回答,并在CalWebApp上试了一试。
js = "window.scrollTo(0,0)"
query(tab_name, {calabashStringByEvaluatingJavaScript:js})
wait_for_none_animating发布于 2016-01-20 08:59:29
您必须使用javascript或jquery方法来执行此操作。
使用jquery滚动到元素
evaluate_javascript(query_string, javascript)示例:
evaluate_javascript('EditText', '#{id}.ScrollTO()')滚动到顶部
evaluate_javascript('EditText', 'scrollTop()')您可以在这里找到更多详细信息:javascript
https://stackoverflow.com/questions/34873453
复制相似问题