如何触摸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];
答案 0 :(得分:3)
您只能点击已知元素引用的特定坐标。意思是,您无法点击坐标(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帖子中找出正确偏移量的更多信息。
如果您只是想点击Action
按钮,则可以直接访问它(而不是深入查看所有这些查询)。
XCUIApplication *app = [[XCUIApplication alloc] init];
[[[app.navigationBars element].staticTexts[@"Action"] tap];
答案 1 :(得分:1)
要点按屏幕的特定坐标,请从窗口元素构建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];
答案 2 :(得分:0)
它是快速版本
let point = app.labels["labelName"].coordinate(withNormalizedOffset: CGVector(dx: 0, dy:0))
point.tap()