XCTest UI测试-如何在不重新启动的情况下关闭和打开应用程序? [英] XCTest UI Testing - How to close and open an app without relaunch?
问题描述
我想让我的应用程序进入后台,然后又回到前台.
I want to make my app to go to background, and then comeback to the foreground.
要使应用程序进入后台,请执行以下操作: XCUIDevice.shared().press(XCUIDeviceButton.home)
To make an app to go background: XCUIDevice.shared().press(XCUIDeviceButton.home)
要终止应用(强制点击): XCUIApplication().terminate()
To terminate an app(force tap): XCUIApplication().terminate()
要启动该应用程序: XCUIApplication().launch()
问题:当我尝试关闭并打开该应用程序时,launch()方法从后台清除该应用程序,并重新打开该应用程序.
Problem: when I try to close and open the app, the launch() method clears the app from background and it opens the app freshly.
我看到了此评论.但是无法在UI测试中弄清楚.我正在使用Swift.需要帮助!
I saw this comment regarding this. But cant able to figure out it in UI test. I'm using Swift. Help needed!!
推荐答案
从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.
这篇关于XCTest UI测试-如何在不重新启动的情况下关闭和打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!