简体   繁体   English

从 Android 中的应用程序执行检测测试

[英]Execute instrumented test from app in Android

First of all sorry if my question is ignorant, I'm not an Android developer.首先很抱歉,如果我的问题是无知的,我不是 Android 开发人员。 I have to build a bot application (automatically executes actions in another app) and found out that this is possible with UIAutomator .我必须构建一个机器人应用程序(在另一个应用程序中自动执行操作),并发现使用UIAutomator可以做到这一点。 (I know I'm misusing the framework, but the way I see it what I'm trying to achieve is not possible in any other way.) (我知道我在滥用框架,但是我看到它的方式是我试图实现的,这是任何其他方式都不可能的。)

I started to build the app and it works, however, I would like to be able to run it without the device being connected to Android Studio.我开始构建应用程序并且它可以工作,但是,我希望能够在没有将设备连接到 Android Studio 的情况下运行它。 Is it possible to execute the tests from the app by pressing a button or something?是否可以通过按按钮或其他方式从应用程序执行测试?

UIautomation relies on android.app.Instumentation which can only be started through adb or from within a System App . UIautomation 依赖于 android.app.Instumentation 只能通过 adb 或从 System App 中启动 For security reasons the system doesn't grant permissions to "execute actions on other apps" unless accesibility permissions has been granted explicitly from the user.出于安全原因,除非用户明确授予访问权限,否则系统不会授予“在其他应用程序上执行操作”的权限。

For this reasons I'm afraid it will NOT be possible to use UIAutomator without "being connected to android studio" (this connection is achieved precisely by means of adb, which Android studio uses to connect to the cellphone).由于这个原因,我担心如果没有“连接到 android 工作室”就无法使用 UIAutomator(这种连接是通过 adb 精确实现的,Android 工作室用来连接手机)。 You can also use adb directly if needed BUT connection to a computer and adb executable are essential.如果需要,您也可以直接使用 adb,但必须连接到计算机和 adb 可执行文件。

More information about UIAutomator and AccessibilityService diferencies here .有关 UIAutomator 和 AccessibilityService 差异的更多信息在这里

Update: Even if it's not possible to use UIAutomator without an adb connection, you can still write your own accessibility service using this detailed guide .更新:即使没有 adb 连接就无法使用 UIAutomator,您仍然可以使用此详细指南编写自己的无障碍服务。 Please note it will not allow you to "execute instrumented tests" but to "perform actions in another app".请注意,它不允许您“执行检测测试”,而是“在另一个应用程序中执行操作”。

Do you have to run it in a certain device/through app?您是否必须在某个设备/通过应用程序中运行它? What I would do (and this will take a day or two (even more if you are not familiar with it but there are lots of good medium posts etc for doing it)) would be to setup a CI env like Jenkins and either trigger the Jenkins Job whenever I want, or introduce periodic jobs which gets executed in every X hour.我会做的(这将需要一两天(如果您不熟悉它,则需要更多时间,但有很多好的中等帖子等))将设置一个 CI 环境,如Jenkins并触发Jenkins 随时工作,或者引入每隔 X 小时执行一次的定期工作。

If you are dead set on running it from your phone, good thing here is that you can setup a webhook and trigger it through your app then jenkins will trigger the job you want.如果您对通过手机运行它一无所知,那么这里的好处是您可以设置一个 webhook 并通过您的应用程序触发它,然后 jenkins 将触发您想要的工作。

Another plus of this approach is that, theoretically you can scale it to infinity and your bot runs the tests in parallel in X different devices这种方法的另一个优点是,理论上您可以将其扩展到无穷大,并且您的机器人在 X 个不同的设备中并行运行测试

Once you have setup Jenkins and download Android commandline tools, you will need to create a job, where you build a normal apk and a test apk and install it to the target device.设置 Jenkins 并下载 Android 命令行工具后,您将需要创建一个作业,在其中构建一个普通 apk 和一个测试 apk 并将其安装到目标设备。

