XCUIElementQuery API

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

方法

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;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您的问题不太清晰,我猜测您想问的是如何使用 Python 进行 iOS 自动化测试时的 OCR 文字识别和坐标定位。 OCR 文字识别可以使用 Python 的第三方库 pytesseract 来实现,它是一个基于 Google 的开源 OCR 引擎 Tesseract 的 Python 封装。您可以通过以下命令安装: ``` pip install pytesseract ``` 在使用 pytesseract 进行 OCR 识别时,您需要先对需要识别的区域进行截图,然后使用 pytesseract.image_to_string() 方法来获取识别结果。例如: ```python import pytesseract from PIL import Image # 截图 im = Image.open('screenshot.png') region = im.crop((x1, y1, x2, y2)) # OCR 识别 text = pytesseract.image_to_string(region, lang='eng') print(text) ``` 坐标定位可以通过 iOS 自动化测试框架 XCTest 来实现。您可以使用 XCTest 提供的 XCUIElementQuery 来查找您需要的 UI 元素,然后使用 XCUIElement 的坐标信息来进行操作。例如: ```python import time import unittest from appium import webdriver class iOSAutomationTest(unittest.TestCase): def setUp(self): desired_caps = { "platformName": "iOS", "platformVersion": "14.5", "deviceName": "iPhone 12", "app": "/path/to/your/app", "automationName": "XCUITest", "udid": "your-device-udid" } self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) def tearDown(self): self.driver.quit() def test_example(self): # 查找 UI 元素 button = self.driver.find_element_by_xpath("//XCUIElementTypeButton[@name='Example']") # 获取坐标信息 x = button.location['x'] y = button.location['y'] # 点击 self.driver.tap([(x, y)]) # 等待 time.sleep(2) if __name__ == '__main__': unittest.main() ``` 需要注意的是,iOS 自动化测试需要使用 Appium 作为驱动,并且需要在您的 iOS 设备上安装 Appium 的 WebDriverAgent 服务。同时,您需要在 Appium 中设置正确的 capabilities 和启动参数,才能进行 iOS 自动化测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值