如何在UIInstrumentation测试的列表视图中断言是否存在特定值

时间:2018-09-18 06:29:01

标签: android android-instrumentation android-uiautomator

我正在尝试进行仪器测试。我在youtube上执行。我的初始测试运行正常,但断言失败。我想要实现的是打开youtube搜索内容并通过检查结果中是否存在来断言。因此,我搜索“驱魔楼梯场景”,并断言是否确实找到了结果。但是我收到了AssertionError。我在做什么错?/这是正确的方法吗?

以下是我的代码

    Context context = InstrumentationRegistry.getInstrumentation().getContext(); //gets the context based on the instrumentation
        Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.google.android.youtube");  //sets the intent to start your app
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);  //clear out any previous task, i.e., make sure it starts on the initial screen
        context.startActivity(intent);
        UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        UiObject icon = mDevice.findObject(new UiSelector().descriptionContains("Search"));
        icon.waitForExists(5000);
        icon.click();
        UiObject editText = mDevice.findObject(new UiSelector().text("Search YouTube"));

        editText.waitForExists(5000);
        editText.setText("Exorcist staircase scene");
        mDevice.pressEnter();
        UiScrollable listView = new UiScrollable(new UiSelector().scrollable(
                true));
        listView.setAsHorizontalList();
        int index =0;
        if(index <= listView.getChildCount()){


> Assert.assertTrue(listView.getChild(new
> UiSelector().index(index).resourceId("com.google.android.youtube:id/title").descriptionContains("Exorcist")).exists());

            //listView.getChild(new UiSelector().index(index).descriptionContains("Exorcist")).exists();
        }
        synchronized (mDevice) {
            mDevice.wait(20000);
        }

这是我出错的地方

  
    

Assert.assertTrue(listView.getChild(新     UiSelector()。index(index).resourceId(“ com.google.android.youtube:id/title”)。descriptionContains(“ Exorcist”))。exists());

  
My error log

> java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at com.protossoft.root.uitestingapplication.ExampleInstrumentedTest.useAppContext(ExampleInstrumentedTest.java:66)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
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)
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 android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
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 android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1867)

Tests ran to completion.

谢谢:)

0 个答案:

没有答案