简体   繁体   English

Xcode 外部启动的UI测试

[英]Xcode UI testing for external launching

Hope the title isn't too vague.希望标题不要太含糊。 In the app I am testing, certain app-flow launches external apps (like Safari or Facebook for example).在我正在测试的应用程序中,某些应用程序流会启动外部应用程序(例如 Safari 或 Facebook)。 How can I verify that the app launched them with a UI test?我如何验证应用程序是否通过 UI 测试启动了它们? I can test for like an openURL with a unit test but is there an equivalent for UI?我可以通过单元测试来测试 openURL,但是是否有 UI 的等价物?

I'm not trying to actually continue after leaving the app, just to test and ensure the appropriate new app or URL was launched.我并没有在离开应用程序后尝试继续,只是为了测试并确保启动了适当的新应用程序或 URL。 The simulator/recorder can select UI elements from the launched application, but the test breaks at that point of the code.模拟器/记录器可以从启动的应用程序中获取 select 个 UI 元素,但测试在代码的那个点中断。 I also tried getting a handle to something on the menu bar (always present in the app, like a hamburger button) while it was there and then checking for it after launching the other app (to make sure it wasn't there).我还尝试在菜单栏上获取句柄(始终存在于应用程序中,例如汉堡包按钮),然后在启动其他应用程序后检查它(以确保它不存在)。 But that broke the test as well.但这也打破了测试。

Is there a work around?有解决办法吗? Or is this just something to be tested by a Unit Test?或者这只是单元测试要测试的东西?

As you mentioned, the UI framework can only test the given application. 如您所述,UI框架只能测试给定的应用程序。 So I would do an assert to make sure that the screen you were previously on (before opening safari or facebook, etc) is no longer present. 因此,我会做一个断言,以确保您之前所在的屏幕(在打开safari或facebook等之前)不再存在。 So for example: 例如:

XCTAssertFalse(app.tables.elementBoundByIndex(0).exists, "Found element, so app didn't open safari/facebook")

You're just asserting that the element isn't there, change app.tables.elementBoundByIndex(0).exists to be whatever element you're checking. 你只是断言元素存在,将app.tables.elementBoundByIndex(0).exists更改为你要检查的任何元素。

Xcode 13 and iOS 15 for safari : we could check if safari opens as below. Xcode 13 和 iOS 15 为 safari :我们可以检查 safari 是否打开,如下所示。

let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
let isSafariBrowserOpen = safari.wait(for: .runningForeground, timeout: 30)

Then do what you want:然后做你想做的:

if isSafariBrowserOpen {
    // Safari browser has opened, then additionally we could check if the url isn't nil
    safari.textFields["Address"].tap()
    let url = safari.textFields["Address"].value as? String
    XCTAssertNotNil(url)
}
else {
    // Safari browser hasn't opened
    // do something here, if necessary
}

Note: I didn't check with other external apps.注意:我没有检查其他外部应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM