且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XCTest UI测试-如何在不重新启动的情况下关闭和打开应用程序?

更新时间:2023-10-16 10:18:46

从Xcode 9和iOS 11开始, XCUIApplication()具有您可以使用的 activate()方法可以用来重新启动该应用.

As of Xcode 9 and iOS 11, XCUIApplication() has an activate() method that you can use to relaunch the app.

按照brandenbyers的建议,您可以按下"主屏幕按钮以使应用程序后台运行,然后像这样再次激活它以避免使用Siri:

As brandenbyers suggested, you can "press" the home button to background your app, and then activate it again like this to avoid using Siri:

XCUIDevice.shared.press(.home)
XCUIApplication().activate()

请注意,这仅适用于使用XCUITest构建的目标,而不适用于XCTest.如果在XCTest构建的目标中尝试此操作,则所有XCUIApplication操作都将崩溃.

Note that this only works with targets built using XCUITest, not XCTest. If you try this within a target built from XCTest, all XCUIApplication operations will crash.