标签: android-instrumentation

如何在 Espresso 测试中重新启动 Android 应用程序(/应用程序状态)?

我正在测试登录页面 - 特别是“自动登录”复选框,以便用户登录一次后,将在重新打开应用程序时自动登录(默认情况下,用户应从头开始登录)。

我怎样才能模拟这种行为?重新启动应用程序是唯一的方法吗?我可以以某种方式将应用程序重置到初始屏幕(就像重新启动一样),但应该保留用户数据/cookie吗?

android-testing android-espresso android-instrumentation

5
推荐指数
1
解决办法
3556
查看次数

AndroidJunit4 不接受空格函数测试名称?

我有以下测试,其中测试名称带有空格和反引号用于我的仪器测试

@RunWith(AndroidJUnit4::class)
class MyTestClass {
    @Rule
    @JvmField
    var activityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)

    @Test
    fun `My space name testing`() {
          // Some test
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在运行的时候,却无法执行(即No test was found)

检查它,我在测试函数名称上看到了这个 linting 错误..

This inspection reports identifiers in android projects which are not accepted by the Android runtime (for example, method names containing spaces)

当我将测试函数从My space name testingto重命名时mySpaceNameTesting,测试运行。

AndroidJunit4 运行时真的不支持带空格的测试函数名吗?

android kotlin android-instrumentation android-runtime android-junit

5
推荐指数
1
解决办法
1705
查看次数

写入检测上下文内部文件夹时出现权限被拒绝异常

我创建了一个函数,将文件从资产复制到我的仪器测试中的内部路径。

fun copyFileFromAssetsToInternal(context: Context, fis: FileInputStream) {
        File(context.filesDir, file).outputStream().use { fos ->
            var read: Int
            val buffer = ByteArray(1024)
            do {
                read = fis.read(buffer)
                if (read == -1) break
                fos.write(buffer, 0, read)
            } while (read > 0)
        }
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序的包是 gr.example.myapp

当我使用InstrumentationRegistry.getInstrumentation().targetContext上述工作正常但它写入/data/user/0/gr.example.myapp/files/

但是当我使用InstrumentationRegistry.getInstrumentation().context上面的尝试写入 /data/user/0/gr.example.myapp.test/files/ (我已经检查过并且它确实存在)并失败时,出现以下异常:

java.io.FileNotFoundException: /data/user/0/gr.example.myapp.test/files/test.zip (Permission denied)
Run Code Online (Sandbox Code Playgroud)

我已经添加了

@get:Rule
    val rule = GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE, permission.READ_EXTERNAL_STORAGE)
Run Code Online (Sandbox Code Playgroud)

到我的测试文件的顶部,我仍然收到错误

有什么我可以做的吗?因为我真的不想用测试数据污染我的实际应用程序的内部数据

android android-instrumentation

5
推荐指数
0
解决办法
132
查看次数

ActivityScenario.launch 寻找一些缓存的 jar

我正在运行 android 仪器测试,我有以下代码:

    lateinit var scenario: ActivityScenario<MainActivity>

@Before
fun before(){
    scenario = ActivityScenario.launch(MainActivity::class.java)
}
Run Code Online (Sandbox Code Playgroud)

测试本身运行良好。

但是,每次启动套件(或单个测试)时,我都会在日志中看到以下内容

E/System: Unable to open zip file: /data/user/0/com.my.app/cache/dFBYiv5m.jar
E/System: java.io.FileNotFoundException: /data/user/0/com.my.app/cache/dFBYiv5m.jar (No such file or directory)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:265)
        at java.util.zip.ZipFile.<init>(ZipFile.java:187)
        at java.util.jar.JarFile.<init>(JarFile.java:169)
        at java.util.jar.JarFile.<init>(JarFile.java:106)
        at libcore.io.ClassPathURLStreamHandler.<init>(ClassPathURLStreamHandler.java:46)
        at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:756)
        at dalvik.system.DexPathList$Element.findResource(DexPathList.java:783)
        at dalvik.system.DexPathList.findResources(DexPathList.java:578)
        at dalvik.system.BaseDexClassLoader.findResources(BaseDexClassLoader.java:248)
        at java.lang.ClassLoader.getResources(ClassLoader.java:839)
        at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:349)
        at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:402)
        at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:488)
        at androidx.test.internal.platform.ServiceLoaderWrapper.loadService(ServiceLoaderWrapper.java:46)
        at androidx.test.internal.platform.ServiceLoaderWrapper.loadSingleService(ServiceLoaderWrapper.java:69)
        at androidx.test.core.app.ActivityScenario.<init>(ActivityScenario.java:140)
        at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:189)
        at com.my.app.test.authFlow.OnboardingFlowSteps.before(OnboardingFlowSteps.kt:80)
        at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) …
