Can't get access to string localizations in UI Test (Xcode 7)

后端 未结 2 1305
情书的邮戳
情书的邮戳 2020-12-14 02:52

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 me

相关标签:
2条回答
  • 2020-12-14 03:45

    Here is my solution:

    1. In your UI Tests target -> Build Phases -> Copy Bundle Resources, add the needed localization files (e.g. Localizable.strings).

    2. 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 */
      
    3. Use the function like this: app.buttons[localizedString("localized.string.key")]

    0 讨论(0)
  • 2020-12-14 03:50

    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.

    0 讨论(0)
提交回复
热议问题