Android13 平板项目测试GtsPermissionControllerHostTestCases Failed,失败日志如下:
java.lang.AssertionError: on-device tests failed:
com.android.gts.permissioncontroller.PermissionControllerTest29#testGranted:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.test.uiautomator.UiObject2.isChecked()' on a null object reference
at com.android.gts.permissioncontroller.BasePermissionsTest.setPermissionGrantState(BasePermissionsTest.java:527)
at com.android.gts.permissioncontroller.BasePermissionsTest.setPermissionGrantState(BasePermissionsTest.java:502)
at com.android.gts.permissioncontroller.BasePermissionsTest.grantPermissions(BasePermissionsTest.java:393)
at com.android.gts.permissioncontroller.BasePermissionsTest.grantPermission(BasePermissionsTest.java:389)
at com.android.gts.permissioncontroller.PermissionControllerTest29.testGranted(PermissionControllerTest29.java:103)
at android.permissioncontroller.gts.Utils.runDeviceTests(Utils.java:130)
at android.permissioncontroller.gts.Utils.runDeviceTests(Utils.java:80)
at android.permissioncontroller.gts.Utils.runDeviceTestsAsCurrentUser(Utils.java:44)
at android.permissioncontroller.gts.PermissionControllerHostTest.testGrantAppPermissionFragment(PermissionControllerHostTest.java:229)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:61)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at com.android.tradefed.testtype.DeviceJUnit4ClassRunner.runChild(DeviceJUnit4ClassRunner.java:111)
at com.android.tradefed.testtype.DeviceJUnit4ClassRunner.runChild(DeviceJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at com.android.tradefed.testtype.DeviceJUnit4ClassRunner.run(DeviceJUnit4ClassRunner.java:147)
at com.android.tradefed.testtype.junit4.ExceptionThrowingRunnerWrapper.run(ExceptionThrowingRunnerWrapper.java:43)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.android.tradefed.testtype.HostTest.runJUnit4Tests(HostTest.java:736)
at com.android.tradefed.testtype.HostTest.runTestClasses(HostTest.java:616)
at com.android.tradefed.testtype.HostTest.run(HostTest.java:564)
at com.android.compatibility.common.tradefed.testtype.JarHostTest.run(JarHostTest.java:56)
at com.android.tradefed.testtype.suite.GranularRetriableTestWrapper.intraModuleRun(GranularRetriableTestWrapper.java:379)
at com.android.tradefed.testtype.suite.GranularRetriableTestWrapper.run(GranularRetriableTestWrapper.java:289)
at com.android.tradefed.testtype.suite.ModuleDefinition.run(ModuleDefinition.java:595)
at com.android.tradefed.testtype.suite.ITestSuite.runSingleModule(ITestSuite.java:951)
at com.android.tradefed.testtype.suite.ITestSuite.run(ITestSuite.java:828)
at com.android.tradefed.invoker.InvocationExecution.runTest(InvocationExecution.java:1359)
at com.android.tradefed.invoker.InvocationExecution.runTests(InvocationExecution.java:1138)
at com.android.tradefed.invoker.TestInvocation.prepareAndRun(TestInvocation.java:626)
at com.android.tradefed.invoker.TestInvocation.performInvocation(TestInvocation.java:278)
at com.android.tradefed.invoker.TestInvocation.invoke(TestInvocation.java:1357)
at com.android.tradefed.command.CommandScheduler$InvocationThread.run(CommandScheduler.java:686)
观察日志,大概意思是某个view是空,导致改测试项没继续下去,反编译该测试apk,查看源码如下:
代码很简单,就是去测试app会去申请CALENDAR 权限,具有权限以后,从日历的数据库查询一条数据,查询成功,该测试项视为PASS
申请权限,其实就是会跳转权限管理界面,也就是APP的详情界面--》Permissions-->权限列表,找到CALENDAR 权限,然后点击进去允许。。
我们失败的原因是最后一个界面的允许权限,没获取到对应的控件。仔细看下这块找控件的代码,其实是根据权限--》label-->然后找到这个view--》点击进去
我们这里就是
android.permission.READ_CALENDAR--》CALENDAR根据这个label找到view..
其实这里我们就能找到问题了,如果一个界面上有两个TextView的名字都是CALENDAR,那这里就不一定会是权限列表的那个TexviewView,接着就不会进到第三个界面,也就会造成测试报错那样,找不到getDenyButton()isChecked,也就终止了测试。
因为我们的是平板项目,Android12以后的大屏或者平板项目,底部的导航栏会变成显示一个任务栏,从 NavigationBar 变成 Taskbar ,Taskbar 允许一部分app常驻于此;
可以看到不管点进哪个app,底部都有个TaskBar,而且我们的TaskBar 常驻的app里面刚好有App叫CALENDAR,这就会导致我们在测试的时候,第二个界面就会点击到底部TaskBar的CALENDAR
这就是我们测试失败的原因。修改方法也很简单,PermissionControllerTest29 APP内涉及的权限,底部不包含这些权限对应Label同名的APP就可以了
也就是我们底部的TASKBAR 不能包含APP名字为:Contacts,Calendar,Location,Camera即可。移除以后,复测PASS