触摸特定坐标(UI 测试)

lan*_*rey 2 ios xcode-ui-testing

如何使用 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];
Run Code Online (Sandbox Code Playgroud)

Joe*_*tti 5

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

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

我在我的UI 测试备忘单帖子中记录了有关如何确定正确偏移量的更多信息。


如果您只是想点击Action按钮,您可以直接访问它(而不是深入了解所有这些查询)。

XCUIApplication *app = [[XCUIApplication alloc] init];
[[[app.navigationBars element].staticTexts[@"Action"] tap];
Run Code Online (Sandbox Code Playgroud)

  • 今天我了解了什么是归一化偏移。假设你找到了一个按钮 XYZ,那么在 XYZ.cooperative(withNormalizedOffset: CGVector(dx, dy)) 中,dx,dy = 0,0 将是其左上角。dx,dy = 1,1 将是其右下角角. dx,dy = 0.5,0.5 将是它的中间 (2认同)