执行Android应用程序中的仪器化测试。

huangapple go评论106阅读模式
英文:

Execute instrumented test from app in Android

问题

首先,如果我的问题显得无知的话,我很抱歉,我不是安卓开发者。我需要构建一个机器人应用程序(可以自动执行另一个应用程序中的操作),并发现使用 UIAutomator 可以实现这一目标。(我知道我在滥用这个框架,但在我看来,我要实现的目标在其他方式下是不可能的。)

我已经开始构建这个应用程序,它能正常工作,但是,我想要能够在不将设备连接到Android Studio的情况下运行它。
是否有可能通过按一个按钮或其他方式来执行应用程序中的测试?

英文:

First of all sorry if my question is ignorant, I'm not an Android developer. I have to build a bot application (automatically executes actions in another app) and found out that this is possible with 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.
Is it possible to execute the tests from the app by pressing a button or something?

答案1

得分: 2

以下是翻译好的部分:

"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."
我会这样做(这将花费一天或两天时间(如果你不熟悉的话可能需要更多时间,但有很多好的中等帖子等可供参考),就是设置一个类似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."
这是一个运行UI测试所需的构建命令的小例子(你可以使用Emulator Plugin或下载自己的模拟器,从Firebase Test Labs等连接真实设备。它还包括禁用锁定屏幕/动画的方法,这会破坏UI测试。不幸的是,Jenkins/Android命令行工具的整个设置远超出了本问题的范围。

"Here are some helpful content about this"
以下是一些有关这个的有用内容

Setup Jenkins on Ubuntu
在Ubuntu上设置Jenkins

Setup Android on Jenkins
在Jenkins上设置Android

英文:

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.

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.

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

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.

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

Setup Jenkins on Ubuntu
Setup Android on Jenkins

./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

答案2

得分: 1

UIautomation 依赖于 android.app.Instrumentation,这只能通过 adb 启动或从系统应用程序内部启动。出于安全原因,系统不会授予"在其他应用上执行操作"的权限,除非用户明确授予辅助功能权限。

因此,我担心不能在没有"连接到 Android Studio"的情况下使用 UIAutomator(这种连接是通过 adb 实现的,Android Studio 用于连接到手机)。如果需要的话,您也可以直接使用 adb,但连接到计算机和 adb 可执行文件是必不可少的。

有关 UIAutomator 和 AccessibilityService 的更多信息,请参阅这里

更新:
即使没有 adb 连接也无法使用 UIAutomator,您仍然可以使用此详细指南编写自己的辅助功能服务。请注意,这将允许您"执行仪器化测试",而不是"在另一个应用程序中执行操作"。

英文:

UIautomation relies on android.app.Instumentation which can only be started through adb or from within a 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). You can also use adb directly if needed BUT connection to a computer and adb executable are essential.

More information about UIAutomator and AccessibilityService diferencies here.

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. Please note it will not allow you to "execute instrumented tests" but to "perform actions in another app".

答案3

得分: 0

Yes its possible.

可以实现的方式有两种:

  • 在第一个应用中实现基于意图的框架,并从第二个应用中发送定制的意图,将它们与第一个应用能理解的数据捆绑在一起,或者通过adb进行发送

    am broadcast -a com.android.example --es KEY some_key --es VALUE some_value

  • 使用JAVA套接字通信实现TCP服务器和UiAutomator框架,该服务器可以识别和执行第一个应用的UI对象上的操作,并将其编译为仪器测试APK,然后从第二个应用中向此服务器应用发送命令以控制它(总共有3个应用:第一个,第二个(客户端)和服务器)

一开始可能听起来有点混乱,但是是可以实现的!

英文:

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

    am broadcast -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)

I know it sounds confusing at first but yeah it works !!

huangapple
  • 本文由 发表于 2020年8月11日 04:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63347232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定