Adam
Adam

Reputation: 4780

UITest Query for UIImage inside of a UITableViewCell

I am writing a UI test in Swift against an iOS app that contains a UITableView with a custom UITableViewCell loaded from a nib. The cell contains a UIImageView and a UILabel.

When the table is rendered, there is no XCUIElementQuery that finds the image element. How can you get access to the ImageView during the UI Test to know the source of the image that was specified in the icon field?

class MyCell: UITableViewCell {
    @IBOutlet var name: UILabel!
    @IBOutlet var icon: UIImageView!
}

The image does not appear in app.images, app.tables.cells.images or app.tables.cells.otherElements. It seems like the image icons are nowhere to be found in the UI hierarchy.

Upvotes: 1

Views: 2852

Answers (2)

Matias Gualino
Matias Gualino

Reputation: 41

It's late, but for other users...

I solved it as follows:

  1. Add accessibilityIdentifier for table.

self.tableView.accessibilityIdentifier = "ExampleTableView"

  1. Set imageView isAccessibilityElement to true !!!

cell.icon.isAccessibilityElement = true

  1. Image name in variable and set accessibilityLabel's image with variable name.

let imageName = "example_image"

cell.icon.accessibilityIdentifier = "ExampleTableViewCellIcon"

cell.icon.accessibilityLabel = imageName

cell.icon.image = UIImage(named: imageName)!

  1. Test

XCTAssert(app.tables["ExampleTableView"].cells.element(boundBy: 0).images[ExampleTableViewCellIcon].label == "example_image")

Upvotes: 4

Joe Masilotti
Joe Masilotti

Reputation: 17008

The path of the image is not accessible in UI Testing. The framework sees the app as a user does, why would someone care about the filename of a check mark?

Update: To relate the user interface / accessibility to your different icons, you can set accessibility identifiers when setting the image.

MyCell().icon?.accessibilityIdentifier = "check"

Upvotes: 1

Related Questions