問(wèn)題描述
我正在考慮使用 XCUITest 對(duì)我的 iOS 應(yīng)用程序進(jìn)行 UI 測(cè)試.看起來(lái) XCUITest 具有廣泛的功能,包括使用多個(gè)應(yīng)用程序的能力.但是,多應(yīng)用支持似乎有些受限.
I am looking at using XCUITest for UI tests for my iOS apps. It looks like XCUITest has a wide arrange of functionality, including the ability to work with multiple apps. However, the multiple app support seems somewhat limited.
似乎使用 XCUIApplication 我可以使用 Bundle ID 啟動(dòng)另一個(gè)應(yīng)用程序,甚至可以監(jiān)控它的狀態(tài).但是,我想做的是能夠?yàn)閮蓚€(gè)具有緊密交互的應(yīng)用程序運(yùn)行協(xié)調(diào)測(cè)試(例如,一個(gè)應(yīng)用程序?qū)α硪粋€(gè)應(yīng)用程序執(zhí)行打開(kāi) URL,執(zhí)行一些 UI 操作,然后返回到第一個(gè)應(yīng)用程序).
It seems that using XCUIApplication I can start another app using Bundle ID and even monitor its state. However, what I want to do is be able to run a coordinated test for two apps that have tight interaction (for example, one app does an open URL to another app, performs some UI action, and then returns to the first app).
這可能只使用 XCUITest,還是我需要一些更高級(jí)別的工具?(我認(rèn)為有一些工具可以做到這一點(diǎn),但如果可能的話,我更愿意繼續(xù)使用 XCUITest)
Is this possible just using XCUITest, or do I need some higher level tool? (I think some tools exist that can do this but would prefer to stay with XCUITest if possible)
理想情況下,我會(huì)將所有代碼放在一個(gè)文件中,以便在兩個(gè)應(yīng)用程序中執(zhí)行 UI 操作.但如果這是不可能的,我愿意編寫單獨(dú)的測(cè)試應(yīng)用程序,這些應(yīng)用程序有效地相互移交,在輪到"時(shí)執(zhí)行操作.但我不確定如何協(xié)調(diào)這兩個(gè)測(cè)試應(yīng)用程序.
Ideally, I would have all the code in a single file that executed the UI actions in the two apps. But if that was impossible, I would be open to writing to separate test apps which effectively hand off to one other, performing actions when it is their 'turn'. But I am not sure how I would work the coordination of the two tests apps.
推薦答案
這實(shí)際上在 Xcode 9 中得到了很好的支持.下面是如何設(shè)置它:
This is actually supported really nicely in Xcode 9. Here's how to set it up:
我發(fā)現(xiàn)最簡(jiǎn)單的方法是創(chuàng)建一個(gè)包含您的兩個(gè)應(yīng)用程序的工作區(qū).在包含您的 UI 測(cè)試代碼的應(yīng)用程序項(xiàng)目中,以執(zhí)行這兩個(gè)應(yīng)用程序,確保這兩個(gè)應(yīng)用程序都列在您的 UI 測(cè)試目標(biāo)的目標(biāo)依賴項(xiàng)下:
I've found the easiest is to create a workspace containing both of your apps. In the app project containing your UI test code to excercise both apps, make sure both apps are listed under Target Dependencies in your UI Test target:
現(xiàn)在只需在測(cè)試的設(shè)置函數(shù)中啟動(dòng)這兩個(gè)應(yīng)用程序,使用它們的包標(biāo)識(shí)符:
Now it's just a matter of starting both apps in your setup function of your test, using their bundle identifiers:
app1 = XCUIApplication(bundleIdentifier: "no.terje.app1.App1")
app1.launch()
app2 = XCUIApplication(bundleIdentifier: "no.terje.app2.App2")
app2.launch()
(應(yīng)用程序是我的測(cè)試類的實(shí)例成員,var app1: XCUIApplication!
)
(the apps are instance members of my test class, var app1: XCUIApplication!
)
現(xiàn)在一切就緒.像這樣對(duì)兩個(gè)應(yīng)用程序運(yùn)行測(cè)試:
Now you're all set up. Run tests against both apps like this:
func testButtonsExist() {
app1.activate()
app1.buttons["mybutton"].exists
app2.activate()
app2.buttons["myotherbutton"].exists
}
至于您提到的等待或異步挑戰(zhàn):我想其中大多數(shù)也存在于單個(gè)應(yīng)用程序中的異步代碼中,例如等待打開(kāi) URL.但是,如果您遇到特定問(wèn)題,請(qǐng)發(fā)布您正在嘗試和失敗的特定事情.
As for the waiting or async challenges you are mentioning: I would imagine most of those exist for async code within a single app too, for example waiting for a URL to open. But do post a specific thing you are trying and failing if you have specific problems.
這篇關(guān)于XCUITest:以協(xié)調(diào)的方式跨兩個(gè)應(yīng)用程序運(yùn)行測(cè)試的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!