How to access localized string during automated UI test in Xcode 7?
Asked Answered
L

1

8

I have one more question based on my previous discussion thread of localized strings. Here is the link: How to do automated UI testing for system button on XCode7?

Now if I switch the iOS system language to another one, I can't get the localized string since it's in different bundle. I found another thread is talking about this: Can't get access to string localizations in UI Test (Xcode 7)

It works good if I copy all of localized strings from target app to UI tests. But for those strings in XIBs or storyboards they are using Object ID to represent a message, it's meaningless and not readable. How to make Object ID human-readable?

So I have to convert it first to know what it's originally before using NSLocalizedString:

NSLocalizedString("Help", bundle: NSBundle(forClass: AClassInYourUITests.self), comment: "")

Is there any easy way to resolve this issue within Xcode? If possible I wouldn't like to use these meaningless string in my code, it's difficult to know what it's. Or I have to write a script to handle this?

To be more precise, let's have an example. Some of my localized strings in storyboard of target app are:

"U2v-M1-HYu.text" = "ヘルプ";
"eTC-Zg-zHl.headerTitle" = "完了";

If I want to do the UI tests for different languages, I can't just copy it to the UI test bundle. I need to convert it first like this:

"Help" = "ヘルプ";
"Done" = "完了";

Any suggestions are welcome.

Lampedusa answered 4/12, 2015 at 7:51 Comment(0)
R
1

Step 1. Add your localizable.strings file to your UI Test target

Step 2. Create these helper methods in your Test class (found here):

var currentLanguage: (langCode: String, localeCode: String)? {
    let currentLocale = Locale(identifier: Locale.preferredLanguages.first!)
    guard let langCode = currentLocale.languageCode else {
        return nil
    }
    var localeCode = langCode
    if let scriptCode = currentLocale.scriptCode {
        localeCode = "\(langCode)-\(scriptCode)"
    } else if let regionCode = currentLocale.regionCode {
        localeCode = "\(langCode)-\(regionCode)"
    }
    return (langCode, localeCode)
}

func localizedString(_ key: String) -> String {
    let testBundle = Bundle(for: /*YourTestClass*/.self)
    if let currentLanguage = currentLanguage,
        let testBundlePath = testBundle.path(forResource: currentLanguage.localeCode, ofType: "lproj") ?? testBundle.path(forResource: currentLanguage.langCode, ofType: "lproj"),
        let localizedBundle = Bundle(path: testBundlePath)
    {
        return NSLocalizedString(key, bundle: localizedBundle, comment: "")
    }
    return "?"
}

Step 3. Change YourTestClass in step 2 code to your test class

Step 4. Get your localized string by using localizedString("String")

NOTE: This doesn't seem to work for all localization strings. I have found it doesn't work for storyboard strings for me, only the localizable.strings file.

Reinforce answered 5/9, 2017 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.