定位元素的对象,可以理解为存放控件的容器

方法

elementAtIndex

获得传入的索引值所在的元素,返回XCUIElement对象。只能从当前对象的查找。更深层次的元素不在查找范围内

elementMatchingPredicate

根据NSPredicate定义的匹配条件查找元素。返回XCUIElement对象。只能从当前对象中查找。更深层次的元素不在查找范围内

elementMatchingType

根据元素类型(XCUIElementType)和id号来匹配查找元素。返回XCUIElement对象。只能从当前对象中查找。更深层次的元素不在查找范围内

descendantsMatchingType

传入XCUIElementType作为匹配条件,得到匹配的XCUIElementQuery对象,查找对象为当前控件的子子孙孙控件。返回XCUIElementQuery对象

childrenMatchingType

传入XCUIElementType作为匹配条件,得到匹配的XCUIElementQuery对象,查找对象为当前控件的儿子控件。返回XCUIElementQuery对象

matchingPredicate

传入NSPredicate作为过滤器,得到XCUIElementQuery对象。返回XCUIElementQuery对象

matchingType

传入XCUIElementType和id号作为匹配条件,得到XCUIElementQuery。返回XCUIElementQuery对象

matchingIdentifier

传入id号作为匹配条件,得到XCUIElementQuery。返回XCUIElementQuery对象

containingPredicate

传入NSPredicate过滤器作为匹配条件。从子节点中找到包含该条件的XCUIElementQuery对象

containingType

传入XCUIElementType和id作为匹配条件。从子节点中找到包含该条件的XCUIElementQuery对象。

属性

element

query用element表示形式,如果query中只有一个元素,可以讲element当成真正的element,执行点击等操作,从这一方面来讲XCUIElementQuery其实也是一种XCUIElement对象,只是是用来存放0~N个XCUIElement的容器。得到XCUIElement对象。

count

query中找到的元素数量,得到整数。

allElementsBoundByAccessibilityElement

query中根据accessibility element得到的元素数组。得到XCUIElement数组

allElementsBoundByIndex

query中根据索引值得到的元素数组。得到XCUIElement数组

debugDescription

调试信息

下标

subscript (key: String) -> XCUIElement { get } 使得我们下面通过下标定位成为了可能。返回XCUIElement对象

app.tables.staticTexts["Groceries"]

实例

使用element开头的三个方法查找


func testXCUIElementQueryByElement() {
        // Use recording to get started writing UI tests.
        // Use XCTAssert and related functions to verify your tests produce the correct results.

        let app = XCUIApplication()
        //获取所有textField的query对象
        let query = app.windows.textFields
        let element = query.element
        //创建匹配器,匹配placeholderValue的值为Type in number的控件
        let predicate = NSPredicate(format: "placeholderValue == %@", "Type in number")

        let button = query.elementMatchingPredicate(predicate);
        let button2 = query.elementAtIndex(0)



        if(button.exists){
            button.tap()
            button.typeText("button")
        }
        if(button2.exists){
            button2.tap()
            button2.typeText("button2")
        }
        //创建匹配器,匹配placeholderValue的值为Type in number且value值为Hello的控件
        let predicate1 = NSPredicate(format: "value == %@ AND placeholderValue == %@", "button","Type in number")
        let button3 = query.elementMatchingPredicate(predicate1);
        if(button3.exists){
            button3.tap()
            button3.typeText("button3")
        }

        //根据elementMatchingType方法查找元素
        let button4 = query.elementMatchingType(.TextField, identifier: "")
        if(button4.exists){
            button4.tap()
            button4.typeText("button4")
        }
    }

源码

Swift


/*! Object for locating elements that can be chained with other queries. */
@available(iOS 9.0, *)
class XCUIElementQuery : NSObject, XCUIElementTypeQueryProvider {

    /*! Returns an element that will use the query for resolution. */
    var element: XCUIElement { get }

    /*! Evaluates the query at the time it is called and returns the number of matches found. */
    var count: UInt { get }

    /*! Returns an element that will resolve to the index into the query's result set. */
    func elementAtIndex(index: UInt) -> XCUIElement

    /*! Returns an element that matches the predicate. */
    func elementMatchingPredicate(predicate: NSPredicate) -> XCUIElement

    /*! Returns an element that matches the type and identifier. */
    func elementMatchingType(elementType: XCUIElementType, identifier: String?) -> XCUIElement

