使用 UI 测试滚动单元格

Scroll the cells using UI Testing

有没有类似的方法

- (void)scrollByDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY;

对于 iOS?

我觉得上面的方法只适用于OSX。 我想根据提供的增量值滚动我的表格视图。

提前致谢。

在iOS上,如果你想按元素移动,可以使用XCUIElement.press(forDuration:thenDragTo:)

相对坐标移动,可以得到一个元素的XCUICoordinate,然后用XCUICoordinate.press(forDuration:thenDragTo:).

let table = XCUIApplication().tables.element(boundBy:0)

// Get the coordinate for the bottom of the table view
let tableBottom = table.coordinate(withNormalizedOffset:CGVector(dx: 0.5, dy: 1.0))

// Scroll from tableBottom to new coordinate
let scrollVector = CGVector(dx: 0.0, dy: -30.0) // Use whatever vector you like
tableBottom.press(forDuration: 0.5, thenDragTo: tableBottom.withOffset(scrollVector))

或在Objective-C:

XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *table = [app.tables elementBoundByIndex: 0];

// Get the coordinate for the bottom of the table view
XCUICoordinate *tableBottom = [table coordinateWithNormalizedOffset:CGVectorMake(0.5, 1.0)];

// Scroll from tableBottom to new coordinate
CGVector scrollVector = CGVectorMake(0.0, -30.0); // Use whatever vector you like
[tableBottom pressForDuration:0.5 thenDragToCoordinate:[tableBottom coordinateWithOffset:scrollVector]];

Oletha 的回答正是我要找的,但 Objective-C 示例中有几个小错误。由于编辑被拒绝,我将其包含在此处作为对其他任何人的回复:

XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *table = [app.tables elementBoundByIndex: 0];

// Get the coordinate for the bottom of the table view
XCUICoordinate *tableBottom = [table 
coordinateWithNormalizedOffset:CGVectorMake( 0.5, 1.0)];

// Scroll from tableBottom to new coordinate
CGVector scrollVector = CGVectorMake( 0.0, -30.0); // Use whatever vector you like
[tableBottom pressForDuration:0.5 thenDragToCoordinate:[tableBottom coordinateWithOffset:scrollVector]];

这个 Swift4 版本对我有用。希望对以后的人有帮助。

let topCoordinate = XCUIApplication().statusBars.firstMatch.coordinate(withNormalizedOffset: .zero)
let myElement = XCUIApplication().staticTexts["NameOfTextLabelInCell"].coordinate(withNormalizedOffset: .zero)
// drag from element to top of screen (status bar)
myElement.press(forDuration: 0.1, thenDragTo: topCoordinate)