My Storyboard has only one UI, and it has navigation bar with one UIBarButtonItem with System Item: Add. It also has another info UIButton.
When doing UI testing in English everything works good without any problem. But if you switch the language to another one, it is always failed. Here is the testing code snippet:
app.navigationBars["My Product"].buttons["Add"].tap()
app.buttons["More Info"].tap()
According to the error log, Xcode find it in another language. Here it is:
Assertion Failure: UI Testing Failure - No matches found for "More Info" Button
Query input was
Button 0x7fa4ca65a1d0: traits: 8724152321, {{8.0, 31.5}, {21.0, 21.0}}, label: '戻る',
Button 0x7fa4ca657700: traits: 8589934593, {{330.5, 26.0}, {40.0, 30.0}}, label: '追加',
Button 0x7fa4ca4658d0: traits: 8589934593, {{312.0, 605.0}, {22.0, 22.0}}, label: '詳細情報'
However, I haven't created any localized string for it since it's just an image created by iOS system. Does anyone know how to fix it?
Thanks so much.
Option 1: Set a Default Language
Create a new scheme for UI Testing and set the default Application Language. This will lock the app into one localized file so you can write all of your tests for that language.
Set the option from Product -> Scheme -> Manage Schemes or ⌘⇧,. Then select the Options tab and set the language.
Pros: Simple, one-time change.
Cons: Cannot be used to create localized screenshots with snapshot (a tool that runs your app via UI Testing and generates App Store screenshots along the way).
Option 2: Use -accessibilityIdentifier
for Localized Strings
Instead of accessing items via their displayed text or value, use accessibilityIdentifier
. This is read by the UI Testing framework but never shown or read to users (even with accessibility turned on). In the old UIAutomation docs Apple mentions using this for developer functionality, which this seams like a good use case.
You can then continue to set accessibilityLabel
and accessibilityValue
like normal, with the localized versions.
Pros: Can be used for more generic solutions, such as taking automated screenshots.
Cons: Might require more work changing each label you need "unlocalized" for testing.
Have you considered using
[[app.buttons elementBoundByIndex: 0] tap]; //0-2
instead of trying to identify a button by its label? This is a less refined solution to your problem, but it may get the job done.
With swift three you can use BarButtons with below code
app.navigationBars.buttons.element(boundBy: 0).tap()
you can change index as per your need