Here is a small example of build commands you need to provide to run the ui tests (you can either use Emulator Plugin or download your own emulator with avd connect a real device from Firebase Test Labs etc. It also includes the way to disable lock screen/animations which breaks ui tests. Unfortunately whole setup of Jenkins/Android commandline tools far exceeds the scope of this question Here are some helpful content about this这是运行 ui 测试需要提供的构建命令的一个小示例(您可以使用Emulator Plugin或下载您自己的仿真器和 avd 从 Firebase 测试实验室等连接真实设备。它还包括禁用锁定屏幕的方法/animations 这会破坏 ui 测试。不幸的是,Jenkins/Android 命令行工具的整个设置远远超过了这个问题的 scope 这里有一些有用的内容

Setup Jenkins on Ubuntu Setup Android on Jenkins 在 Ubuntu 上设置 Jenkins 在 Jenkins 上 设置 Android

./gradlew clean myapp:assembleDebug --no-daemon -Dkotlin.compiler.execution.strategy="in-process"
./gradlew myapp:assembleDebugAndroidTest --no-daemon -Dkotlin.compiler.execution.strategy="in-process"


/var/lib/jenkins/android-sdk/platform-tools/adb -s emulator-5558 install -r  myapp/build/outputs/apk/debug/myapp-debug.apk
/var/lib/jenkins/android-sdk/platform-tools/adb -s emulator-5558 install -r -t myapp/build/outputs/apk/androidTest/debug/myapp-debug-androidTest.apk
/var/lib/jenkins/android-sdk/platform-tools/adb -s emulator-5558 shell input keyevent KEYCODE_MENU 
 /var/lib/jenkins/android-sdk/platform-tools/adb -s emulator-5558 shell settings put global window_animation_scale 0 &
    /var/lib/jenkins/android-sdk/platform-tools/adb -s emulator-5558 shell settings put global transition_animation_scale 0 &
    /var/lib/jenkins/android-sdk/platform-tools/adb -s emulator-5558 shell settings put global animator_duration_scale 0 &
   /var/lib/jenkins/android-sdk/platform-tools/adb -s emulator-5558  shell am instrument -w  -e clearPackageData true --no-window-animation  -e package com.mypackage.testcases.tests -e debug false com.mypackage.test/androidx.test.runner.AndroidJUnitRunner

Yes its possible.是的,有可能。

It can be acheived in two ways:可以通过两种方式实现:

  • Implement intent based framework in first app and sends customized intents from your second app bundling them with data that can be understood by the first app or from adb在第一个应用程序中实现基于意图的框架,并从您的第二个应用程序发送定制的意图,将它们与第一个应用程序或 adb 可以理解的数据捆绑在一起

    am broadcast -a com.android.example --es KEY some_key --es VALUE some_value正在广播 -a com.android.example --es KEY some_key --es VALUE some_value

  • Implement a TCP server using JAVA socket communication and UiAutomator framework which recognizes and perform operations on UI objects of first app and compile it as Instrument test apk and send comands to this server app from your second app to control it(so 3 apps in total: first, second(client) and server)使用 JAVA 套接字通信和 UiAutomator 框架实现 TCP 服务器,该框架识别并在第一个应用程序的 UI 对象上执行操作,并将其编译为仪器测试 apk,并从您的第二个应用程序发送命令到此服务器应用程序以控制它(总共 3 个应用程序:第一,第二(客户端)和服务器)

I know it sounds confusing at first but yeah it works !!我知道一开始听起来很混乱,但它确实有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android测试,模拟最终课程 - Android Instrumented Test, Mock Final Classes 项目库上的Android Instrumented单元测试 - Android Instrumented unit test on project library Mockito:doNothing尝试在Android检测测试中调用void方法 - Mockito : doNothing tries to invoke the void method in Android instrumented test 如何从我的应用程序中的 androidTest(插桩测试)测试类访问资源? - How do I access resources from my application inside of an androidTest (instrumented test) test class? 在 Java 中从 Android 应用程序执行 Python 脚本? - Execute Python script from Android app in Java? 如何从Android应用程序执行Shell命令? - How to execute shell command from android app? 从 android 应用程序执行外部程序 - Execute an external program from an android app 使用Android Test Orchestrator执行检测测试时无法获取异常的堆栈跟踪 - Can't get stack trace of an exception while executing Instrumented tests using Android Test Orchestrator Android 仪器测试:由:java.lang.VerifyError:JVMVRFY012 堆栈形状不一致引起 - Android Instrumented Test: Caused by: java.lang.VerifyError: JVMVRFY012 stack shape inconsistent 尝试从Eclipse到Android Studio执行Android App的异常 - Exception trying to execute Android App from Eclipse To Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM