sau123
sau123

Reputation: 352

Scroll the cells using UI Testing

Is there a method like

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

for iOS?

I think the above method is only for OSX. I would like to scroll my tableview according to the deltavalues provided.

Thanks in advance.

Upvotes: 5

Views: 6363

Answers (3)

Jiraheta
Jiraheta

Reputation: 481

This Swift4 version that worked for me. Hope it helps someone in the future.

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)

Upvotes: 5

Oletha
Oletha

Reputation: 7649

On iOS, you can use XCUIElement.press(forDuration:thenDragTo:) if you want to move in terms of elements.

To move in terms of relative co-ordinates, you can get the XCUICoordinate of an element, and then use 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))

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

Upvotes: 7

Wade
Wade

Reputation: 180

Oletha's answer was exactly what I was looking for but there are a couple of minor mistakes in the Objective-C example. Since the edit was rejected, I'll include it here as a reply for anyone else that comes along:

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

Upvotes: 5

Related Questions