2017-04-27 61 views
0

我是任何類型的iOS編程的新手。我正在爲我的一個場景編寫UI測試用例。在Swift 3 UI測試中訪問自定義視圖組件中的元素

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

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

在這個自定義組件中有兩個按鈕。我想知道哪個按鈕被選中。爲此,我需要確定按鈕。但是,當我點擊自定義視圖時,我會得到相同的代碼。

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

回答

2

添加無障礙標識在應用程序的代碼,您的自定義視圖。

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) 

注意,爲了使您的測試確定性,你應該已經知道了哪個按鈕(一個或多個)應選擇。這可以確保您的測試每次運行時都會測試相同的內容。

相關問題