Xcode 7 UITests with localized UI

后端 未结 11 910
既然无缘
既然无缘 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:53

    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.

    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.

    0 讨论(0)
  • 2020-12-07 17:57

    If you're doing this for the purpose of running Snapshot (rather than actual UI testing), then I find the simplest solution is to cheat and use HSTestingBackchannel

    It is a tool that I wrote which allows you to send notifications from the UITesting class to the app. You then write code in the app which responds directly to the notifications.

    0 讨论(0)
  • 2020-12-07 18:01

    In addition to Joe's answer you can also force language for UI tests directly in test code without editing a scheme like this:

    - (void)setUp
    {
        [super setUp];
    
        self.continueAfterFailure = NO;
        XCUIApplication *app = [[XCUIApplication alloc] init];
        app.launchArguments = @[@"-AppleLanguages", @"(en)", @"-AppleLocale", @"en_EN"];
        [app launch];
    }
    
    0 讨论(0)
  • 2020-12-07 18:01

    I've made localized screenshots with Fastlane using next scheme:

    1) Add to Fastfile next param:

    localize_simulator: true

    2) Use this code to get localized string:

    class LocalizationHelper: NSObject {
    
        class func localizedString(_ key: String) -> String {
            let testBundle = Bundle(for: UITests.self)
            let currentLocale = Locale.current
            if let code = currentLocale.languageCode,
                let testBundlePath = testBundle.path(forResource: code, ofType: "lproj") ?? testBundle.path(forResource: code, ofType: "lproj"), let localizedBundle = Bundle(path: testBundlePath) {
                return NSLocalizedString(key, bundle: localizedBundle, comment: "")
            }
            return ""
        }
    
    }
    
    0 讨论(0)
  • 2020-12-07 18:03

    The answer of Volodymyr helped me a lot, but it can fail if the localization bundle folder name differs from the deviceLanguage set in Snapshot. This snippet works fine for me in Swift 3.0 and with languages like italian (where current locale is "it" but device language is "it-IT").

        func localizedString(key:String) -> String {
          let languageBundlePath = Bundle(for: PlinthUITests.self).path(forResource: deviceLanguage, ofType: "lproj") ?? Bundle(for: PlinthUITests.self).path(forResource: NSLocale.current.languageCode!, ofType: "lproj")
          let localizationBundle = Bundle(path: languageBundlePath!)
          let result = NSLocalizedString(key, bundle:localizationBundle!, comment: "")
        return result
    }
    
    0 讨论(0)
提交回复
热议问题