Eugene Gordin
Eugene Gordin

Reputation: 4107

How to test UIImageView elements in iOS XCTest?

I'm trying to test my UIs using XCTest UI testing, and having troubles of testing UIImageViewsI have in my app (hit tests, presence etc).

In the list of the XCUIElementType there is no such type, and when I look at the children of the superview my UIImageViews are not listed there for some reason eventhough I can see them on screen and in the UI inspector in the Xcode.

Has anyone had this kind of problem?

Upvotes: 9

Views: 9726

Answers (1)

Joe Masilotti
Joe Masilotti

Reputation: 17008

Assert the presence of an image by its accessibility label.

Production Code

let image = UIImage(named: "profile")
let imageView = UIImageView(image: image)
imageView.accessibilityIdentifier = "Your profile image"

UI Test Code

let app = XCUIApplication()
XCTAssert(app.images["Your profile image"].exists)

Upvotes: 18

Related Questions