可能将应用程序从背景到前景?
在运行XCT UItesting时,可以将应用程序在后台进行testing:
XCUIDevice().pressButton(XCUIDeviceButton.Home)
它可能以某种方式使应用程序回到前台(活动状态),而无需重新启动应用程序?
Xcode 9的更新 :从Xcode 9开始,现在可以简单地在任何XCUIApplication上调用activate()
。
let myApp = XCUIApplication() myApp.activate() // bring to foreground
https://developer.apple.com/documentation/xctest/xcuiapplication/2873317-activate
是的。 但是,你需要XCUIElement的私有头文件(这可以通过Facebook的头文件转储)。 为了前台应用程序,您需要调用解决scheme ,我相信parsing元素的查询(对于应用程序意味着前台应用程序)。
对于Swift,您必须将XCUIElement.h导入您的桥接头。 对于Objective-C,您只需要导入XCUIElement.h。
随着应用程序后台:
迅速:
XCUIApplication().resolve()
Objective-C的
[[XCUIApplication new] resolve];
如果这是你需要的唯一的function,你可以写一个快速的ObjC类别。
@interface XCUIElement (Tests) - (void) resolve; @end
如果您需要启动/解决其他应用程序。 Facebook在这里通过Springboard就有一个例子。
从Xcode 8.3和iOS 10.3开始,你可以用Siri完成这个工作:
XCUIDevice.shared().press(XCUIDeviceButton.home) XCUIDevice.shared().siriService.activate(voiceRecognitionText: "Open {appName}")
在testing套件文件的顶部包含@available(iOS 10.3, *)
,你应该很好的去!
如果有人需要从后台移动应用程序,我已经写了(基于上面的答案)类别,真正有效(非常感谢指向FB Git)
@implementation XCUIApplication(SpringBoard) + (instancetype)springBoard { XCUIApplication * springboard = [[XCUIApplication alloc] performSelector:@selector(initPrivateWithPath:bundleID:) withObject:nil withObject:@"com.apple.springboard"]; [springboard performSelector:@selector(resolve) ]; return springboard; } - (void)tapApplicationWithIdentifier:(NSString *)identifier { XCUIElement *appElement = [[self descendantsMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"identifier = %@", identifier] ]; [appElement tap]; } @end
对于Swift,需要在Brigding-Header.h中声明XCUIApplication私有方法接口,如下所示:
@interface XCUIApplication (Private) - (id)initPrivateWithPath:(NSString *)path bundleID:(NSString *)bundleID; - (void)resolve; @end
然后在您的testing用例中调用resolve()以将应用程序恢复:
XCUIApplication().resolve()