1

When I tap a UIButton, the image should change to reflect its new state (e.g. Record -> Pause etc).

In my XCode UITest function, how do I interrogate the buttons current image after the tap to assert that its image has changed correctly to the correct image's .png file?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Robert
  • 231
  • 3
  • 10
  • Related - it doesn't seem to be possible directly: https://stackoverflow.com/a/31251379/3151675 – Tamás Sengel Jul 28 '18 at 22:45
  • This isn't possible in XCTest UI tests. You need to unit test the assigned image as the UI tests are functional, not visual. – Oletha Jul 29 '18 at 19:51

2 Answers2

0

I did it like this

// Find the button by the image name
// In this example the image's names are "record_image" and "pause_image"
// "_" are replaced by " " in the image name when searching
let recordButton = XCUIApplication().buttons["record image"]
recordButton.tap()
XCTAssertFalse(recordButton.exists) // Record button won't exist anymore since the image has changed
let pauseButton = XCUIApplication().buttons["pause image"]
XCTAssertTrue(pauseButton.exists)
pauseButton.tap()
XCTAssertFalse(pauseButton.exists)
XCTAssertTrue(recordButton.exists)
MateusK
  • 110
  • 3
0

I did like this, not best way but efficient for me,

every image change I changed accessibility identifier then checked the access.ids

public func setFollowed(_ isFollowed: Bool) {
    if isFollowed {
        followButton.setImage(UIImage(named: "followed-green-icon"), for: .normal)
        followButton.accessibilityIdentifier = "ProfileInfoView_followButton_followed"
    }
    else {
        followButton.setImage(UIImage(named: "follow-blue-icon"), for: .normal)
        followButton.accessibilityIdentifier = "ProfileInfoView_followButton_follow"
    }
}

sample UI test part:

func getFollowButton(isFollowed: Bool) -> XCUIElement {
    if isFollowed == true {
        return app.descendants(matching: .button)["ProfileInfoView_followButton_followed"]
    } else {
        return app.descendants(matching: .button)["ProfileInfoView_followButton_follow"]
    }
}

then tested returned element, state changed etc.

babacan
  • 9
  • 5