    subscript (key: String) -> XCUIElement { get }

    /*! Immediately evaluates the query and returns an array of elements bound to the resulting accessibility elements. */
    var allElementsBoundByAccessibilityElement: [XCUIElement] { get }

    /*! Immediately evaluates the query and returns an array of elements bound by the index of each result. */
    var allElementsBoundByIndex: [XCUIElement] { get }

    /*! Returns a new query that finds the descendants of all the elements found by the receiver. */
    func descendantsMatchingType(type: XCUIElementType) -> XCUIElementQuery

    /*! Returns a new query that finds the direct children of all the elements found by the receiver. */
    func childrenMatchingType(type: XCUIElementType) -> XCUIElementQuery

    /*! Returns a new query that applies the specified attributes or predicate to the receiver. */
    func matchingPredicate(predicate: NSPredicate) -> XCUIElementQuery
    func matchingType(elementType: XCUIElementType, identifier: String?) -> XCUIElementQuery
    func matchingIdentifier(identifier: String) -> XCUIElementQuery

    /*! Returns a new query for finding elements that contain a descendant matching the specification. */
    func containingPredicate(predicate: NSPredicate) -> XCUIElementQuery
    func containingType(elementType: XCUIElementType, identifier: String?) -> XCUIElementQuery

    /*!
     @discussion
     Provides debugging information about the query. The data in the string will vary based on the time
     at which it is captured, but it may include any of the following as well as additional data:
        • A description of each step of the query.
        • Information about the inputs and matched outputs of each step of the query.
     This data should be used for debugging only - depending on any of the data as part of a test is unsupported.
     */
    var debugDescription: String { get }
}

OC


NS_CLASS_AVAILABLE(10_11, 9_0)
@interface XCUIElementQuery : NSObject <XCUIElementTypeQueryProvider>

/*! Returns an element that will use the query for resolution. */
@property (readonly) XCUIElement *element;

/*! Evaluates the query at the time it is called and returns the number of matches found. */
@property (readonly) NSUInteger count;

/*! Returns an element that will resolve to the index into the query's result set. */
- (XCUIElement *)elementAtIndex:(NSUInteger)index;

/*! Returns an element that matches the predicate. */
- (XCUIElement *)elementMatchingPredicate:(NSPredicate *)predicate;

/*! Returns an element that matches the type and identifier. */
- (XCUIElement *)elementMatchingType:(XCUIElementType)elementType identifier:(nullable NSString *)identifier;

/*! Keyed subscripting is implemented as a shortcut for matching an identifier only. For example, app.descendants["Foo"] -> XCUIElement. */
- (XCUIElement *)objectForKeyedSubscript:(NSString *)key;

/*! Immediately evaluates the query and returns an array of elements bound to the resulting accessibility elements. */
@property (readonly, copy) NSArray<XCUIElement *> *allElementsBoundByAccessibilityElement;

/*! Immediately evaluates the query and returns an array of elements bound by the index of each result. */
@property (readonly, copy) NSArray<XCUIElement *> *allElementsBoundByIndex;

/*! Returns a new query that finds the descendants of all the elements found by the receiver. */
- (XCUIElementQuery *)descendantsMatchingType:(XCUIElementType)type;

/*! Returns a new query that finds the direct children of all the elements found by the receiver. */
- (XCUIElementQuery *)childrenMatchingType:(XCUIElementType)type;

/*! Returns a new query that applies the specified attributes or predicate to the receiver. */
- (XCUIElementQuery *)matchingPredicate:(NSPredicate *)predicate;
- (XCUIElementQuery *)matchingType:(XCUIElementType)elementType identifier:(nullable NSString *)identifier;
- (XCUIElementQuery *)matchingIdentifier:(NSString *)identifier;

/*! Returns a new query for finding elements that contain a descendant matching the specification. */
- (XCUIElementQuery *)containingPredicate:(NSPredicate *)predicate;
- (XCUIElementQuery *)containingType:(XCUIElementType)elementType identifier:(nullable NSString *)identifier;

/*!
 @discussion
 Provides debugging information about the query. The data in the string will vary based on the time
 at which it is captured, but it may include any of the following as well as additional data:
    • A description of each step of the query.
    • Information about the inputs and matched outputs of each step of the query.
 This data should be used for debugging only - depending on any of the data as part of a test is unsupported.
 */
@property (readonly, copy) NSString *debugDescription;
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