葫芦/ruby while循环未结束

葫芦/ruby while循环未结束,ruby,calabash,calabash-android,Ruby,Calabash,Calabash Android,简而言之,我有一个自定义步骤定义,可以向下滚动搜索结果页面,直到找到文本字符串 它向下滚动并找到文本字符串 但是while循环没有结束,尽管它正确地向下滚动并在找到文本字符串时停止 这显然给我带来了无尽的伤害 Then /^I scroll until I see the "This is a test" text$/ do q = query("android.widget.TextView text:'This is a test'") while q.empty? scr

简而言之,我有一个自定义步骤定义,可以向下滚动搜索结果页面,直到找到文本字符串

它向下滚动并找到文本字符串

但是while循环没有结束,尽管它正确地向下滚动并在找到文本字符串时停止

这显然给我带来了无尽的伤害

Then /^I scroll until I see the "This is a test" text$/ do 
  q = query("android.widget.TextView text:'This is a test'")
  while q.empty?
    scroll("android.view.View id:'search_listView'", :down)
    q = query("android.widget.TextView text:'This is a test'")
  end
end

是我一直在用的红宝石

您使用哪种版本的葫芦? 我已经编写了类似的步骤定义,它非常适合我(葫芦0.5.1,Ruby 2.1.2)

下面是代码(我认为它更通用、更简单):

element\u不存在()
scroll\u down
都是葫芦Ruby API的函数

相反,您可以尝试使用功能滚动指定的滚动视图

(编辑:对不起,我没有看评论;)

试试这个

def scroll_to_text(text)
  element = "android.widget.TextView text:'#{text}'"
  if !element_exists(element)
    wait_poll(:until_exists => element, :timeout => 60, :screenshot_on_error => true) do
      scroll("android.view.View id:'search_listView'", :down)
    end
  end
end

此方法将为您提供一个屏幕截图,如果在60秒后未发现文本滚动,则会引发错误。您可以根据需要修改以使用。(我只是从您的帖子中获得代码,因此如果第一次出现问题,请尝试修改此代码)。

我认为这里没有足够的信息来确定问题的原因。当
q.empty?
返回false时,循环将中断。如果
q.empty?
从不返回false,则循环将永远继续。确定,但当查询发现文本字符串将q.empty更改为false时(“android.widget.TextView text:‘这是一个测试’”),则不是,否则循环将终止。或者您的假设是错误的,
query(“android.widget.TextView text:“这是一个测试”)
没有找到测试字符串。其中一个肯定是真的。考虑添加一些调试代码,或者使用PrY调试器之类的工具来手动检查问题的原因。尽管它似乎找到了文本字符串q=query(“android.widget.TextView id:
idoftextviewelementinquestion”““这是一个测试”)
是我应该使用的查询,这使它工作@Ajedi32,谢谢你推我过去。另外,那个顶带D的圆锥形帽子是我的,谢谢。
def scroll_to_text(text)
  element = "android.widget.TextView text:'#{text}'"
  if !element_exists(element)
    wait_poll(:until_exists => element, :timeout => 60, :screenshot_on_error => true) do
      scroll("android.view.View id:'search_listView'", :down)
    end
  end
end