【问题标题】:UIAutomator 2 Run Shell Command on devices running API Level less than 21UIAutomator 2 在运行 API 级别小于 21 的设备上运行 Shell 命令
【发布时间】:2016-03-05 19:38:42
【问题描述】:

我正在使用 eclipse 编写 UIAutomator 测试并在命令行上执行以下测试以运行它们,如网络上的几个地方所述:

android create uitest-project -n myUITest -t 2 -p  C:\Users\JohnDoe\workspace\myUITest
ant build
adb push bin\myUITest.jar /data/local/tmp
adb shell uiautomator runtest myUITest.jar -c Tests.Test1

但是,我最近将测试代码迁移到 Android Studio 以利用 Android)Junit4,选择 uiautomator 2,并将所有内容集成到一个地方(adb 监视器、构建等)。 我按照步骤将以下内容添加到 app/source/Test 下的 gradle 文件中。到目前为止,一切都很好。

dependencies {
    compile 'com.google.guava:guava:18.0'
    // Testing-only dependencies
    // Force usage of support annotations in the test app, since it is internally used by the runner module.
    androidTestCompile 'com.android.support:support-annotations:23.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    // UiAutomator Testing
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.3'
}

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.1'
    defaultConfig {
        applicationId "com.example.android.testing.uiautomator.BasicSample"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

运行测试时,我可以进行 gui 测试,例如唤醒屏幕、滑动、触摸,但我发现我不再能够通过 getRuntime.exec 执行 shell 事件来模拟低级输入(物理按钮按下)像之前一样。运行该代码时没有任何反应。 经过进一步研究,我发现 executeShellCommand(字符串命令)是在级别 21 中添加的提供该功能的新 API。但是,被测设备正在运行 kit kat(API 级别 19)。

// Runtime.getRuntime used to work when working under eclipse before migrating to android studio/uiautomator 2
Runtime.getRuntime().exec("sendevent /dev/input/eventX 1 678 1");  // some low level event

有没有办法运行 shell 命令或发送低级/原始输入事件,因为在使用 UIAutomator2 和 android studio 时,通过Runtime.getRuntime 发送 shell 命令似乎不起作用,我不相信我可以使用UIAutomation executeShellCommand 因为我的设备正在运行 android kit kat (api 19)。

我花了一天多的时间浏览帖子,但找不到解决方案。任何帮助/指导将不胜感激。

【问题讨论】:

    标签: android eclipse shell android-uiautomator


    【解决方案1】:

    很遗憾,这是不可能的。如您所说,在 API 级别 21 中添加了以 shell 用户身份执行命令的 API。在旧平台上无法使用此功能。

    【讨论】:

    • 是的,我明白了。您是否知道使用 UiAutomator 生成用户输入的另一种方法,而不是 shell setevent ?另外,我目前正在使用带有 UiAutojmator 的 eclipse,但想使用 API 级别 19 的 uiautomator 版本迁移到 Android Studio。当我切换到 android studio 时,我不知道如何将 uiAutomator 设置为旧版本。
    • UiDevice.pressKeyCode(...) 是否适用于您的用例?
    • 好点艾伦..我正在生成的事件使用这种方式:shellCmd("sendevent /dev/input/event3 1 528 1") 我什至尝试写入 event3 文件,因为所有设备都表示为文件。但是,我收到了权限错误。我不确定shellCmd("sendevent /dev/input/event3 1 528 1") 如何转换为 UiDevice.pressKeyCode(...)。感谢您的时间和宝贵的意见。
    猜你喜欢
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 2020-05-16
    相关资源
    最近更新 更多