2017-06-23 34 views
0

我想使用uiautomator使用Google's sample app撤销在应用程序的详细信息我的应用程序的权限。但是,我遇到了问题,我的uiautomator似乎得到了错误的切换状态。当检查切换时,由于某些原因,uiautomator仍然会返回我falseuiautomator单击/撤销申请许可列表

我的最终目标是从具有不同索引的相同布局中逐一切换按钮。

enter image description here

@Test 
public void clickThruSettings() throws UiObjectNotFoundException { 

// go to the target page and open the permissions list. 
    Context context = InstrumentationRegistry.getContext(); 
    final Intent intent = new Intent(); 
    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    Uri uri = Uri.fromParts("package", BASIC_SAMPLE_PACKAGE, null); 
    intent.setData(uri); 
    context.startActivity(intent); 
    UiObject permissions = mDevice.findObject(new UiSelector().text("Permissions")); 
    permissions.clickAndWaitForNewWindow(); 

// toggle on and off with index for linearLayout. 
    togglePermissionButton(true,0); 
    togglePermissionButton(false,1); 
    togglePermissionButton(true,2); 
    togglePermissionButton(false,3); 
    } 

private void togglePermissionButton(boolean enabled, int index) throws UiObjectNotFoundException { 
    UiObject permissionButton = mDevice.findObject(new UiSelector().className("android.widget.LinearLayout") 
      .scrollable(false).index(index)).getChild(new UiSelector().className(android.widget.Switch.class 
      .getName())); 
    if (permissionButton.waitForExists(2000)&& permissionButton.exists()&& permissionButton.isEnabled()) { 
     if (permissionButton.isChecked() != enabled) { 
      permissionButton.click(); 
     } 
    } 
} 

任何想法?

回答

0

索引是基于它的父类,不能用这样的。 您可以直接找到具有实例属性的交换机。

​​