I like the smell of Swift in the morning…

When your are writing UITests for a multi language app you need access to your Localizable.strings. For example when you need to press a “Save” Button in your test. Because your app is localized the text “Save” is part of your Localizable.strings file.

When you want to tap that button in a UITest you need to know the title of the button:

1

XCUIApplication().buttons["Save"].tap()

You could use the quick and dirty approach by just using “Save”. But that will break your test when the button’s title is changed in your Localizable.strings file. And it feels dirty, too 😉

The right way to do this is to retrieve the button title from your Localizable.strings file. To make that work you have to do two things:

1. Add your Localizable.strings file to your UITest target

2. Access the file via the UITest bundle (I use a little helper method for that):

1

2

3

4

func localized(_key:String)->String{

let uiTestBundle=Bundle(for:AClassFromYourUITests.self)

returnNSLocalizedString(key,bundle:uiTestBundle,comment:"")

}

You can use any class from your UITests to access the UITest bundle. When you are not explicitly asking to use the UITest bundle when initializing the NSLocalizedString the default value Bundle.main will be used. Using Bundle.main does not work when running UITests because it gives you the bundle of the UITest Runner App instead of the UITest bundle.