Search code examples
pythonmonkeyrunner

Not able to identify text using monkeyrunner


I have a script that finds a text view,get its co-ordinates and click on it. In order to click i have to scroll and find that text view. Script is as below,

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

It does dragging. Although text 'abc' is present, it prints "Text is not found".

I removed drag() method, and manually did the dragging, it worked fine(identified text and did the clicking).

Can anyone knows whats wrong with my drag() method.

Thanks


Solution

  • AndroidViewClient.dump() dumps what is currently displayed on the screen, so if you have to scroll to make the TextView visible it won't be in the first (implicit) dump. Therefore, you have to dump again after scrolling:

    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
    

    or

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

    Also take into accout the point mentioned by NRP about the sleep.