使用 Espresso 自动化 Android APK [英] Automation of Android APK with Espresso

查看:39
本文介绍了使用 Espresso 自动化 Android APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动化我的 Android 应用程序的一些 UI(我没有源代码,所以我使用的是 APK 文件).

I am trying to automate some UI of my Android application(I do not have source code so I am using the APK file) .

我已经完成了这里和谷歌还提供了一些教程,但都需要源代码.

I have gone through tutorial provided here and also some tutorial available at Google but all of them require source code.

如果有人知道如何在没有源代码的情况下使用 Espresso 自动化 UI,请帮忙.

If anyone have some idea how to automate the UI with Espresso without source code, please help.

我使用 IntelliJ 作为 IDE 和应用程序 android 版本 5.0.2.

I am using IntelliJ as IDE and app android version 5.0.2.

推荐答案

答案是肯定的,您可以使用 Espresso 运行自动化测试,而无需应用源代码.

The answer is yes, you can run automation test using Espresso without app source code.

Espresso 基于 Android 检测框架,这意味着自动化测试内置于单个测试 apk 中.本次测试apk与普通应用apk不同:

Espresso is based on Android instrumentation framework, which means the automation test is built into a single test apk. This test apk is different from normal application apk:

  1. 在AndroidManifest.xml中注册了一个instrumentation,一旦安装了test apk就会注册到Android系统中

  1. There is an instrumentation registered in AndroidManifest.xml, which will be registered to Android system once test apk is installed

测试apk必须使用与应用apk相同的签名,才能运行自动化测试

The test apk must be signed using the same signature with the application apk, in order to run automation test

测试apk与应用apk在同一个进程中运行

The test apk runs in the same process as application apk

以上是任何基于仪器的测试框架的唯一要求.所以没有源代码的依赖.

Above are the only requirements of any instrument based test framework has. So there is no dependency of source code.

但是为什么我们发现大多数 Espresso 教程都与源代码混合在一起?因为这样会让测试更简单:

But why we find most of the Espresso tutorials are mixed with source code? Because it will make the test simpler:

  1. 您可以使用类 ActivityTestRule 轻松控制活动生命周期.

  1. You can easily control the activity lifecycle using class ActivityTestRule.

您可以轻松测试应用程序定义的类.

You can test application defined classes easily.

您可以使用小部件 ID 测试 UI 小部件

You can test UI widgets using widget id

相反,如果不使用源代码进行编译,则必须编写大量反射代码才能获得所需的类.例如:

On the contrary, you have to write lots of reflection code to get the classes you need if you don't compile with source code. For example:

  1. 您必须使用 Class.forName 加载入口活动并启动它

  1. You have to use Class.forName to load the entrance activity and launch it

您必须使用 Java 反射来测试应用程序定义的类

You have to use Java reflection to test application defined classes

您必须使用文字信息来查找 UI 小部件,因为您没有 UI 小部件的 id

You have to use literal information to find UI widgets, because you don't have the id of the UI widgets

我认为是由于上述缺点,这使得 Google 更愿意将 Espresso 测试与源代码一起构建.

I think it's due to the above disadvantages, which makes Google prefer to building Espresso test together with source code.

综上所述,Espresso 自动化测试无需应用源代码也可以运行,但难度大,测试代码难看.

To sum up, it is OK to run Espresso automation test without application source code, but it's much harder and make test codes ugly.

您可以从 AndroidTestWithoutSource 引用示例项目.

You can refer the example project from AndroidTestWithoutSource.

这篇关于使用 Espresso 自动化 Android APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