How to test button with image #19
-
In what area do you have a technical challenge?Xcode, Simulator & Previews DescriptionI cannot find a way to write a test for a button that has an image rather than a text label. Any suggestions? ReproductionN/A Expected behaviorN/A Additional contextNo response Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @ckunchur, if the button only has an image, you can use the name of the image to test it - see the example here: https://smashswift.com/how-to-test-buttons-in-ui-tests-in-swiftui/ |
Beta Was this translation helpful? Give feedback.
-
Hi @ckunchur, As @vishnuravi pointed out, the accessibility modifier is the right way to go, you are on a good path! Button(
action: {
// ... your action
},
label: {
Bundle.module.image(fromFileNamed: "Instagram")
.resizable()
.padding(.horizontal, 5)
.frame(width: 65, height: 40)
}
)
.accessibilityIdentifier("Instagram") You can then tap the button in your UI test using the following call: app.buttons["Instagram"].tap() |
Beta Was this translation helpful? Give feedback.
Hi @ckunchur,
As @vishnuravi pointed out, the accessibility modifier is the right way to go, you are on a good path!
Your code example that you have shared only as a syntax error and you have to attach the accessibility modifier to the complete button:
You can then tap the button in your UI test using the following call: