如何跨多个 Activity 测试 Android 应用程序?
我们正在构建一个复杂的 Android 应用程序,其中包含许多屏幕和分布在许多活动中的工作流程。我们的工作流程与您在银行 ATM 机上看到的类似,例如,有一个要登录的 Activity
,它会转换到主菜单 Activity
,然后可以转换到基于用户选择的其他活动。
由于我们有如此多的工作流程,我们需要创建跨多个活动的自动化测试,以便我们可以从头到尾测试工作流程。例如,使用 ATM 示例,我们需要输入有效的 PIN,验证是否将我们发送到主菜单,选择提取现金,验证我们是否在提取现金屏幕上,等等,最终发现自己返回主菜单或“注销”。
我们尝试过 Android 附带的测试 API(例如 ActivityInstrumentationTestCase2
)以及 Positron,但似乎都无法超出单个 Activity
的范围进行测试,虽然我们可以在这些工具中找到一些用于某些单元测试的实用工具,但它们无法满足我们的要求需要跨多个活动的测试场景。
我们对 xUnit 框架、脚本、GUI 记录器/回放等持开放态度,并欢迎任何建议。
We are building a complex Android application consisting of many screens and workflows spread across many Activities. Our workflows are similar to what you might see on a Bank's ATM machine, for example, there is an Activity
to login in that transitions to a main menu Activity
which can transition to other activities based on the user's choices.
Since we have so many workflows we need to create automated tests that span multiple activities so we can test a workflow from end to end. For example, using the ATM example, we would want to enter a valid PIN, verify that sends us to the main menu, choose withdraw cash, verify that we are on the withdraw cash screen, etc., etc., and eventually find ourselves back on the main menu or "logged" out.
We've toyed with the test APIs that come with Android (e.g. ActivityInstrumentationTestCase2
) and also with Positron, but neither seem capable of testing beyond the bounds of a single Activity
, and while we can find some utility in these tools for some unit testing, they won't meet our needs for testing scenarios that cut across multiple Activities.
We are open to an xUnit framework, scripting, GUI recorders/playbacks, etc. and would appreciate any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我对回答自己的赏金问题感到有点尴尬,但就是这样……
我对此进行了大量搜索,不敢相信任何地方都没有发布答案。我已经非常接近了。我现在肯定可以运行跨活动的测试,但我的实现似乎存在一些计时问题,测试并不总是可靠地通过。据我所知,这是成功跨多个活动进行测试的唯一示例。希望我对它的提取和匿名化没有引入错误。这是一个简单的测试,我在登录活动中输入用户名和密码,然后观察在不同的“欢迎”活动上显示正确的欢迎消息:
此代码显然不太可读。我实际上已经将它提取到一个带有类似英语的 API 的简单库中,所以我可以这样说:
我已经测试了大约 4 个活动的深度,并且对该方法的工作原理感到满意,尽管正如我所说,似乎有这是一个偶然的时间问题,我还没有完全弄清楚。我仍然有兴趣了解跨活动测试的任何其他方法。
I feel a bit awkward about answering my own bounty question, but here it is...
I've searched high and low on this and can't believe there is no answer published anywhere. I have come very close. I can definitely run tests that span activities now, but my implementation seems to have some timing issues where the tests don't always pass reliably. This is the only example that I know of that tests across multiple activities successfully. Hopefully my extraction and anonymizing of it did not introduce errors. This is a simplistic test where I type a username and password into a login activity, and then observe a proper welcome message is shown on a different "welcome" activity:
This code is obviously not very readable. I have actually extracted it into a simple library with an English-like API so I can just say things like this:
I've tested to a depth of about 4 activities and am satisfied that the approach works though as I said, there appears to be an occasional timing issue I have not completely figured out. I am still interested in hearing of any other ways of testing across activities.
看看 Robotium
“一个开源测试框架,旨在使 Android 应用程序的自动黑盒测试比开箱即用的 Android 仪器测试更快、更容易。”
http://www.robotium.org/
来源:
http://github.com/jayway/robotium
请注意,Robotium 项目由我工作的公司
Take a look at Robotium
'a open-source test framework created to make automatic black-box testing of Android applications significantly faster and easier than what is possible with Android instrumentation tests out-of-the-box.'
Homepage:
http://www.robotium.org/
Source:
http://github.com/jayway/robotium
Please note that the Robotium project is maintained by the company I work for
您始终可以使用 Robotium。它像 Selenium 一样支持黑盒测试,但适用于 Android。您可以在 Robotium.org 上找到它
You could always use Robotium. It supports blackbox testing just like Selenium but for Android. You will find it at Robotium.org
我很惊讶没有人提到一些领先的自动化功能测试工具。与Robotium相比,这些不需要编写Java代码。
MonkeyTalk:一个由 Gorilla Logic 公司支持的开源工具。优点:为非技术用户提供更轻松的录制和更高级别的脚本语言,并且是跨平台的(包括 iOS)。考虑到这些好处作为要求,我们发现这是最好的解决方案。它还允许自定义超出了使用 Javascript 在脚本语言中可以完成的操作。
Calabash-Android:Cucumber 风格的开源工具特征。优点:用 Gherkin 语言编写功能,该语言是业务可读的领域特定语言,可让您描述软件的行为,而无需详细说明该行为的实现方式。 cucumber-ios 中为 iOS 提供了类似但不完全的支持。记录功能不太好,因为它们产生二进制输出。
其他一些参考:
Monkeytalk 和葫芦。它提到 TestDroid 作为另一个
可能性。
I'm surprised no one has mentioned some of the leading automated functional testing tools. Compared with Robotium, these don't require writing Java code.
MonkeyTalk: an open-source tool backed by the company Gorilla Logic. Pros: provides recording as well as a higher-level scripting language easier for non-technical users, and is cross-platform (includes iOS). Given those benefits as requirements, we've found this to be the best solution. It also allows customization beyond what can be done in their scripting language using Javascript.
Calabash-Android: an open-source tool for Cucumber-style features. Pros: write features in the Gherkin language which is Business Readable, Domain Specific Language that lets you describe software’s behavior without detailing how that behavior is implemented. Similar but not exact support is available for iOS in cucumber-ios. Recording capabilities are not as good, as they produce a binary output.
A couple of other references:
Monkeytalk and Calabash. It mentions TestDroid as another
possibility.
我为 Android 创建了一个录制和播放工具,并将其发布在 GitHub 上。它易于配置和使用,无需编程,可在真实设备上运行(无需 root),并在运行测试时自动保存屏幕截图。
I created a record-and-playback tool for Android and made it available on GitHub. It's easy to configure and use, requires no programming, runs against real devices (which do not have to be rooted) and automatically saves screenshots as it plays tests.
首先,使用“ActivityInstrumentationTestCase2”,而不是“InstrumentationTestCase”作为基类。我使用 Robotium 并定期在多个活动中进行测试。我发现我必须将登录活动指定为通用类型(以及构造函数的类参数)。
“ActivityInstrumentationTestCase2”构造函数忽略包参数并且不需要它。不推荐使用使用包的构造函数。
来自 Java 文档:
“ActivityInstrumentationTestCase2(字符串pkg,类activityClass)
该构造函数已被弃用。使用 ActivityInstrumentationTestCase2(Class) 代替”
使用推荐的基类允许框架处理某些样板文件,例如启动您的活动。如有必要,这可以通过调用“getActivity()”来完成。
First of all, use 'ActivityInstrumentationTestCase2', not 'InstrumentationTestCase', as your base class. I use Robotium and routinely test across multiple Activities. I found that I have to specify the login activity as the generic type (and class argument to the constructor).
The 'ActivityInstrumentationTestCase2' constructor ignores the package argument and does not require it. The constructor that takes the package is deprecated.
From the Javadocs:
"ActivityInstrumentationTestCase2(String pkg, Class activityClass)
This constructor is deprecated. use ActivityInstrumentationTestCase2(Class) instead"
Using the recommended base class allows the framework to handle certain boilerplate, like starting your activity. That's done by the call to 'getActivity()', if necessary.
经过一些修改后发现这很有用。
首先
getInstrumentation().waitForIdleSync()
将解决 SingleShot 所说的不稳定问题InstrumentationTestCase
还有一个lauchActivity
函数,可以替换启动活动行。Found this useful with a couple of modifications.
Firstly
getInstrumentation().waitForIdleSync()
will cure the flakiness SingleShot speaks ofand also
InstrumentationTestCase
has alauchActivity
function that can replace the start activity lines.你可以这样做以避免薄片等待时间不同步:
you can do it like this to avoid the flake waiting times out of sync :
我正在做几乎相同的事情,并且我可能会对这个问题的公认答案进行变体,但我确实遇到了 计算 (gitHub) 在我搜索解决方案期间。
I'm working on pretty much the same thing, and I'll probably go with a variation on the accepted answer to this question, but I did come across Calculuon (gitHub) during my searches for a solution.
我个人没有使用过它,但 ApplicationTestCase 看起来可能就是您正在寻找的。
I haven't personally used it, but ApplicationTestCase looks like it might be what you're looking for.
接受的方法是否适用于来自不同应用程序、由不同证书签名的不同活动?如果没有,Robotium 是在同一应用程序中测试活动的最佳方法。
Will accepted approach work with different Activities from different Applications, signed by different certificates? If not, Robotium the best way to test activities within same application.
还有另一种方法可以使用 ActivityInstrumentation 类来执行多个活动。
这是一个正常的自动化场景......
首先获得您想要的任何对象的焦点,然后发送密钥
就这么简单
示例代码
唯一的事情是理解每个 API 调用会对我们有所帮助。
There is another way to do the multiple activity using ActivityInstrumentation Class..
Its a normal automation scenario...
First get the focus of what ever object you want and then send a key
Simple as that
sample code
Only thing is understanding the every API calls will help us.
这个答案基于已接受的答案,但经过修改以解决时间问题,对我来说,在添加大约六个测试后,时间问题变得一致。正如已接受的答案评论中所引用的那样,@pajato1 因解决时间问题而受到赞誉。
This answer is based on the accepted answer but modified to solve the timing issue which for me became consistent after adding about a half dozen tests. @pajato1 gets the credit for solving the timing issue, as cited in the accepted answer comments.
尝试Monkey工具测试
第1步:
打开android studio终端(工具->打开终端)
第2步:
为了使用monkey ,打开命令提示符并导航到以下目录。
第 3 步:
将此 Monkey 命令添加到终端中,然后按 Enter 键。
在模拟器中看看它的神奇之处。
500-它是频率计数或要发送用于测试的事件数。
您可以更改此计数..
更多参考,
http://www. tutorialspoint.com/android/android_testing.htm
http://androidtesting.blogspot.in/2012/04/android-testing-with-monkey-tool.html
Try the Monkey tool testing
Step 1:
open the android studio terminal(Tools-> open terminal)
Step 2:
In order to use monkey , open up a command prompt and just naviagte to the following directory.
Step 3:
add this monkey command into terminal and press enter..
see the magic in your emulator.
500- it is the frequency count or the number of events to be sent for testing.
you can change this count..
More reference,
http://www.tutorialspoint.com/android/android_testing.htm
http://androidtesting.blogspot.in/2012/04/android-testing-with-monkey-tool.html