Java+Uiautomator自动化测试 -- 10.UiCollection学习

UiAutomator1.0主要的API汇总

官网介绍:

The UI Automator APIs allow you to write robust tests without needing to know about the implementation details of the app that you are targeting. You can use these APIs to capture and manipulate UI components across multiple apps:

  • UiCollection: Enumerates a container's UI elements for the purpose of counting, or targeting sub-elements by their visible text or content-description property.
  • UiObject: Represents a UI element that is visible on the device.
  • UiScrollable: Provides support for searching for items in a scrollable UI container.
  • UiSelector: Represents a query for one or more target UI elements on a device.
  • Configurator: Allows you to set key parameters for running UI Automator tests.

UiCollection

 

一 类说明

a. UiCollection是UiObject的子类

b. UiCollection代表元素条目的集合

 

二 UiCollection功能

先按照一定的条件列举出容器中的所有符合条件UI元素,然后在这些符合条件的元素中通过其可见文本或内容描述属性对子元素进行计数或定位。

public class UiCollection extends UiObject 

继承的常量:

 androidx.test.uiautomator.UiObject

 

三 UiCollection使用场景

a. 一般使用容器类组件作为父类

b. 一般使用在需要找子类并且子类由于某些因素不好定位

c. 获取某一类的数量,比如获取短信列表下面短信的数量

 

 

四. 使用文本与描述条件从集合中查找对象

public UiObject getChildByDescription (UiSelector childPattern, String text)
pubilc UiObject getChildByText (UiSelector, childPattern, String text)
pubilc UiObject getChildByInstance (UiSelector childPattern, int instance)

如上所说,在UiSelector选择器的查找条件中从子ui元素中搜索,递归寻找所有符合条件的子集。然后用描述,文本,实例条件搜索元素。

a. childPattern:UiSelector从子元素中选择的条件

b. text,instance都是从搜索出来的元素中再次用描述,文本条件搜出来的元素。

返回值:UIObject

抛出异常:UIObjectNotFoundException

 

五. 获取某种搜索条件组合的数量

public int getChildCount(UiSelector childPattern)

按照查找条件递归的查找所有符合条件的子元素集合的数量

 

六. 代码栗子

需求:

一个手机页面上同时存在很多相同的按键,我们需要找到我们需要的控件,然后enable和disable这个控件,如下图所示:

 

这里我们需要去打开和关闭Google go的开关,假如这个页面上只有这一个那就好办了,我们用getText() 获取这个控件的开和关闭值,然后写个判断值是开我就关闭,是关闭的我就点击控件打开,很简单。

但是如上图,这个办法就不行了,因为他的控件都是一样的,只有text会有变化,是ON和OFF,这个时候需要精确的打开Google Go就需要我们的Collection了。

 

代码:

private void openAppSwitch(String appName, boolean on) throws UiObjectNotFoundException {
        UiCollection collection = new UiCollection(new UiSelector().className("android.widget.LinearLayout"));
        UiSelector appNameSelector = new UiSelector().resourceId("android:id/title").text(appName);
        UiSelector switchOffSelector = new UiSelector().resourceId("android:id/switch_widget")
                .className("android.widget.Switch").text("OFF");
        UiSelector switchOnSelector = new UiSelector().resourceId("android:id/switch_widget")
                .className("android.widget.Switch").text("ON");
        if (on) {
            if (collection.getChild(appNameSelector).exists() &&
                    collection.getChild(switchOffSelector).exists()) {
                new UiObject(appNameSelector).clickAndWaitForNewWindow(5000L);
            }
        } else {
            if (collection.getChild(appNameSelector).exists() &&
                    collection.getChild(switchOnSelector).exists()) {
                new UiObject(appNameSelector).clickAndWaitForNewWindow(5000L);
            }
        }
    }


@Test
    public void testSettings_0130_9_enableGoogleGoAppPermission() throws UiObjectNotFoundException {
        // 打开Google Go的权限
        mSettingsPage.enterAppsAndNotifications();
        AppsNotificationsPage appsNotificationsPage = new AppsNotificationsPage(mDevice);
        appsNotificationsPage.enterAppPermissions();
        appsNotificationsPage.enterCamera();
        openAppSwitch("Google Go", true);  //关闭,就false
    }

代码解释:

首先先获得父类的集合

然后找到符合条件的子元素,也就是Google Go 和 开关按钮  (android:id/switch_widget) 

然后判断 开关按钮是开还是关,是开的就点击Google Go这个空间关闭它,是关闭的就点击Google GO去打开它,完美解决问题;

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值