从应用程序内运行仪器测试并等待结果 [英] Run an instrument test from within app and wait for result

查看:42
本文介绍了从应用程序内运行仪器测试并等待结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 QA 自动化解决方案,可以在 Android 上记录/回放 QA 测试.一个关键的业务需求是在回放录制的测试时不依赖于连接的 PC.为此,我试图在没有连接 PC 的情况下运行仪器测试.(特别是 Appium UiAutomator2 测试).

I am developing a QA automation solution that can record/playback QA tests on Android. A key business requirement is to have no dependency on a connected PC while playing back the recorded test. To that end, I'm trying to run an Instrumentation test without a connected PC. (Specifically, an Appium UiAutomator2 test).

我目前的方法是尝试从我的应用程序中以编程方式运行测试.如果我从连接的 PC 正常运行测试,我会使用命令 adb shell am instrument -w.我尝试从我的应用程序访问 ADB Shell 并运行 am instrument -w,但这会产生一个错误,表明我缺少 INTERACT_ACROSS_USERS_FULL 权限.

My current approach is trying to run the test programmatically from my app. If I were running the test normally from a connected PC, I would use the command adb shell am instrument -w. I tried accessing ADB Shell from my app and running am instrument -w, but this produces an error that I am missing the INTERACT_ACROSS_USERS_FULL permission.

为了解决这个问题,我尝试使用 startInstrumentation.这成功地开始了测试.但是,测试立即崩溃.经过进一步调查,我将崩溃追溯到 NPE:测试试图检索 InstrumentationRegistry.getInstrumentation.getUiAutomation(0),但这返回 null.

To get around this issue, I am trying to run the test using startInstrumentation. This successfully starts the test. However, the test immediately crashes. Upon further investigation, I traced the crash to an NPE: the test is trying to retrieve InstrumentationRegistry.getInstrumentation.getUiAutomation(0), but this returns null.

如何以编程方式运行测试并授予它访问所需 UiAutomation 实例的权限?

这是我开始测试的方式:

This is how I start the test:

public void runTest() {
    final String pm = getPackageName().replaceFirst(".test$", "");
    final InstrumentationInfo info = getInstrumentationInfo(pm);
    if (info != null) {

        final ComponentName cn = new ComponentName(info.packageName,
                info.name);

        Bundle arguments = new Bundle();
        arguments.putString("class", "io.testim.appiumwrapper.test.AppiumUiAutomator2Server");
        //cn = {io.extension.test/android.support.test.runner.AndroidJUnitRunner}
        startInstrumentation(cn, null, arguments);

    } 
}

推荐答案

参见 签名保护级别 - 澄清 ...除非该软件包被 Google 的发布密钥列入白名单,否则您将无法获得必要的许可.这是一个安全/完整性功能,目的是限制恶意软件能够做什么——以及你打算在那里做什么,是典型的恶意软件行为——无论它的实际意图如何;与系统作对无济于事.

see signature protection level - clarifying ...unless having the package white-listed by Google's release key, you won't be able to obtain the necessary permission. this is a security/integrity feature, with the purpose to limit what malware is able to do - and what you intend to do there, is typical malware behavior - no matter the actual intention of it; working against the system leads nowhere.

我可以想象的唯一方法是直接从终端模拟器或测试应用程序运行命令 - 针对 AOSP,这样你就可以将 android:protectionLevel="signature" 添加到 Manifest.xml 然后要求 android.permission.INTERACT_ACROSS_USERS_FULL.但是对于库存ROM,绝对没有机会这样做.并非如此,这将是不可能的",但构建自定义 ROM 意味着要付出相当大的努力才能达到目标.至少对于 NexusPixel 设备,所需的驱动程序可用 这里;对于其他设备,您必须在设备供应商处找到它们(如果它们甚至可用).

the only way I could imaging would be to run commands directly from a terminal emulator or the test application - against a custom build of AOSP, so that you could add android:protectionLevel="signature" to the Manifest.xml and then require android.permission.INTERACT_ACROSS_USERS_FULL. but with a stock ROM, there definitely is no chance to do so. it is not, that it would be "impossible", but building a custom ROM means quite some effort, in order to get there. at least for Nexus and Pixel devices, the required drivers are available here; for other devices, you'd have to find them at the device's vendor, if they're even available.

诀窍是使用与应用程序相同的密钥签署 ROM - 只有这样才能获得 signature 级别的权限 - 而对于股票 ROM,您(理论上)需要 Google 的发布签收包裹的钥匙.如此处所述,可以强制执行单个用户,而这也是仅适用于系统应用.

the trick is to sign off the ROM with the same key as the app - only then signature level permissions can be obtained - while for a stock ROM, you'd (theoretically) need Google's release key to sign off the package. one can enforce single user, as explained here, while this also is only available to system apps.

这篇关于从应用程序内运行仪器测试并等待结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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