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

查看:279
本文介绍了使用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中注册了一个仪器,一旦安装了测试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 加载并启动入口活动

您必须使用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天全站免登陆