monekeyrunner无法返回“ getProperty”和“ getSystemProperty”的值

时间:2019-03-15 07:28:54

标签: android monkeyrunner

当我编写一个Python脚本以使用 monkeyrunner 时,我发现该脚本无法获得我想要的getProperty的返回值。该脚本仅获得None结果。

这是我想要getProperty的返回值的python代码:

#-*-coding:utf-8-*-

# import module
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

# connect service 
device = MonkeyRunner.waitForConnection()

print device.getProperty("build.ID")
print device.getProperty("activity") 
print device.getSystemProperty('build.version.sdk')

结果是None

我不知道怎么了。 请查看并提供反馈。

运行结果如下:

enter image description here

这是我编写的整个脚本:

#-*-coding:utf-8-*-

# import module
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, 
MonkeyImage

# connect service
device = MonkeyRunner.waitForConnection()

# screenshot
result = device.takeSnapshot()
result.writeToFile('E:\\Taskmanagetest_1\\testresult\\Test_001.jpg','jpg')

# install apk  
device.installPackage('E:\\Taskmanagetest_1\\app\\build\\outputs\\apk\\debug\\app-debug.apk')  

# sleep
MonkeyRunner.sleep(5)

# screenshot
result = device.takeSnapshot()
result.writeToFile('E:\\Taskmanagetest_1\\testresult\\Test_002.jpg','jpg')

# print device.getProperty("am.current.comp.package")

print('Install Success!')

# test
device.startActivity(component="com.example.samuel_soon.taskmanagetest_1/.Ma"
                           "inActivity")
MonkeyRunner.sleep(5)

result = device.takeSnapshot()
result.writeToFile('E:\\Taskmanagetest_1\\testresult\\Test_003.jpg','jpg')

device.touch(520,520,'DOWN_AND_UP')

MonkeyRunner.sleep(5)
result = device.takeSnapshot()
result.writeToFile('E:\\Taskmanagetest_1\\testresult\\Test_004.jpg','jpg')

print device.getProperty("build.ID")
print device.getProperty("activity")
print device.getSystemProperty('build.version.sdk')

device.removePackage("com.example.samuel_soon.taskmanagetest_1")
print('Remove Success!')
result = device.takeSnapshot()
result.writeToFile('E:\\Taskmanagetest_1\\testresult\\Test_005.jpg','jpg')

1 个答案:

答案 0 :(得分:0)

您可以使用AndroidViewClient/culebra生成可以编辑和添加print语句的脚本

#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2013-2018  Diego Torres Milano
Created on 2019-03-26 by Culebra v15.5.1
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
"""


from com.dtmilano.android.viewclient import ViewClient

device, serialno = ViewClient.connectToDeviceOrExit()
vc = ViewClient(device, serialno)

print device.getProperty("ro.build.id")
print device.getTopActivityName()
print device.getSystemProperty("ro.build.version.sdk")

执行时会打印期望值

OSM1.180201.007
com.google.android.apps.nexuslauncher/.NexusLauncherActivity
27