2

How to check that tabbar badge (tabBarItem.badgeValue) contains correct number in XCTests?

enter image description here

I can see target tabbar button in Accessibility Inspector, but badge element isn't inspectable.

dasdom
  • 13,975
  • 2
  • 47
  • 58
brigadir
  • 6,874
  • 6
  • 46
  • 81

1 Answers1

7

You can set the accessibilityIdentifier value to the tab bar item.

tabBarItem.accessibilityIdentifier = "Your_Identifier"

On your UITest case,

guard let value = app.buttons["Your_Identifier"].value as? String 
else {
    XCTFail("badge value not updated")
    return
}
XCTAssert(value == "1 item", "badge value not updated")

The value will be in the format "number_on_badge item(s)". Thus in your case, you should be testing against "19 items".

John Kuan
  • 116
  • 2
  • 11