2016-12-02 76 views
2

細胞有沒有像滾動使用UI測試

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

適用於iOS的方法?

我覺得上面的方法只適用於OSX。 我想滾動我的tableview根據提供的deltavalues。

在此先感謝。

回答

2

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

這段代碼適用於我,但有一些更改(我在WebView中使用它而不是表) 。首先''.coordinate'方法的參數在'Normalized'字中帶有'z'(對於Swift部分);第二,我不得不將這個標準化的偏移量定位到WebView的中心(因爲WebView的底部有一些按鈕,所以它們通過了'dx'和'dy'的0.5)第三,我不得不將初始的持續時間從0.5降低到0.2,因爲持續半秒導致WebView選擇文本。 – lagoman

+0

更新了我的代碼片段,使z在規範化中,對此感到抱歉! – Oletha

2

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