我最近开始使用MonkeyRunner来测试我的安卓应用程序的UI (我也在使用Espresso,但我想玩MonkeyRunner)。我遇到的问题是无法使用自动化脚本将文本输入到EditText字段。
脚本在我的应用程序中导航得很完美,但它似乎并没有在调用MonkeyRunner.type()命令时输入任何文本。
请在下面找到我的剧本。
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')从上面的脚本可以看到,This is sample text应该放在EditText框中。仿真程序和截图都没有显示文本字段中的文本。
我是错过了一步,还是做错了什么?
任何帮助都将不胜感激!
发布于 2014-03-15 05:56:18
我宁愿使用AndroidViewClient/culebra来简化任务。基本上,您可以将设备与adb连接起来,然后运行
culebra -VC -d on -t on -o myscript.py脚本获取对所有可见视图的引用。编辑脚本并在末尾添加
no_id10.type('This is sample text')
no_id10.writeImageToFile('/tmp/image.png')不需要担心视图坐标,不需要触摸和键入,不需要添加睡眠等等。
注意事项:使用no_id10作为示例,您的EditText的id可能是不同的
发布于 2014-03-14 16:43:20
首先,我希望不是使用MonkeyRunner.sleep命令,而是使用time包和time.sleep命令。只需导入包
import time你应该可以走了。
此外,我建议您在device.touch和device.type之间等待一段时间。试着
device.touch(205,361, "DOWN_AND_UP")
time.sleep(1)
device.type("This is sample text")https://stackoverflow.com/questions/22410766
复制相似问题