Search code examples
iosunit-testingxctestuitabbaritembadge

Test tabbar badge via Xcode UI XCTests in iOS


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.


Solution

  • 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".