簡體   English   中英

在Swift 3 UI測試中訪問自定義視圖組件中的元素

[英]Access elements in a custom view component in Swift 3 UI testing

我是任何類型的iOS編程的新手。 我正在嘗試為我的一個場景編寫UI測試用例。

以下是我使用recode方法並點擊自定義組件時得到的代碼。

let button = XCUIApplication().children(matching: .window).element(boundBy: 0).children(matching: .other).element.children(matching: .button).element

在此自定義組件中有兩個按鈕。 我想知道選擇了哪個按鈕。 為此我需要識別按鈕。 但是我得到了相同的代碼,我點擊自定義視圖。

如何訪問自定義視圖中的每個組件。 任何幫助都會很棒。

在應用程序代碼中向自定義視圖添加輔助功能標識符。

let customView: UIView!
customView.accessibilityIdentifier = "myCustomView"

然后訪問這樣的內容:

let app = XCUIApplication()
let customView = app.otherElements["myCustomView"]
let button1 = customView.buttons.element(boundBy: 0)
let button2 = customView.buttons.element(boundBy: 1)
XCTAssertTrue(button1.isSelected)
XCTAssertFalse(button2.isSelected)

請注意,為了使測試具有確定性,您應該已經知道應該選擇哪個按鈕。 這可確保您的測試每次運行時都測試相同的內容。

您需要使元素對輔助功能可見。

我建議你觀看關於Xcode中UI測試WWDC會議,尤其是這部分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM