簡體   English   中英

如何訪問 XCUITest 導航欄上的右側項目按鈕?

[英]How to access a right item button on navigation bar in XCUITest?

我正在為我的 iOS Swift 應用程序編寫 UITest 案例。 在應用程序中,我以這種方式在導航欄上創建了一個自定義的右鍵項目按鈕:

let barButtonItem = UIBarButtonItem(customView: customView)
navigationItem.rightBarButtonItem = barButtonItem

現在我不知道如何在 XCUITest 中訪問這個自定義的右鍵項目按鈕,真的需要一些幫助。 提前致謝。

您無法訪問UIBarButtonItem ,因為它不是真正的 UIElement (它不是UIView子類),但無論如何您可能想訪問右欄按鈕項中的UIButton

有幾種方法可以訪問該按鈕,這里有兩個想法:

1.查詢導航欄中的第一個按鈕

let rightNavBarButton = XCUIApplication().navigationBars.children(matching: .button).firstMatch
XCTAssert(rightNavBarButton.exists)

這樣你就可以訪問UINavigationBar的第一個UIButton

這僅適用於導航欄中只有一個按鈕的情況。 所以當你添加另一個按鈕時它會中斷。

2. 使用可訪問性標識符

您可以為右側欄按鈕項內的按鈕定義可訪問性標識符,並在測試期間使用它來訪問它:

在您的應用中:

let barButtonItem = UIBarButtonItem(customView: customView)
barButtonItem.accessibilityIdentifier = "navbarRightItem"
navigationItem.rightBarButtonItem = barButtonItem

在您的測試中:

let rightNavBarButton = XCUIApplication().navigationBars.buttons["navbarRightItem"]
XCTAssert(rightNavBarButton.exists)

只要確保您使用的是accessibilityIdentifier而不是accessibilityLabel 因為accessibilityLabel將由VoiceOver 為殘障用戶讀取,並且應該包含有用的文本。

您必須為按鈕分配一個accessibilityIdentifier

barButtonItem.accessibilityIdentifier = “barButtonItemID”

如果不起作用,請將IsAccessibilityElement設置為 YES/true

暫無
暫無

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

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