Search code examples
iosswiftxcodexcode-ui-testing

How to UI test a checkbox button


I have a checkbox button which is a custom button, I am changing button image to selected and unselected on button click.

I need to UI test this element, how do I do that?

I tried to get the element using the image name but it didn't help me


Solution

  • When the checkbox is selected, add the selected accessibility trait. Then, in your UI test, check the element's isSelected property.

    // App code
    imageView.accessibilityTraits = imageView.accessibilityTraits.union([.selected])
    
    // Test code
    XCTAssertTrue(checkboxElement.isSelected)
    

    Remember to add logic to remove the selected trait when the checkbox is unselected.