How to do automated UI testing for system button on XCode7?
Asked Answered
G

3

3

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.

Gilbertogilbertson answered 5/10, 2015 at 11:24 Comment(0)
S
3

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.

Xcode - Set the Default Application 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.

Saker answered 5/10, 2015 at 12:7 Comment(2)
Thanks for the comments. I try to force the language to English based on your suggestion, but it does not work at all. Not sure whether it's a bug of Xcode.Gilbertogilbertson
I checked its accessibility label and identifier and all of them are empty, not sure why it's still translated. So I add a label for it, in my case I just add "More Info" for that button, now it can work on all other languages without any problem. I have opened a bug report to Apple for the option 1. Thanks Joe!Gilbertogilbertson
D
1

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.

Disorganization answered 26/11, 2015 at 19:41 Comment(0)
C
1

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

Compositor answered 19/9, 2017 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.