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

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

问题描述

我正在开发一个质量检查自动化解决方案,该解决方案可以在Android上记录/回放质量检查测试.一项关键的业务要求是在回放记录的测试时不依赖于连接的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.

要解决此问题,我正在尝试使用

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意味着要付出很多努力才能到达那里.至少对于 Nexus Pixel 设备而言,所需的驱动程序可用

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-只有在获得签名级别权限的情况下,而-对于普通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天全站免登陆