使用UI测试滚动单元格

时间:2016-12-02 02:49:41

标签: ios uitableview scroll xctest

是否有类似

的方法
- (void)scrollByDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY;

for iOS?

我认为上述方法仅适用于OSX。 我想根据提供的deltaval来滚动我的tableview。

提前致谢。

3 个答案:

答案 0 :(得分:6)

在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]];

答案 1 :(得分:5)

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]];

答案 2 :(得分:4)

这个适用于我的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)