2012-12-21 50 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