触摸特定坐标(UI 测试)

Touch specific coordinate (UI tests)

如何使用 UITests 触摸特定坐标?

当我在特定位置录制点击时,我有类似的东西:

XCUIApplication *app = [[XCUIApplication alloc] init];
[[[[[[[[[[app.otherElements containingType:XCUIElementTypeNavigationBar 
identifier:@"Navigation Bar title"] 
childrenMatchingType:XCUIElementTypeOther].element 
childrenMatchingType:XCUIElementTypeOther].element 
childrenMatchingType:XCUIElementTypeOther].element 
childrenMatchingType:XCUIElementTypeOther] elementBoundByIndex:0] 
childrenMatchingType:XCUIElementTypeOther].element 
childrenMatchingType:XCUIElementTypeOther] 
elementBoundByIndex:0].staticTexts[@"Action"] tap];

您只能点击参考已知元素的特定坐标。也就是说,您不能点击坐标 (20, 400) 处的像素。相反,您需要找到一个元素,然后点击具有偏移量的东西。

XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *label = app.labels[@"Label Name"];
XCUICoordinate *coordinate = [label coordinateWithNormalizedOffset(CGVectorMake(0.5, 1.2));
[coordinate tap];

我在 UI Testing Cheat Sheet post.

中记录了有关如何计算正确偏移量的更多信息

如果您只是想点击 Action 按钮,您可以直接访问它(而不是向下钻取所有这些查询)。

XCUIApplication *app = [[XCUIApplication alloc] init];
[[[app.navigationBars element].staticTexts[@"Action"] tap];

要点击屏幕的特定坐标,请从 window 元素构建一个 XCUICoordinate

XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *window = [app.windows elementAtIndex:0];
// Get co-ordinate for the top left corner of the screen
XCUICoordinate *origin = [window coordinateWithNormalizedOffset:CGVectorMake(0.0, 0.0)];
// Get coordinate relative to the top left of the screen
XCUICoordinate *myCoordinate = [origin coordinateWithOffset:CGVectorMake(40.0, 100.0)];

[coordinate tap];

是swift版本

let point = app.labels["labelName"].coordinate(withNormalizedOffset: CGVector(dx: 0, dy:0))
point.tap()