使用 espresso 的 Android 自动化,无需应用源代码 [英] Android automation using espresso without the app source code

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

问题描述

是否无法在没有源代码的情况下使用 espresso 自动化 android 应用程序.Gradle 期望这样的结构:

Is it not possible to automation android app using espresso without source code. Gradle expects a structure like this:

src/main/
src/androidTest/

但我想在不同版本的应用上运行这些自动化测试?是否可以仅通过安装应用程序并运行测试来实现?

But I would like to run these automation tests on a different version of the app? Is this possible just by installing the app and running the tests?

这里说这是不可能的:

使用 Espresso 自动化 Android APK

推荐答案

答案是肯定的,您可以使用 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

综上所述,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 自动化,无需应用源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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