我正在尝试自动关闭并重新启动iOS应用程序,同时保留模拟器中的状态。
谷歌搜索告诉我在我的应用程序中添加一个按钮来调用exit(0)
,但这是重置应用程序状态,尽管许多线程说它没有(尽管苹果很久以前就说它不可靠(。
另一个线程说使用 abort
,但这类似于抛出异常,绝对不是正确的事情。
执行 [自动] 手动步骤(双击主屏幕按钮并将应用程序滑动至死亡(会断开调试器的连接,因此另一个选项失败。
测试状态还原的正确过程是:单击模拟器上的"主页"按钮一次,以便应用后台并保存状态。现在,回到 Xcode,停止并重新启动。
您可以像这样模拟单击"主页"按钮
XCUIDevice.shared.press(.home)
然后调用此函数以启动您的应用程序
XCUIApplication().launch()
根据launch()
文档,它应该终止您的应用程序并重新启动它:
* Launches the application synchronously. On return the application ready to handle events. If the * application is already running, the existing instance will be terminated to ensure a clean state * for the launched instance. * * Any failure in the launch sequence will be reported as a test failure and the test will be halted * at that point.