简体   繁体   English

iOS UITests NSLocalizedString 不工作

[英]iOS UITests NSLocalizedString Not Working

I'm trying to localize my UITests to work with other languages (currently using Snapshot to automate screenshots so that's why I need this to work).我正在尝试本地化我的 UITests 以使用其他语言(目前使用 Snapshot 来自动化屏幕截图,这就是我需要它工作的原因)。

My main problem right now is with a Done button in IQKeyboardManager .我现在的主要问题是IQKeyboardManager 中有一个 Done 按钮。

In English I have the following code and it works fine:在英语中,我有以下代码,它工作正常:

app.toolbars.buttons["Done"].tap()

to tap the done button after entering text.输入文本后点击完成按钮。

In Spanish that button is called "OK".在西班牙语中,该按钮称为“确定”。 Looks like it's getting that from some default UIKit localized string or something like that.看起来它是从一些默认的 UIKit 本地化字符串或类似的东西中获取的。

I tried adding a .strings file in my UITest es.lproj folder and put "UIBarButtonSystemItem.Done" = "OK";我尝试在我的 UITest es.lproj文件夹中添加一个 .strings 文件并放置"UIBarButtonSystemItem.Done" = "OK"; in it.在其中。

I also changed it to be:我也改成了:

app.toolbars.buttons[NSLocalizedString("UIBarButtonSystemItem.Done", bundle: Bundle.main, value: "Done", comment: "")].tap()

and that didn't work.那没有用。 Always used "Done".一直使用“完成”。

It always gives the error:它总是给出错误:

No matches found for "Done" Button.没有找到“完成”按钮的匹配项。

I also tried:我也试过:

app.toolbars.buttons[NSLocalizedString("UIBarButtonSystemItem.Done", comment: "")].tap()

and that resulted in an error:这导致了一个错误:

No matches found for "UIBarButtonSystemItem.Done" Button.未找到与“UIBarButtonSystemItem.Done”按钮的匹配项。

So it looks like my .strings file isn't working for my UITests.所以看起来我的 .strings 文件不适用于我的 UITests。 Any ideas of how to get it working?关于如何让它工作的任何想法?

I created this small project that supports English and Spanish.我创建了这个支持英语和西班牙语的小项目。 The tests can also be run using the two different languages by switching it in the scheme's configuration还可以通过在方案的配置中切换来使用两种不同的语言运行测试

在此处输入图片说明

This is how the test is build这就是测试的构建方式

func testExample() {
    let greeting = localizedString(key: "Greetings.Hello")
    XCTAssert(XCUIApplication().staticTexts[greeting].exists, "Expected \"\(greeting)\" label to exist")
}

It uses the following function to get the translations它使用以下函数来获取翻译

func localizedString(key:String) -> String {
    let bundle = Bundle(for: LocalizationUITests.self)
    let deviceLanguage = Locale.preferredLanguages[0]
    let localizationBundle = Bundle(path: bundle.path(forResource: deviceLanguage, ofType: "lproj")!)
    let result = NSLocalizedString(key, bundle:localizationBundle!, comment: "") //
    return result
}

Here's the project that where you can see it work: https://github.com/TitouanVanBelle/LocalizationUITests这是您可以看到它工作的项目: https : //github.com/TitouanVanBelle/LocalizationUITests

Just for the reference and easy lookup:仅供参考和方便查找:

It is indeed because you cannot access your main bundle from your UITests target.这确实是因为您无法从 UITests 目标访问主包。

let testBundle = Bundle(for: type(of: self ))
let lookup = NSLocalizedString("UIBarButtonSystemItem.Done", bundle: testBundle, value: "Done", comment: "")
app.toolbars.buttons[lookup].tap()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM