2012-12-21 52 views
1

我有一个脚本可以找到文本视图,获取其坐标并点击它。为了点击我必须滚动并找到文本视图。脚本如下,无法使用monkeyrunner识别文本

text = 'abc' 
self.device.drag((400,600), (300, 200), 0.01, 120) 
tv = self.vc.findViewWithText(text) 
if tv: 
    (x, y) = tv.getXY() 
    print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..." % (text, x, y) 
    tv.touch() 
else: 
     print "Text is not found" %text 

它拖动。尽管文本'abc'存在,但它会打印出“未找到文本”。我删除了drag()方法,并且手动进行了拖动,它工作正常(标识文本并执行了单击操作)。

任何人都可以知道我的drag()方法有什么问题。

感谢

回答

1

AndroidViewClient.dump()转储什么是目前显示在屏幕上,所以如果你有滚动,使TextView可见它不会是第一个(隐含的)转储。 因此,你必须滚动后再次转储:

text = 'abc' 
self.device.drag((400,600), (300, 200), 0.01, 120) 
MonkeyRunner.sleep(3) 
self.vc.dump() 
tv = self.vc.findViewWithText(text) 
if tv: 
    (x, y) = tv.getXY() 
    print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..." % (text, x, y) 
    tv.touch() 
else: 
     print "Text is not found" %text 

text = 'abc' 
self.device.drag((400,600), (300, 200), 0.01, 120) 
MonkeyRunner.sleep(3) 
self.vc.dump() 
self.vc.findViewWithTextOrRaise(text).touch() 

也考虑到户头由NRP关于睡眠中提到的点。

+0

感谢dtmilano ..我添加了seld.vc.dump()并且工作正常。 –

0

MonkeyRunner excutes所有命令非常快,所以有些时候你要它开始寻找一个视图之前补充睡眠。所以你的代码会像。

text = 'abc' 
self.device.drag((400,600), (300, 200), 0.01, 120) 
MonkeyRunner.sleep(1) 
tv = self.vc.findViewWithText(text) 
if tv: 
    (x, y) = tv.getXY() 
    print >>sys.stderr, "Clicking TextView %s @ (%d,%d) ..." % (text, x, y) 
    tv.touch() 
else: 
     print "Text is not found" %text