针对 Android Web 应用程序的事件记录器EventRecorder

在诞生 RemoteJS 之后,我们开始思考我们如何能够进一步就有关测试 Android  Web 应用程序的方面进行实践。尽管在 Monkey 基础上便可以构建一个测试框架,不过功能却有所欠缺,不但没有事件回放,而且根据我们的经验,还不太稳定的说。

After we came up with RemoteJS, we started thinking about how we could go one step further regarding the testing ofAndroid web applications based onSencha Touch. WithMonkey, it has been possible to build a test framework on top of it, but one still needs to generate the events for playing back. Also, its features are limited and it often isn’t so stable, according to our experience.

我们认为该解决方案还会改善,不单单只是为我们改善 Sencha Touch 自身,而且还是为了其余的 Sencha 产品和 Android 的 Web 应用程序社区能够带来益处。这就是为什么我们已经创建了一个测试套件EventRecorder 的原因,EventRecorder 现已寄存于 Sencha 实验室。我们已经对 Froyo、Gingerbread 和 Honeycomb 测试升级。在接下来的视频中可以立马看看演示。

We thought the situation could be improved, not just for our own benefit by improving Sencha Touch, but also for the rest of the Sencha and Android web application communities. This is why we have created a test suite calledEventRecorder that is now available on Sencha Labs. We have tested it on Froyo, Gingerbread and Honeycomb. Take a look at the video below to see a demo of the tool in action.

工作原理 How it works

那么,它究竟为你做什么?该套件可让您记录测试 Web 应用程序的情况下,这意味着所有与网页的用户交互将被记录。这包括 URL 加载,触摸事件和硬件键盘事件。您还可以运行 JavaScript 和捕捉截图,这些行动的结果将被保存在主机上,并可以在以后相比以前的试运行。

So, what exactly will this do for you? The suite allows you to record test cases for web applications, meaning that all user interaction with a web page will be recorded. This includes URL loads, touch events and hardware keyboard events. You can also evaluate JavaScript and capture screenshots, and the result of these actions will be saved on the host computer and can later be compared to a previous test run.

EventRecorder 由一台主机上,记录所有的触摸和硬件键盘事件,以及允许用户执行下述几段行动运行的 Python 脚本。此外,有目标的 Android 设备上运行的 Java 应用程序。该设备的应用程序使用了 Android 的 WebView 的组件,它是能够记录和回放所有的用户交互。此组件是完全相同的 Android浏览器中使用。 Python  脚本通信与此应用程序超过亚行通过邮件或发送文件。由于我们使用相同的技术,如果你再熟悉我们的 RemoteJS 工具,理解起来就会不那么费劲。

EventRecorder consists of a Python script running on a host that records all touch and hardware keyboard events, as well as allowing the user to perform a few actions described below. In addition, there’s aJava application running on your target Android device. The device application uses an AndroidWebView component that is able to record and replay all user interaction. This component is the exact same one used in the Android browser. The Python script communicates with this application via messages or file sending overADB. If you’re familiar with our RemoteJS tool, you’ll have a better understanding on how it works already, since we used the same techniques for this suite.

记录 Recording

采用该套工具,想必就是先拿来用于测试测试。首先你必须把 Android 链接到你的计算机并按照一个 ADB。另外因为屏幕捕获,你还得要安装 Python 的PIL

The first thing you’ll want to do when using this tool is to record a test case. To do this you need to have an Android device connected to your computer and a working ADB setup. A PIL installation for your Python interpreter is required in order to make screen captures.

开始 Starting

运行  recorder.py 脚本并送入一个测试例子的名字作为第一个参数。

Run the recorder.py script and pass a name for your test case as the argument.

python recorder.py picker

记录器会安装并运行一个 Java 程序然后提示你可以输入命令行了。

The recorder will then install and run the Java application on your device and present you a prompt where you can enter commands.

加载网址 URL loading

第一个命令是 URL 网址,就是你的 Web 应用程序地址,例如:

The first command will always be an URL load, which should be your target web application, e.g.


所有以 http:// 或 www 开头的都被视为 URL 网址。

All commands starting with either http:// or www. will be interpreted as URL loads.

文字输入 Text Input


After the page has been fully loaded you can start user interaction including touch and hardware keyboard events. Due to technical limitations it’s not possible to record events from the soft keyboard. If your device doesn’t have a hardware keyboard then you can use the text (or simply t) command to input test.

t Hello World

如果你有硬件键盘当然可以直接输入,不过从主机输入的话则更简单一些。在测试 Sencha Touch 这个 picker example 例子的时候,可能不太说明问题。不过在说明表单例子的时候就十分有用啦。

Of course you can also do this even if you have a hardware keyboard but just want the ease of inputing text from the host computer. In the particular case of testing our Sencha Touchpicker example it probably doesn’t make much sense to use this functionality. However, it might definitely be very useful on examples likeforms.

屏幕捕获 Screen Capture

浏览一下过后,你可能就想进行采集屏幕活动,以便等下的比较。通过 screen 命令可实现,或简单的 s

After you’ve been navigating for a while you might want to make screen captures for later comparison. This is achieved with thescreen command, or more simplys

JavaScript Evaluation and Logging

凡是除了以上提到过的前缀之外,剩下的命令都被视作为 JS 语句进行执行。这样输入:

Any commands not starting with any of the prefixes mentioned above will be interpreted as JavaScript. This way, entering


就会弹出 document.title。

will cause the document title to be displayed in a popup box.

如果你打算把多个回放的 js 进行比较,你可以使用 console.log() 方法,接着执行结果随之送到你主机。如下代码就是把 Sencha Touch 中日期拾取器获得的结果 log 一下。

