Reputation: 581
So I have a situation in which I have a few textFields that are validated. I'm trying to run a UI test, and when they fail they will get an alert to pop up with an error message (potentially a different message depending on what fields are invalid and in what way).
I'd like to test that not only that an alert appeared, but that the correct message is displayed. The problem I'm having is that I need to get the localized text for comparison (if I run tests in another language other than english), but when I call NSLocalizedString in the UITest it can't gather the correct localized string (just returns the key [default])
I've tried adding the localizeable.strings
files to the UITest target, but to no avail. Does anyone know if this is possible?
edit as a side note: I also tried setting an accessibility identifier on the UIAlertView
but when I query with that accessibility identifier it doesn't exist, I can only query it using the title of the alert which seems backwards.
Upvotes: 18
Views: 5160
Reputation: 13063
I have another approach written here: https://stackoverflow.com/a/75178860/7715250. Copy paste for when the question is deleted/closed:
Different approach without calling methods on your
String
Prerequisites
You are using
NSLocalizedString
.Step 1
Make sure you add the translations to your test targets (Go to your
Localizable
file and on the right side you can tap your UI test targets).Step 2
Add this somewhere in your main target
#if DEBUG // Can be changed by UI tests to get access to localized content var bundleForLocalizedTexts = Bundle.main #else let bundleForLocalizedTexts = Bundle.main #endif
Step 3
Add this value to the parameter
bundle
in all yourNSLocalizedString
s, like this:NSLocalizedString( "localized", bundle: bundleForLocalizedTexts, comment: "" )
Step 4
Override the method setUp in your
XCTestCase
subclass and add this line:
bundleForLocalizedTexts = Bundle(for: MySubclass.self)
Step 5
Everything should work! All languages should work, no extra methods to call.
Upvotes: 0
Reputation: 578
Here is my solution:
In your UI Tests target -> Build Phases -> Copy Bundle Resources, add the needed localization files (e.g. Localizable.strings).
Add a function similar to the following:
func localizedString(key:String) -> String {
/*1*/ let localizationBundle = NSBundle(path: NSBundle(forClass: /*2 UITestsClass*/.self).pathForResource(deviceLanguage, ofType: "lproj")!)
/*3*/ let result = NSLocalizedString(key, bundle:localizationBundle!, comment: "") //
return result
}
/*1 Gets correct bundle for the localization file, see here: http://stackoverflow.com/questions/33086266/cant-get-access-to-string-localizations-in-ui-test-xcode-7 */
/*2 Replace this with a class from your UI Tests*/
/*3 Gets the localized string from the bundle */
Use the function like this: app.buttons[localizedString("localized.string.key")]
Upvotes: 7
Reputation: 878
In the UI tests, the main bundle seems to be a random launcher app. That's why the .strings file doesn't appear: even though you added it to your test bundle, NSLocalizedString is checking the wrong bundle. To get around this, you need an invocation like this:
NSLocalizedString("LOCALIZATION_KEY", bundle: NSBundle(forClass: AClassInYourUITests.self), comment: "")
Which you might want to pull into a helper method.
Upvotes: 21