如何在 iOS XCTest 单元测试中测试从右到左的语言?

How to test right-to-left language in iOS XCTest unit tests?

有没有办法将 XCTest 单元测试切换为从右到左模式,以测试从屏幕右到左书写句子的阿拉伯语版本应用程序?我的应用程序代码逻辑根据语言方向表现不同。我想在单元测试中验证此功能。我需要做的是将应用程序从 XCTest 单元测试用例切换到从右到左的语言模式。

通过将方案的 应用程序语言 设置更改为 从右到左,可以 运行 从右到左模式的应用程序伪语言。有没有办法在单元测试中做类似的事情?

我的不完美解决方案

我最终将被测视图的 semanticContentAttribute 更改为 .ForceRightToLeft。它做了我需要做的。不过,这感觉并不是一种非常干净的方法。首先,它只适用于 iOS 9。其次,看起来我正在从单元测试的低级别修改我的应用程序视图。相反,如果可能的话,我更愿意将整个应用程序的语言切换为从右到左。

class MyTests: XCTestCase {
  func testRightToLeft() {
    if #available(iOS 9.0, *) {
      let view = UIView()
      view.semanticContentAttribute = .ForceRightToLeft
      // Test code involving the view
    }
  }
}

您要找的是Automated UI-Testing

此示例 JavaScript 代码更改设备方向,例如:

var target = UIATarget.localTarget();
var app = target.frontMostApp();
//set orientation to landscape left
target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);
UIALogger.logMessage("Current orientation now " + app.interfaceOrientation());
//reset orientation to portrait
target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);
UIALogger.logMessage("Current orientation now " + app.interfaceOrientation());

对于测试,如果您的布局已更改为 RTL 或 LTR,您可以尝试访问特定的 UI 元素并根据预期内容检查它们的内容。 所以在这里是另一个从官方文档检查 TableViewCell 内容的例子:

The crux of testing is being able to verify that each test has been performed and that it has either passed or failed. This code example runs the test testName to determine whether a valid element recipe element whose name starts with “Tarte” exists in the recipe table view. First, a local variable is used to specify the cell criteria:

var cell = UIATarget.localTarget().frontMostApp().mainWindow() \
.tableViews()[0].cells().firstWithPredicate("name beginswith 'Tarte'");

Next, the script uses the isValid method to test whether a valid element matching those criteria exists in the recipe table view.

if (cell.isValid()) {
  UIALogger.logPass(testName);
} else {
  UIALogger.logFail(testName);
}

If a valid cell is found, the code logs a pass message for the testName test; if not, it logs a failure message.

Notice that this test specifies firstWithPredicate and "name beginsWith 'Tarte'". These criteria yield a reference to the cell for “Tarte aux Fraises,” which works for the default data already in the Recipes sample app. If, however, a user adds a recipe for “Tarte aux Framboises,” this example may or may not give the desired results.

如果你想测试一个特定的方案:

Executing an Automation Instrument Script in Xcode After you have created your customized Automation template, you can execute your test script from Xcode by following these steps: Open your project in Xcode. From the Scheme pop-up menu (in the workspace window toolbar), select Edit Scheme for a scheme with which you would like to use your script. Select Profile from the left column of the scheme editing dialog. Choose your application from the Executable pop-up menu. Choose your customized Automation Instrument template from the Instrument pop-up menu. Click OK to approve your changes and dismiss the scheme editor dialog. Choose Product > Profile. Instruments launches and executes your test script.

您可以通过

检测书写方向
let writingDirection = UIApplication.sharedApplication().userInterfaceLayoutDirection
switch writingDirection {
case .LeftToRight:
    //
case .RightToLeft:
    //
default:
    break // what now? You are obviously using iOS 11's topToBottom direction…
}

要在启动时设置不同的语言和区域设置

除了传入环境标志或像您现在所做的那样设置 semanticContentAttribute 之外,现在没有简单的方法可以通过 testing/UI 测试来执行此操作。 Filing a bug强烈推荐苹果。

您还可以在方案中更改设备语言和地区。这意味着您需要针对您想要 运行:

的各种 LTR/RTL 测试使用单独的方案

Xcode甚至为超长字符串和 RTL 测试提供了伪语言。