If you wish to compare JavaScript output across replays then you can use the console.log() method and the evaluated result will later be sent back to your host machine. In the code below, we log the value stored by the date picker component in the example provided in Sencha Touch.


更复杂的 js 当然没问题。但一行代码中的 js 数据不能超过 1kB。

You can of course use more complex JavaScript, but you are restricted to about 1kB of data in addition to having all JavaScript statements on one line.

搞定 Finishing

当你觉得差不多的时候你可以按下回车,记录器便会停止记录并写入回放脚本到你当前的目录。脚本的名称与你调用记录器时输入的名称一致,不过就是 .py 扩展名的。请谨记记录器不会获取任何屏幕捕获或 js log 文件。负责该项工作的是生成基线结果集合的回放脚本才可以。

When you’re done recording you simply need to press ENTER on the prompt, this will cause the recorder to stop recording and write a replay script to your current directory. The name of the script is the same name as you passed as an argument to the recorder, but with a .py extension. Please do note that the recorder will not fetch any screen capture or JavaScript log files. This is done on playback only by the replay script, which means you’ll have to play the test back once after recording to generate a baseline result set.

回放 Replaying


When you’re ready to replay you can start the script written by the recorder by running

python mytest.py

这样就会回放刚才记录的 URL 加载、事件、屏幕捕捉、JS 运算,并把 console log 文件的内容写入当前目录。

This will replay all the URL loads, events, screen captures and JavaScript evaluations that were recorded in the previous step. Just before exiting, all screen captures and console log files will be written to your current directory.

比较 Comparing

现在你有一个结果文件,你应该怎样做呢?嗯,这取决于您的要求。比较 JavaScript 输出文件(console.log)应该是相当简单,但比较图像则比较棘手的。为方便起见,我们做一个非常简单imagediff.py 的程序,接受两个输入图像和第三个参数作为灰度差分图像文件的名称。如果输入图像是相同的,不是写在所有的输出文件。如果你需要一个更完整的比较解决方案,那么你可以使用ImageMagick 的比较工具。

Now that you have a set of result files, what should you do? Well, that depends on your requirements. Comparing the JavaScript output file (console.log) should be fairly straightforward but comparing images is a bit more tricky. For convenience, we do include a very simple imagediff.py application that accepts two input images and a third argument being the file name for a grayscale difference image. The output file is not written at all if the input images are identical. If you need a more complete comparison solution then you could use the compare tool fromImageMagick.

以选择器作为一个用例的例子。我们执行的一些事件,把日期选择器组件中的数据存储在日志的文本文件中的,然后我们作一幅屏幕快照。如果我们想执行正在寻找确保返回的日期始终是相同的回归测试,以及相同的像素相同的捕获。在这种情况下,将 console.log()  做的输出和屏幕捕获的图像进行比较,通过使用任何广泛使用的第三方 {“diff”:http://en.wikipedia.org/wiki/Diff}类似的工具。

Take the picker example as an use case. We perform some events, we log the data stored by the date picker component in a text file, and we grab a screen shot. If we wish to perform a regression test, what we’re looking for is making sure that the returned date is always the same, as well as the captures being pixel-identical. In this case it would be trivial to do a comparison of both theconsole.log() output and the screen capture images by using any widely available third-party {“diff”:http://en.wikipedia.org/wiki/Diff}-like tool.

已知问题 Known Issues

触摸事件的时间即使被记录下来,回放也有可能不会记录点准确地重播的事件。因为系统的其余部分的开销的缘故,使得 Android 模拟器开销更大,更为明显。因此,回放可能不会产生完全一样的记录结果。滚动最受影响。如果遇到这情况,这个给你的问题,那么你可能不得不重新录制。

Even though the timings of the touch events are recorded, the playback might not replay the events exactly at the point they were recorded at. Due to the overhead of the rest of the system, this is more apparent on theAndroid Emulator than on an actual device. Thus, the playback might not produce exactly the same result as the recording. This mostly affects fling scrolling. If you encounter cases where this gives you problems then you might have to re-record the test case.

不幸的是,通过软键盘输入记录的事件将不会在回放中出现。这是一个 Android 的限制。作为一种变通方法,我们提供 text 的文本命令(上文已述)。

Unfortunately, events recorded through soft keyboard typing won’t be reproduced in the playback. This is an Android limitation. As a workaround, we provide thetext command described above.

多点触摸尚未支持。只有 Gingerbread 的 Android API 才支持多点触摸。出于这个原因,我们决定干脆暂时不搞。

Multi-touch is not yet supported, as the necessary Android API that makes that possible is only available in Gingerbread. For that reason, we decided to leave the feature out for now.

结论 Conclusion

在 EventRecorder 帮助下,我们可以创建一个针对全体 Sencha Touch 例子相对完整的基础设施的自动化测试,其目的在于,对每一个版本,在不同的设备之上,都确保呈现一个最佳的框架之品质。Android Web 应用程序亦不例外。EventRecorder 一些更多的信息,请参阅自述文件

With the help of EventRecorder, we can create an entire infrastructure to automate the testing of all Sencha Touch examples, before every release and across the different devices, making sure we provide a framework with the best quality possible. It is also our hope that it can help you delivering your Android web applications in the same way. For some more information onEventRecorder, please refer to theREADME file.


Just like all the other tools on Sencha Labs, the entire source code of the suite is completelyavailable under theMIT license. As usual, we appreciate your feedback and encourage you to contribute fixes and/or new features. We hope this tool can be useful in your professional or hobbyist work.

©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页