Run Code Online (Sandbox Code Playgroud)

android android-instrumentation activity-scenario

5
推荐指数
0
解决办法
144
查看次数

无法使用 minifyEnabled 运行 Android 仪表化 (UI) 测试

minifyEnabled目前,我的 UI 测试在调试版本设置为 false 时运行良好。然而,我希望能够使用 minifyEnabled 运行我们的 UI 测试,但我收到错误:

java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/LazyKt;
at com.myapp.android.helpers.BaseUITest.<init>(BaseUITest.kt:89)
at com.myapp.android.helpers.BaseLoggedInUITest.<init>(BaseUITest.kt:58)
at com.myapp.android.BookingTest.<init>(BookingTest.kt:25)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Caused by: java.lang.ClassNotFoundException: Didn't …
Run Code Online (Sandbox Code Playgroud)

android proguard android-instrumentation

5
推荐指数
1
解决办法
653
查看次数

Espresso android - IllegalStateException:UiAutomationService android.accessibilityservice.IAccessibilityServiceClient 已注册

对于下面定义的规则,在 AWS 上随机运行 IllegalStateException 异常

@Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(<permissions>)
Run Code Online (Sandbox Code Playgroud)

这是异常跟踪

INSTRUMENTATION_STATUS: stack=java.lang.IllegalStateException: UiAutomationService android.accessibilityservice.IAccessibilityServiceClient$Stub$Proxy@8129d6balready registered!
at android.os.Parcel.createException(Parcel.java:2079)
at android.os.Parcel.readException(Parcel.java:2039)
at android.os.Parcel.readException(Parcel.java:1987)
at android.app.IUiAutomationConnection$Stub$Proxy.connect(IUiAutomationConnection.java:427)
at android.app.UiAutomation.connect(UiAutomation.java:241)
at android.app.Instrumentation.getUiAutomation(Instrumentation.java:2167)
at android.app.Instrumentation.getUiAutomation(Instrumentation.java:2124)
at androidx.test.runner.permission.UiAutomationShellCommand.<init>(UiAutomationShellCommand.java:65)
at androidx.test.runner.permission.PermissionRequester.addPermissions(PermissionRequester.java:92)
at androidx.test.rule.GrantPermissionRule.grantPermissions(GrantPermissionRule.java:100)
at androidx.test.rule.GrantPermissionRule.grant(GrantPermissionRule.java:94)
Run Code Online (Sandbox Code Playgroud)

可能出什么问题了?

android android-espresso android-instrumentation android-espresso-recorder

5
推荐指数
1
解决办法
1440
查看次数

Android,在检测模式下运行常规应用程序(仅限 adb + debug)

我想以一种可以InstrumentationRegistry.getInstrumentation()在运行时访问的方式运行 Android 应用程序。

这仅适用于调试版本,通过 ADB 安装应用程序就足够了。因为我不想使用仪器测试套件,所以我不想使用InstrumentationTestRunner但直接启动MainActivity应用程序。

这怎么可能?

我尝试过的:

  1. 添加了对测试包的常规实现依赖(而不是仅测试依赖) - 有效
    implementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    implementation 'androidx.test:runner:1.1.0'
Run Code Online (Sandbox Code Playgroud)
  1. 添加了检测清单
    <instrumentation android:name=".MainActivity"
        android:targetPackage="com.android.shell"
        android:label="App" />
Run Code Online (Sandbox Code Playgroud)

(不确定这是否真的正确。)

  1. 使用运行应用程序(应用程序已安装在设备上)
adb shell am instrument -w com.example.app/.MainActivity         
Run Code Online (Sandbox Code Playgroud)

这导致:

android.util.AndroidException: INSTRUMENTATION_FAILED: com.example.app/com.example.app.MainActivity
    at com.android.commands.am.Instrument.run(Instrument.java:519)
    at com.android.commands.am.Am.runInstrument(Am.java:202)
    at com.android.commands.am.Am.onRun(Am.java:80)
    at com.android.internal.os.BaseCommand.run(BaseCommand.java:60)
    at com.android.commands.am.Am.main(Am.java:50)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:399)
Run Code Online (Sandbox Code Playgroud)

UiAutomator所以本质上我的问题是,如何以我可以在运行时使用的方式运行应用程序?

提前致谢!

android android-testing android-instrumentation android-uiautomator

5
推荐指数
1
解决办法
756
查看次数

Android Instrumentation HOME按钮

任何想法为什么将HOME键发送到在Instrumentation下运行的应用程序什么都不做?

