如何自动测试主屏幕上的小部件?
我正在寻找自动测试 Android 应用程序小部件的方法。因此,测试的一部分显然是将小部件放在主屏幕上。我想出了一个解决方案,但它对我来说有点太老套了,所以我想知道是否有更好的方法。
我的要求是使用外部脚本在模拟器上运行。现在我使用一个简单的bash脚本,但我使用的方法也应该与monkeyrunner脚本一起使用:
首先我定义一个函数来将密钥发送到模拟器,就像已经SO 上指出:
function send() { ( nc -w 2 localhost 5554 <<EOL
event send $*
quit
EOL
) | grep -E -v "OK|KO|Android Console" ; }
和
function send_many() { for i in $* ; do send EV_KEY:$i:1 EV_KEY:$i:0 ; done ; }
send()
函数相当于调用 MonkeyDevice.send(..., DOWN_AND_UP)。
现在我可以可靠地发送一个键序列来打开菜单,选择“添加”(A 键),向上两次向下一次选择“小部件”,选择我的小部件(它恰好是列表中的第一个),等待一两秒后,小部件配置 UI 就会显示出来,并在其中选择其安装按钮:
$ send_many KEY_MENU KEY_A KEY_UP KEY_UP KEY_DOWN KEY_ENTER KEY_ENTER
$ sleep 2s
$ send_many KEY_UP KEY_RIGHT KEY_ENTER
最后,我将所有这些内容封装在一个脚本中,该脚本会迭代一堆 AVD(例如“test_avd_N”,其中 N 是来自的 API 编号) 3..12),关闭所有正在运行的模拟器,打开一个新模拟器,等待其启动并运行测试脚本。我使用快照准备所有 AVD,并使用模拟器 -no-snapshot-save 选项在运行之间保持快照完整。
总的来说,它可以工作,但相当难看。我想知道我在这里可以改进什么。
I am looking at ways to automate testing of an Android app widget. Part of the test is thus obviously to put the widget on the Home screen. I came up with a solution, but it's a bit too hackish for my taste so I wonder if there's any better way.
My requirement is for this to work on the emulator, using an external script. Right now I use a simple bash script, but the method I use should work with a monkeyrunner script too:
First I define a function to send keys to the emulator, as already indicated on SO:
function send() { ( nc -w 2 localhost 5554 <<EOL
event send $*
quit
EOL
) | grep -E -v "OK|KO|Android Console" ; }
and
function send_many() { for i in $* ; do send EV_KEY:$i:1 EV_KEY:$i:0 ; done ; }
The send()
function is equivalent to calling MonkeyDevice.send(..., DOWN_AND_UP).
Now I can reliabily send a key sequence to open the menu, select Add (A key), go up twice and down once to select "Widget", select my widget (it happens to be the first one in the list), wait a second or two for the widget configuration UI to show up and select its install button in it:
$ send_many KEY_MENU KEY_A KEY_UP KEY_UP KEY_DOWN KEY_ENTER KEY_ENTER
$ sleep 2s
$ send_many KEY_UP KEY_RIGHT KEY_ENTER
Finally I wrap that all that in a script that iterates through a bunch of AVDs (e.g. "test_avd_N" where N is an API number from 3..12), closes any running emulator, opens a new one, wait for it to start and runs the test scripts. I prepare all the AVDs using snapshots and use the emulator -no-snapshot-save
option to keep the snapshot intact between runs.
So overall it works but it's quite unsightly. I wonder what I could improve here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 MonkeyRunner 脚本至少可以消除该
send
函数的笨拙。键序列中的一个明显问题是使用“A”作为主菜单中“添加”的快捷方式。如果模拟设备使用不同的语言,那么这将不起作用,而您必须在某个时候对其进行测试。
说到这里,从 API 9 或 10 开始,模拟器上有一个经过修改的 CustomLocale.apk,您可以使用它通过广播更改区域设置:
要检查您的模拟器是否支持此功能,请检查 CustomLocale apk 包中是否有 v2姓名:
Using a MonkeyRunner script will at least remove the clumsiness of that
send
function.One obvious issue in your key sequence is using 'A' as a shortcut for Add from the Home menu. That won't work if the emulated device is in a different language, which you're bound to test at some point.
Speaking of which, starting with API 9 or 10, there's a revamped CustomLocale.apk on the emulator that you can use to change the locale using a broadcast:
To check if your emulator supports this, check whether the CustomLocale apk has v2 in its package name: