Question

I recently started using MonkeyRunner to test the UI of my Android application (I am also using Espresso but wanted to play around with MonkeyRunner). The problem that I am having is that I am unable to enter text into EditText fields using the automation script.

The script navigates through my app perfectly but it doesn't seem to actually enter any text on call of the MonkeyRunner.type() command.

Please find my script below.

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By
import commands
import sys
import os

# starting the application and test
print "Starting the monkeyrunner script"

# connection to the current device, and return a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
easy_device = EasyMonkeyDevice(device)

apk_path = device.shell('pm path com.mysample
if apk_path.startswith('package:'):
    print "application installed."
else:
    print "not installed, install APK"
    device.installPackage('/MySample/MySample.apk')')

package ="com.mysample"
activity = ".SampleActivity"

print "Package: " + package + "Activity: " + activity

print "starting application...."
device.startActivity(component=package + '/' + activity)
print "...component started"

device.touch(205,361, "DOWN_AND_UP")
device.type("This is sample text")
MonkeyRunner.sleep(1)

result = device.takeSnapshot()

result.writeToFile("images/testimage.png",'png')

As you can see from the script above the text This is sample text should be placed in the EditText box. Both the emulator and screenshot that is taken show no text in the text field.

Am I missing a step or just doing something incorrectly?

Any help would be greatly appreciated!

Was it helpful?

Solution

I would rather use AndroidViewClient/culebra to simplify the task. Basically, you can connect your device with adb and then run

culebra -VC -d on -t on -o myscript.py

The script obtains references to all of the visible Views. Edit the script and add at the end

no_id10.type('This is sample text')
no_id10.writeImageToFile('/tmp/image.png')

No need to worry about View coordinates, no need to touch and type, no need to add sleeps, etc.

NOTE: this is using no_id10 as an example, the id for your EditText could be different

OTHER TIPS

First of all, I would not use the MonkeyRunner.sleep command, but I would rather use the time package and the time.sleep command. Just import the package

import time

and you should be good to go.

Moreover, I suggest you should wait some time between device.touch and device.type. Try with

device.touch(205,361, "DOWN_AND_UP")
time.sleep(1)
device.type("This is sample text")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top