我怎样才能使用uiautomator视图的父? [英] How can I get the parent of a view using uiautomator?

查看:461
本文介绍了我怎样才能使用uiautomator视图的父?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一个UI元素的父视图这样我就可以通过用户界面自由航行。

例如,在设置应用程序,我能找到的文本视图蓝牙:

  UIObject的btView =新UIObject类(新UiSelector()文本(蓝牙));
 

现在,我卡住的部分是这个:我想浏览两级,并开始为开/关按钮启用和禁用蓝牙新的搜索

注:我能得到按钮,如果我用下面的code

  UIObject的btButtonView =新UIObject类(新UiSelector()类名(android.widget.Switch)实例(1));
 

本搜索开关按钮,返回第二次遭遇。我想搜索更precise并查找包含蓝牙文字线性布局的按钮。

更新: 这是设置应用程序的布局(蓝牙一部分,我需要):

 的LinearLayout
    的LinearLayout
        ImageView的
    RelativeLayout的
        的TextView(文本=蓝牙)
    开关 ()
 

解决方案

您需要使用文本找到UIObject的两个级别第一。这可以通过使用完成getChildByText()方法UiCollection或UiScrollable。然后你就可以很容易地找到开关。对于设置这个code ++工程,我的设备上:

  UiScrollable settingsList =新UiScrollable(新UiSelector()滚动(真)。);
UIObject类btItem = settingsList.getChildByText(新UiSelector()类名(LinearLayout.class.getName()),蓝牙,真正的);

UIObject的btSwitch = btItem.getChild(新UiSelector()的className(android.widget.Switch.class.getName())。);
btSwitch.click();
 

I'm trying to identify the parent view of an ui element so I can navigate through the UI freely.

For example, in Settings app, I can find the view with the text "Bluetooth":

UiObject btView = new UiObject(new UiSelector().text("Bluetooth"));

Now, the part where I get stuck is this one: I want to navigate two levels up and start a new search for the on/off button that enables and disables bluetooth.

Note: I can get the button if I use the code below.

UiObject btButtonView = new UiObject(new UiSelector().className("android.widget.Switch").instance(1));

This searches for switch buttons and returns the second encounter. I want the search to be more precise and look for the button in the linear layout that contains the "Bluetooth" text.

UPDATE: This is the layout of the Settings app (the Bluetooth part that I need):

LinearLayout
    LinearLayout
        ImageView
    RelativeLayout
        TextView (with text = "Bluetooth")
    Switch ()

解决方案

You need to find the UiObject two levels up first using the text. This can be done using the getChildByText() methods in UiCollection or UiScrollable. Then you can easily find the switch. For 'Settings' this code works on my device:

UiScrollable settingsList = new UiScrollable(new UiSelector().scrollable(true));
UiObject btItem = settingsList.getChildByText(new UiSelector().className(LinearLayout.class.getName()),"Bluetooth", true);

UiObject btSwitch = btItem.getChild(new UiSelector().className(android.widget.Switch.class.getName()));
btSwitch.click();

这篇关于我怎样才能使用uiautomator视图的父?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