如何从真机运行monkeyrunner脚本?
我已经编写了可以在模拟器中运行的代码。如何让它在真实设备中工作?只需连接设备并更改位置(apk 的设备位置)就足够了吗?
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connect to the current device
device = MonkeyRunner.waitForConnection()
# Install package
device.installPackage('C:/android-sdk-windows/tools/lib/purchase.apk')
# Run activity
device.startActivity(component='com.mobilenetwork.purchase/.StartPage ')
# Importing time
import time
# Waiting for 10 secs to be launched on the emulator:
time.sleep(10)
device.press('KEYCODE_BUTTON_SELECT','DOWN','')
# Screenshot
time.sleep(10)
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('G:\\Screenshot\\screen_shot.png','png')
I have written this code which is working in the emualator. How do I get it to work in a real device? Is just connecting the device and changing the location (device location of apk) enough?
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connect to the current device
device = MonkeyRunner.waitForConnection()
# Install package
device.installPackage('C:/android-sdk-windows/tools/lib/purchase.apk')
# Run activity
device.startActivity(component='com.mobilenetwork.purchase/.StartPage ')
# Importing time
import time
# Waiting for 10 secs to be launched on the emulator:
time.sleep(10)
device.press('KEYCODE_BUTTON_SELECT','DOWN','')
# Screenshot
time.sleep(10)
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('G:\\Screenshot\\screen_shot.png','png')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来它应该像现在一样在真实设备上运行。如果您遇到问题,您可能需要尝试添加“MonkeyRunner.sleep(n)”(其中 n 是数字)语句。有些设备不会等到上一个任务完成才请求下一个任务。
This looks like it should work on a real device just the way it is right now. If you are experiencing issues, you might want to try adding "MonkeyRunner.sleep(n)" (where n is a number) statements. Some devices do not wait for the last task to finish before asking for the next one.
您还可以在连接到设备之前尝试配置包和活动。
You might also try configuring the package and activity before you connect to the device.