可以将应用程序从后台带到前台吗?

时间:2022-06-15 01:28:58

When running an XCT UI test it is possible to put the application under test in the background with:

运行XCT UI测试时,可以在后台运行正在测试的应用程序:

XCUIDevice().pressButton(XCUIDeviceButton.Home)

It it possible in some way to bring the app back to foreground (active state) without relaunching the application?

它可以以某种方式将应用程序恢复到前台(活动状态)而无需重新启动应用程序?

4 个解决方案

#1


3  

Update for Xcode 9: Starting in Xcode 9, you can now simply call activate() on any XCUIApplication.

Xcode 9的更新:从Xcode 9开始,您现在可以在任何XCUIApplication上调用activate()。

let myApp = XCUIApplication()
myApp.activate() // bring to foreground

https://developer.apple.com/documentation/xctest/xcuiapplication/2873317-activate

https://developer.apple.com/documentation/xctest/xcuiapplication/2873317-activate


Yes, it is. But, you'll need XCUIElement's private headers (which are available via header dump from Facebook here). In order to foreground the app, you need to call resolve which I believe resolves the element's query (which for applications means foregrounding the app).

是的。但是,你需要XCUIElement的私有标题(这里可以通过Facebook的标题转储获得)。为了使应用程序前景化,您需要调用我认为解析元素查询的解析(对于应用程序来说,这意味着应用程序的前景)。

For Swift, you'll have to import the XCUIElement.h into your bridging header. For Objective-C you'll just need to import XCUIElement.h.

对于Swift,您必须将XCUIElement.h导入到桥接头中。对于Objective-C,您只需要导入XCUIElement.h。

With the app backgrounded:

应用程序背景:

Swift:

迅速:

XCUIApplication().resolve()

Objective-C

Objective-C的

[[XCUIApplication new] resolve];

If this is the only functionality you need, you could just write a quick ObjC category.

如果这是您需要的唯一功能,您可以编写一个快速的ObjC类别。

@interface XCUIElement (Tests)
- (void) resolve;
@end

If you need to launch / resolve another app. Facebook has an example of that here by going through the Springboard.

如果您需要启动/解决其他应用。通过跳板,Facebook就是这方面的一个例子。

#2


4  

As of Xcode 8.3 and iOS 10.3, you can accomplish this with Siri:

从Xcode 8.3和iOS 10.3开始,您可以使用Siri完成此任务:

XCUIDevice.shared().press(XCUIDeviceButton.home)
XCUIDevice.shared().siriService.activate(voiceRecognitionText: "Open {appName}")

Include @available(iOS 10.3, *) at the top of your test suite file and you should be good to go!

在测试套件文件的顶部包含@available(iOS 10.3,*),你应该好好去!

#3


0  

If somebody needs just move app back from background i have written (based on answer above) category that really works(great thanks to pointing to FB git)

如果有人需要从背景中移动应用程序我已经写了(基于上面的答案)类别真的有效(非常感谢指向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

#4


0  

For Swift, you need to declare the XCUIApplication private methods interface in Brigding-Header.h like this:

对于Swift,您需要在Brigding-Header.h中声明XCUIApplication私有方法接口,如下所示:

@interface XCUIApplication (Private)
- (id)initPrivateWithPath:(NSString *)path bundleID:(NSString *)bundleID;
- (void)resolve;
@end

Then call resolve() in your test cases to bring the app back:

然后在测试用例中调用resolve()以恢复应用程序:

XCUIApplication().resolve()

#1


3  

Update for Xcode 9: Starting in Xcode 9, you can now simply call activate() on any XCUIApplication.

Xcode 9的更新:从Xcode 9开始,您现在可以在任何XCUIApplication上调用activate()。

let myApp = XCUIApplication()
myApp.activate() // bring to foreground

https://developer.apple.com/documentation/xctest/xcuiapplication/2873317-activate

https://developer.apple.com/documentation/xctest/xcuiapplication/2873317-activate


Yes, it is. But, you'll need XCUIElement's private headers (which are available via header dump from Facebook here). In order to foreground the app, you need to call resolve which I believe resolves the element's query (which for applications means foregrounding the app).

是的。但是,你需要XCUIElement的私有标题(这里可以通过Facebook的标题转储获得)。为了使应用程序前景化,您需要调用我认为解析元素查询的解析(对于应用程序来说,这意味着应用程序的前景)。

For Swift, you'll have to import the XCUIElement.h into your bridging header. For Objective-C you'll just need to import XCUIElement.h.

对于Swift,您必须将XCUIElement.h导入到桥接头中。对于Objective-C,您只需要导入XCUIElement.h。

With the app backgrounded:

应用程序背景:

Swift:

迅速:

XCUIApplication().resolve()

Objective-C

Objective-C的

[[XCUIApplication new] resolve];

If this is the only functionality you need, you could just write a quick ObjC category.

如果这是您需要的唯一功能,您可以编写一个快速的ObjC类别。

@interface XCUIElement (Tests)
- (void) resolve;
@end

If you need to launch / resolve another app. Facebook has an example of that here by going through the Springboard.

如果您需要启动/解决其他应用。通过跳板,Facebook就是这方面的一个例子。

#2


4  

As of Xcode 8.3 and iOS 10.3, you can accomplish this with Siri:

从Xcode 8.3和iOS 10.3开始,您可以使用Siri完成此任务:

XCUIDevice.shared().press(XCUIDeviceButton.home)
XCUIDevice.shared().siriService.activate(voiceRecognitionText: "Open {appName}")

Include @available(iOS 10.3, *) at the top of your test suite file and you should be good to go!

在测试套件文件的顶部包含@available(iOS 10.3,*),你应该好好去!

#3


0  

If somebody needs just move app back from background i have written (based on answer above) category that really works(great thanks to pointing to FB git)

如果有人需要从背景中移动应用程序我已经写了(基于上面的答案)类别真的有效(非常感谢指向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

#4


0  

For Swift, you need to declare the XCUIApplication private methods interface in Brigding-Header.h like this:

对于Swift,您需要在Brigding-Header.h中声明XCUIApplication私有方法接口,如下所示:

@interface XCUIApplication (Private)
- (id)initPrivateWithPath:(NSString *)path bundleID:(NSString *)bundleID;
- (void)resolve;
@end

Then call resolve() in your test cases to bring the app back:

然后在测试用例中调用resolve()以恢复应用程序:

XCUIApplication().resolve()