Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 289
sau123 Avatar asked Dec 02 '16 02:12

sau123


3 Answers

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]];
like image 54
Oletha Avatar answered Nov 15 '22 02:11

Oletha


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]];
like image 5
Wade Avatar answered Nov 15 '22 01:11

Wade


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)
like image 4
Jiraheta Avatar answered Nov 15 '22 01:11

Jiraheta