import android.app.Instrumentation;
import android.view.KeyEvent;

public class MyInstrumentation extends Instrumentation {

  public void sendKeys() {
    // Works reliably
    this.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);

    // Does nothing, nothing is printed to logcat
    this.sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
  }
}
Run Code Online (Sandbox Code Playgroud)

使用Robotium时以及直接使用Instrumentation时也是如此.模拟器和非root设备上的行为相同.

instrumentation android robotium android-testing android-instrumentation

4
推荐指数
1
解决办法
951
查看次数

通过spoon-gradle插件从特定包运行仪器测试

我正在使用Roman Mazur 的勺子gradle插件。我能够一次运行所有测试,但我无法指定要启动的“组”测试。目前我的勺子设置看起来像这样:

spoon {
    debug = true

    baseOutputDir = file("$buildDir/spoon-log")
    if (project.hasProperty('spoonClassName')) {
        className = project.spoonClassName

        if (project.hasProperty('spoonMethodName')) {
            methodName = project.spoonMethodName
        }
    }

    adbTimeout = 60 * 60;
}
Run Code Online (Sandbox Code Playgroud)

我的测试位于包中:

在此处输入图片说明

我的目标是创建单独的 gradle 任务,这些任务依赖于勺子分别从每个包启动测试。Roman 给了我们参数instrumentationArgs,它应该能够编辑勺子内的一些属性。

正如我在勺子的主 git 上看到的那样,你可以指定包,勺子运行器应该在其中查找你的测试,示例如下:

--e package=com.mypackage.unit_tests
Run Code Online (Sandbox Code Playgroud)

所以我的想法是把这个属性放到 instrumentationArgs 中。因此,我创建了这样的勺子任务:

task spoonAuthFlowTests(type: GradleBuild, dependsOn: ['spoon']) {
    spoon {
        instrumentationArgs = ["package=com.myapp.instrumentation.flowtests.AuthFlowTests"]
        noAnimations = true;
    }
}

task spoonFlowTests(type: GradleBuild, dependsOn: ['spoon']) {
    spoon {
        instrumentationArgs = ["package=com.myapp.instrumentation.flowtests"]
        noAnimations = true; …
Run Code Online (Sandbox Code Playgroud)

java android spoon android-instrumentation

4
推荐指数
1
解决办法
2086
查看次数

在Android Studio中将UnitTest转换为Instrumentation Test

因此,我从一个名为MyCoolObjectTest的UnitTest开始。需要进行一些测试测试,我通过拖动将类直接在Android项目视图中从test目录移动到androidTest目录。我的UnitTest过去工作正常;除了仪器部分。但是现在它根本不起作用。我不断

Class not found: "com.mypkg.MyCoolObjectTest"Empty test suite.
Run Code Online (Sandbox Code Playgroud)

我一直在整个stackOverflow和官方文档中寻找解决方案。到目前为止没有运气。有没有人经历过类似的事情?

(如果我将其拖回测试文件夹,它将正常工作;如果我将其重新拖动到androidTest文件夹,它将再次停止工作)

我记得那是设置受测试文件夹的一种方法。但是我不再记得了。

为了获得一些见识,我想在测试中使用一些android库,例如

public String stripFormatting(String input){
    return Html.fromHtml(input).toString();
}
Run Code Online (Sandbox Code Playgroud)

这就是为什么我需要仪器。

这是我的单元测试课程,其中包含一个测试示例:

@RunWith(AndroidJUnit4.class)
public class MyCoolObjectTest {
    @Test
    public void testJustToKnow() {
        String actual = "<b>0</b>";
        String expected = "0";
        assertThat(stripFormatting(actual), equalTo(expected));
    }
    public String stripFormatting(String input) {
        return Html.fromHtml(input).toString();
    }
}
Run Code Online (Sandbox Code Playgroud)

更新

这是我得到的另一条迹线。如果单击该方法而不是该类,则会得到以下跟踪:

刚才我单击了方法而不是整个类,并获得了以下跟踪信息:

11/04 13:51:16:启动testJustToKnow()$ adb push /Users/me/StudioProjects/Myapp/app/build/outputs/apk/app-debug.apk /data/local/tmp/com.mypkg.myapp $ adb shell pm install -r“ /data/local/tmp/com.mypkg.myapp” pkg:/data/local/tmp/com.mypkg.myapp成功

$ adb push /Users/me/StudioProjects/Myapp/app/build/outputs/apk/app-debug-androidTest-unaligned.apk /data/local/tmp/com.mypkg.Myapp.test $ adb …

android unit-testing android-studio android-instrumentation

4
推荐指数
1
解决办法
1150
查看次数