让XCUIElement为Automate UITest执行3D触摸?

我正在创建Automate UITest测试用例,我想在用户与元素进行3D Touch交互时测试场景,然后显示Peek和Pop视图。

我似乎找不到任何可能的方法来模拟元素上的3D Touch并继续。

任何人都对此有任何想法,或3D Touch仍无法测试?

谢谢

我能够在运行iOS 10.3的iPhone 7上的应用程序图标上执行强制按下/ 3D Touch。 在10.2上是不可能的。

Objective-C的

首先,您必须声明以下内容或导入此标头

typedef void (^CDUnknownBlockType)(void); @interface XCEventGenerator : NSObject + (id)sharedGenerator; // iOS 10.3 specific - (double)forcePressAtPoint:(struct CGPoint)arg1 orientation:(long long)arg2 handler:(CDUnknownBlockType)arg3; @end 

然后执行强制按下

 XCUIElement *element = ...; // Get your element XCUICoordinate *coord = [mapsIcon coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)]; [[XCEventGenerator sharedGenerator] forcePressAtPoint:coord.screenPoint orientation:0 handler:^{}]; // handler cannot be nil 

在这里,我在地图图标上执行强制按下。

斯威夫特(未经测试)

对于Swift,您必须声明/导入与上面相同的接口/标头,然后像这样执行强制按下

 let el = ...; // Get your element let coord = el.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5) ) let eventGenerator: XCEventGenerator = XCEventGenerator.sharedGenerator() as! XCEventGenerator eventGenerator.forcePress(at: coord.screenPoint, orientation: 0) { }