MonkeyRunner 和 AndroidViewClient (Touch) 的脚本错误
Error of Script with MonkeyRunner and AndroidViewClient (Touch)
我正在尝试使用 monkeyrunner 和 AndroidViewClient 测试一些应用程序,我也使用过 culebra 生成代码,但我有这个错误:
from com.dtmilano.android.viewclient import ViewClient, TextView, EditText
SyntaxError: ("mismatched input 'as' expecting COLON", ('/home/experts/AndroidViewClient-master/src/com/dtmilano/android/viewclient.py', 3795, 35, ''))
这是我的 monkeyrunner 和 AndroidViewClient 脚本
import re
import sys
import os
try:
sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
pass
from com.dtmilano.android.viewclient import ViewClient, TextView, EditText
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
def main():
# Connects to the current device, returning a MonkeyDevice object
emulatorid = 'emulator-5558'
device = MonkeyRunner.waitForConnection('',emulatorid)
print "waiting for connection...\n"
package = "com.duolingo"
activity = "app.LoginActivity"
# sets the name of the component to start
runComponent = package + "/." + activity
#Runs the component
print "Start Component"
device.startActivity(component=runComponent)
MonkeyRunner.sleep(10)
vc = ViewClient(device)
vc.dump()
vc.sleep(_s)
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'GET STARTED').touch()
vc.sleep(_s)
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'Spanish', root=self.vc.findViewByIdOrRaise('id/no_id/3')).touch()
# Takes a screenshot
screenshot = device.takeSnapshot()
# Writes the screenshot to a file
screenshot.writeToFile('./device1.png','png')
print "end"
if __name__ == '__main__':
main()
如果我在没有 monkeyrunner 的情况下执行这个程序,它会显示一个关于 "ViewClient(device)" 的错误,这个脚本在一个循环中 (for)。
编辑:
我执行了 culebra,现在我的脚本 运行 很好!!但是,它通过 CommandLine ($ myscripttest.py emulator-5554) 工作 如果我使用 subprocess.call 调用它,其中 emulatorid='emulator-5554'
subprocess.call('path/script_files/myscripttest.py' + ' ' + emulatorid, shell=True)
显示错误:
File "/path/script_files/myscripttest.py", line 9, in <module>
from com.dtmilano.android.viewclient import ViewClient
File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.0.7-py2.7.egg/com/dtmilano/android/viewclient.py", line 43, in <module>
import xml.parsers.expat
File "/usr/lib/python2.7/xml/parsers/expat.py", line 4, in <module>
from pyexpat import *
ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so: undefined symbol: XML_SetHashSalt
我用这段代码验证:错误在哪里?
$ ldd /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so
linux-vdso.so.1 => (0x00007fffe5dc2000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f55d2fd4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f55d2c0f000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f55d29e5000)
/lib64/ld-linux-x86-64.so.2 (0x00007f55d3403000)
AndroidViewClient/culebra 是 monkeyrunner
的替代品。您应该在脚本中使用其中之一,而不是同时使用两者。
culerba
或 Culebra GUI 将为您生成正确的测试或脚本模板,您可以进一步自定义。
这些工具生成的所有测试和脚本都应使用 python 2.x.
执行
我想我可以猜出你想用你的脚本实现什么,我将使用 culebra
给出一个循序渐进的例子,这样你就会明白它是多么容易。
第 1 步
启动 culebra
表示您还想启动特定的 Activity
$ culebra -GuU --start-activity=com.duolingo/.app.LoginActivity --scale=0.5 -o ~/tmp/duolingo-screenshot.py
此 运行s GUI (-G
),不验证转储中的视图 (-u
),创建单元测试 (-U),启动 Activity (--start-activity
),将 window 缩放 50% 使其适合屏幕 (--scale
) 并将测试保存在文件中 (-o
)。
如果一切顺利,您将看到 Culebra GUI。
第 2 步
单击 window 上的 开始 按钮。
步骤 3
单击西班牙语
第 4 步
使用上下文菜单(交替单击 window)或键盘快捷键
截取所需的屏幕截图
第 5 步
关闭 window.
此时生成了测试,它包含类似这样的内容
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2014 Diego Torres Milano
Created on 2015-12-22 by Culebra v11.0.8
__ __ __ __
/ \ / \ / \ / \
____________________/ __\/ __\/ __\/ __\_____________________________
___________________/ /__/ /__/ /__/ /________________________________
| / \ / \ / \ / \ \___
|/ \_/ \_/ \_/ \ o \
\_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''
import re
import sys
import os
import unittest
from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
TAG = 'CULEBRA'
class CulebraTests(CulebraTestCase):
@classmethod
def setUpClass(cls):
cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
cls.options = {'start-activity': 'com.duolingo/.app.LoginActivity', 'concertina': False, 'device-art': None, 'use-jar': False, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'glare': False, 'dictionary-keys-from': 'id', 'scale': 0.5, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'log-actions': False, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': True, 'verbose-comments': False, 'gui': True, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': None, 'unit-test-method': None, 'interactive': False}
cls.sleep = 5
def setUp(self):
super(CulebraTests, self).setUp()
def tearDown(self):
super(CulebraTests, self).tearDown()
def preconditions(self):
if not super(CulebraTests, self).preconditions():
return False
return True
def testSomething(self):
if not self.preconditions():
self.fail('Preconditions failed')
_s = CulebraTests.sleep
_v = CulebraTests.verbose
self.vc.dump(window=-1)
self.vc.findViewWithTextOrRaise(u'GET STARTED').touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.findViewWithTextOrRaise(u'Spanish', root=self.vc.findViewByIdOrRaise('id/no_id/5')).touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.writeImageToFile('/tmp/${serialno}-${focusedwindowname}-${timestamp}.png', 'PNG', 'None', False, False)
if __name__ == '__main__':
CulebraTests.main()
所以当你运行再次测试时,你会得到一个新的截图。
重要:因为测试不包含有关设备或其特征(如屏幕尺寸等)的任何特定信息,并且它不使用坐标进行触摸,而是使用文本或视图属性id 你可以 运行 在一个非常不同的设备上进行相同的测试,比如 table,它会 运行 成功。即使是保存的屏幕截图也以设备的序列号为前缀,因此您可以 运行 同时在同一 CI 服务器上同时 运行 所有这些,如果您愿意的话。
额外小费
如果您 运行 测试不止一次而没有重新安装应用程序,也许第二次您不会看到 开始使用 屏幕,而是 选择每日目标。如果我们也想考虑这个替代方案,我们可以只编辑脚本并添加相应的条件
def testSomething(self):
if not self.preconditions():
self.fail('Preconditions failed')
_s = CulebraTests.sleep
_v = CulebraTests.verbose
self.vc.dump(window=-1)
if not self.vc.findViewWithText(u'Pick a Daily Goal'):
self.vc.findViewWithTextOrRaise(u'GET STARTED').touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.findViewWithTextOrRaise(u'Spanish', root=self.vc.findViewByIdOrRaise('id/no_id/5')).touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.writeImageToFile('/tmp/${serialno}-${focusedwindowname}-${timestamp}.png', 'PNG', 'None', False, False)
即使您想在设备图像中为屏幕截图加框,也比使用 Android Studio 更容易,您必须重新 select 一次又一次的选项。只需更改 takeScreenshot
的参数,将 STUDIO_DIR
环境变量设置为指向您的 Studio 安装
self.vc.writeImageToFile('/tmp/${serialno}-${focusedwindowname}-${timestamp}.png', 'PNG', 'nexus_5', True, True)
瞧!
我正在尝试使用 monkeyrunner 和 AndroidViewClient 测试一些应用程序,我也使用过 culebra 生成代码,但我有这个错误:
from com.dtmilano.android.viewclient import ViewClient, TextView, EditText
SyntaxError: ("mismatched input 'as' expecting COLON", ('/home/experts/AndroidViewClient-master/src/com/dtmilano/android/viewclient.py', 3795, 35, ''))
这是我的 monkeyrunner 和 AndroidViewClient 脚本
import re
import sys
import os
try:
sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
pass
from com.dtmilano.android.viewclient import ViewClient, TextView, EditText
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
def main():
# Connects to the current device, returning a MonkeyDevice object
emulatorid = 'emulator-5558'
device = MonkeyRunner.waitForConnection('',emulatorid)
print "waiting for connection...\n"
package = "com.duolingo"
activity = "app.LoginActivity"
# sets the name of the component to start
runComponent = package + "/." + activity
#Runs the component
print "Start Component"
device.startActivity(component=runComponent)
MonkeyRunner.sleep(10)
vc = ViewClient(device)
vc.dump()
vc.sleep(_s)
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'GET STARTED').touch()
vc.sleep(_s)
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'Spanish', root=self.vc.findViewByIdOrRaise('id/no_id/3')).touch()
# Takes a screenshot
screenshot = device.takeSnapshot()
# Writes the screenshot to a file
screenshot.writeToFile('./device1.png','png')
print "end"
if __name__ == '__main__':
main()
如果我在没有 monkeyrunner 的情况下执行这个程序,它会显示一个关于 "ViewClient(device)" 的错误,这个脚本在一个循环中 (for)。
编辑:
我执行了 culebra,现在我的脚本 运行 很好!!但是,它通过 CommandLine ($ myscripttest.py emulator-5554) 工作 如果我使用 subprocess.call 调用它,其中 emulatorid='emulator-5554'
subprocess.call('path/script_files/myscripttest.py' + ' ' + emulatorid, shell=True)
显示错误:
File "/path/script_files/myscripttest.py", line 9, in <module>
from com.dtmilano.android.viewclient import ViewClient
File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.0.7-py2.7.egg/com/dtmilano/android/viewclient.py", line 43, in <module>
import xml.parsers.expat
File "/usr/lib/python2.7/xml/parsers/expat.py", line 4, in <module>
from pyexpat import *
ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so: undefined symbol: XML_SetHashSalt
我用这段代码验证:错误在哪里?
$ ldd /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so
linux-vdso.so.1 => (0x00007fffe5dc2000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f55d2fd4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f55d2c0f000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f55d29e5000)
/lib64/ld-linux-x86-64.so.2 (0x00007f55d3403000)
AndroidViewClient/culebra 是 monkeyrunner
的替代品。您应该在脚本中使用其中之一,而不是同时使用两者。
culerba
或 Culebra GUI 将为您生成正确的测试或脚本模板,您可以进一步自定义。
这些工具生成的所有测试和脚本都应使用 python 2.x.
执行我想我可以猜出你想用你的脚本实现什么,我将使用 culebra
给出一个循序渐进的例子,这样你就会明白它是多么容易。
第 1 步
启动 culebra
表示您还想启动特定的 Activity
$ culebra -GuU --start-activity=com.duolingo/.app.LoginActivity --scale=0.5 -o ~/tmp/duolingo-screenshot.py
此 运行s GUI (-G
),不验证转储中的视图 (-u
),创建单元测试 (-U),启动 Activity (--start-activity
),将 window 缩放 50% 使其适合屏幕 (--scale
) 并将测试保存在文件中 (-o
)。
如果一切顺利,您将看到 Culebra GUI。
第 2 步
单击 window 上的 开始 按钮。
步骤 3
单击西班牙语
第 4 步
使用上下文菜单(交替单击 window)或键盘快捷键
截取所需的屏幕截图第 5 步
关闭 window.
此时生成了测试,它包含类似这样的内容
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2014 Diego Torres Milano
Created on 2015-12-22 by Culebra v11.0.8
__ __ __ __
/ \ / \ / \ / \
____________________/ __\/ __\/ __\/ __\_____________________________
___________________/ /__/ /__/ /__/ /________________________________
| / \ / \ / \ / \ \___
|/ \_/ \_/ \_/ \ o \
\_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''
import re
import sys
import os
import unittest
from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
TAG = 'CULEBRA'
class CulebraTests(CulebraTestCase):
@classmethod
def setUpClass(cls):
cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
cls.options = {'start-activity': 'com.duolingo/.app.LoginActivity', 'concertina': False, 'device-art': None, 'use-jar': False, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'glare': False, 'dictionary-keys-from': 'id', 'scale': 0.5, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'log-actions': False, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': True, 'verbose-comments': False, 'gui': True, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': None, 'unit-test-method': None, 'interactive': False}
cls.sleep = 5
def setUp(self):
super(CulebraTests, self).setUp()
def tearDown(self):
super(CulebraTests, self).tearDown()
def preconditions(self):
if not super(CulebraTests, self).preconditions():
return False
return True
def testSomething(self):
if not self.preconditions():
self.fail('Preconditions failed')
_s = CulebraTests.sleep
_v = CulebraTests.verbose
self.vc.dump(window=-1)
self.vc.findViewWithTextOrRaise(u'GET STARTED').touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.findViewWithTextOrRaise(u'Spanish', root=self.vc.findViewByIdOrRaise('id/no_id/5')).touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.writeImageToFile('/tmp/${serialno}-${focusedwindowname}-${timestamp}.png', 'PNG', 'None', False, False)
if __name__ == '__main__':
CulebraTests.main()
所以当你运行再次测试时,你会得到一个新的截图。
重要:因为测试不包含有关设备或其特征(如屏幕尺寸等)的任何特定信息,并且它不使用坐标进行触摸,而是使用文本或视图属性id 你可以 运行 在一个非常不同的设备上进行相同的测试,比如 table,它会 运行 成功。即使是保存的屏幕截图也以设备的序列号为前缀,因此您可以 运行 同时在同一 CI 服务器上同时 运行 所有这些,如果您愿意的话。
额外小费
如果您 运行 测试不止一次而没有重新安装应用程序,也许第二次您不会看到 开始使用 屏幕,而是 选择每日目标。如果我们也想考虑这个替代方案,我们可以只编辑脚本并添加相应的条件
def testSomething(self):
if not self.preconditions():
self.fail('Preconditions failed')
_s = CulebraTests.sleep
_v = CulebraTests.verbose
self.vc.dump(window=-1)
if not self.vc.findViewWithText(u'Pick a Daily Goal'):
self.vc.findViewWithTextOrRaise(u'GET STARTED').touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.findViewWithTextOrRaise(u'Spanish', root=self.vc.findViewByIdOrRaise('id/no_id/5')).touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.writeImageToFile('/tmp/${serialno}-${focusedwindowname}-${timestamp}.png', 'PNG', 'None', False, False)
即使您想在设备图像中为屏幕截图加框,也比使用 Android Studio 更容易,您必须重新 select 一次又一次的选项。只需更改 takeScreenshot
的参数,将 STUDIO_DIR
环境变量设置为指向您的 Studio 安装
self.vc.writeImageToFile('/tmp/${serialno}-${focusedwindowname}-${timestamp}.png', 'PNG', 'nexus_5', True, True)
瞧!