在iOS单元和ui测试中忽略方案语言设置 [英] Scheme language setting ignored in iOS unit and ui tests

查看:100
本文介绍了在iOS单元和ui测试中忽略方案语言设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是发布

  xcodebuild测试 

从命令行中为不同的语言选择不同的方案.

当前,我有两种方案,它们之间的唯一区别是应用程序语言.一种方案是英语,另一种方案是西班牙语.如果我使用xCode来运行该应用程序,则该应用程序可以很好地运行,它会以我选择的方案中指定的语言启动,无论是EN还是ES都可以.

如果我从xCode运行测试,则语言设置将被忽略.无论选择哪种方案,都没关系,它始终显示为设备语言.在模拟器上也一样.使用以下命令运行测试时相同xcodebuild测试采摘方案.(向该方案中添加回显命令可确保选择正确的命令)

在方案编辑器中,选中使用Run操作的参数和环境变量".

我在做什么错了?

谢谢.

解决方案

是的,在XCTest测试中似乎忽略了方案中提供的所有环境变量和启动参数.

但是,您可以在测试中以编程方式设置语言,例如,在 setUp()方法中:

  override func setUp(){super.setUp()//在此处放置设置代码.在调用类中的每个测试方法之前,将调用此方法.让app = XCUIApplication()app.launchArguments + = ["-AppleLanguages",(en-US)"]app.launchArguments + = ["-AppleLocale","\" en-US \"]app.launch()} 

现在,您可以扩展此方法,并执行类似

2.在调用 launch()方法

之前,将测试中的启动参数传递给 XCUIApplication 实例.

  override func setUp(){super.setUp()//在此处放置设置代码.在调用类中的每个测试方法之前,将调用此方法.让app = XCUIApplication()app.launchArguments + = NSProcessInfo().argumentsapp.launch()} 

My final goal is to issue

xcodebuild test

from command line picking different schemes for different languages.

Currently I have two schemes, the only difference between them is the application language. In one scheme it is English, in the other is Spanish. If I use xCode to run the application it works nice, it is launched with the language specified in the scheme I have picked, both EN or ES are okay.

If I run the tests from xCode, language setting is ignored. Whichever scheme I pick, it doesn't matter, it is always displayed as the device language. The same on simulator. The same when running tests with xcodebuild test picking scheme. (Adding an echo command to the scheme ensures that the correct one is picked)

In the scheme editor "Use the Run action's arguments and environment variables" is checked.

What am I doing wrong?

Thank you.

解决方案

Yes, it seems like all environment variables and launch arguments provided in schemes are ignored in XCTest tests.

However, you can set the language programmatically in the test, for example in setUp() method:

override func setUp() {
    super.setUp()

    // Put setup code here. This method is called before the invocation of each test method in the class.

    let app = XCUIApplication()
    app.launchArguments += ["-AppleLanguages", "(en-US)"]
    app.launchArguments += ["-AppleLocale", "\"en-US\""]

    app.launch()

}

Now, you could extend this approach and do something like Snapshot does:

2 thing have to be passed on from snapshot to the xcodebuild command line tool:

  • The device type is passed via the destination parameter of the xcodebuild parameter

  • The language is passed via a temporary file which is written by snapshot before running the tests and read by the UI Tests when launching the application

In the end, In order to change the language on schema bases you can do the following:

1. Write a pre-action script for Test that creates a temp file:

mkdir -p ~/Library/Caches/xcode-helper
echo "en-US" > ~/Library/Caches/xcode-helper/language.txt

2. Load up the file in setUp() and set the app language:

override func setUp() {
    super.setUp()

    let app = XCUIApplication()

    let path = NSProcessInfo().environment["SIMULATOR_HOST_HOME"]! as NSString
    let filepath = path.stringByAppendingPathComponent("Library/Caches/xcode-helper/language.txt")


    let trimCharacterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
    let language = try! NSString(contentsOfFile: filepath, encoding: NSUTF8StringEncoding).stringByTrimmingCharactersInSet(trimCharacterSet) as String

    app.launchArguments += ["-AppleLanguages", "(\(language))"]
    app.launchArguments += ["-AppleLocale", "\"\(language)\""]

    app.launch()
}

From now on, the Xcode will run the test with the language/locale specified in the scheme's pre-action script.

UPDATE

Turns out, tests do not ignore the arguments provided in the scheme. The arguments are actually passed to the test itself but not to the tested app. Which might be unexpected but it makes sense.

That being said, all you need to do this:

1. Set -AppleLanguages (en-US) and -AppleLocale en_US launch arguments for the test in scheme

2. Pass the launch arguments in the test to the XCUIApplication instance before calling launch() method

override func setUp() {
    super.setUp()

    // Put setup code here. This method is called before the invocation of each test method in the class.

    let app = XCUIApplication()
    app.launchArguments += NSProcessInfo().arguments
    app.launch()
}

这篇关于在iOS单元和ui测试中忽略方案语言设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