Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 323
Eugene Gordin Avatar asked Dec 17 '15 19:12

Eugene Gordin


People also ask

How do I test my iOS UI?

Recording a UI TestFrom the debug bar, click the Record UI Test button. Xcode will launch the app and run it. You can interact with the element on-screen and perform a sequence of interactions for any UI test. Whenever you interact with an element, Xcode writes the corresponding code for it into your method.

How do I run an XCUI test?

To run the XCUITests on XCode, you can click the highlighted icon below to see your newly created UI Test targets. You can hover on the “testExample()” test case and click the “Play” icon to run that specific test to see if everything was set up properly.

Is XCTest a unit testing framework?

XCTest framework is one of those frameworks that enables its users to write basic unit, performance and some level of UI tests for iOS apps.

What is XCTest in iOS?

Use the XCTest framework to write unit tests for your Xcode projects that integrate seamlessly with Xcode's testing workflow. Tests assert that certain conditions are satisfied during code execution, and record test failures (with optional messages) if those conditions aren't satisfied.


1 Answers

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)
like image 188
Joe Masilotti Avatar answered Oct 13 '22 01:10

Joe Masilotti