Xcode 7 UITests with localized UI

后端 未结 11 931
既然无缘
既然无缘 2020-12-07 17:38

In my App I\'m using NSLocalizedString to localize my app. Now I want to switch to UITests and habe Testcode like this:

[tabBarsQue         


        
11条回答
  •  失恋的感觉
    2020-12-07 17:50

    YOU CAN RE-USE YOUR PROJECT LOCALIZATION BUNDLES!

    When you test message boxes behaviour you need to know exactly what message box just appeared. You need to copy your localization from another scheme during build phase.

    In your UI Tests target -> Build Phases -> Copy Bundle Resources, add the localization files needed (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 */
    

    Then in your code you can use app.buttons[localizedString("localized.string.key")]

    Full article is here: https://github.com/fastlane-old/snapshot/issues/321#issuecomment-159660882

提交回复
热议问题