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

人盡茶涼 提交于 2021-02-18 22:22:48

问题


Is there a way to switch an XCTest unit test into the right-to-left mode to test Arabic version of the app where sentences are written from right to left of the screen? My app code logic behaves differently based on language direction. I would like to verify this functionality in a unit test. What I need to do is to switch the app into the right-to-left language mode from an XCTest unit test case.

One can run the app in the right-to-left mode by changing the Scheme's Application language settings to Right-to-left Pseudolanguage. Is there a way to do similar thing in a unit test?

My imperfect solution

I ended up changing semanticContentAttribute of a view under test to .ForceRightToLeft. It does what I need to do. It does not feel like a very clean approach though. Firstly, it only works in iOS 9. Secondly, it looks like I am tinkering with my app views on a low level from the unit test. Instead, I would prefer to switch the whole app's language to right-to-left if it is possible.

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

回答1:


There's no easy way to do this right now with testing/UI testing besides passing in environment flags or setting the semanticContentAttribute as you are doing now. Filing a bug to Apple is highly recommended.




回答2:


You can also change the device language & region in the scheme. This means you'll need separate schemes for the various LTR/RTL tests you want to run:

Xcode even provides pseudo-languages for extra-long string & RTL testing.




回答3:


You can detect the writing direction via

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

To set different languages and locales on startup this might be a proper solution.




回答4:


What you are looking for is Automated UI-Testing

This example JavaScript code changes the device orientation for example:

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());

For testing, if your layout has changed to RTL or LTR you could try to access specific UI Elements and check their content against an expected content. So here is another example to check the contents of a TableViewCell from the official docs:

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.

If you want to test a specific scheme:

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.



来源:https://stackoverflow.com/questions/31769186/how-to-test-right-to-left-language-in-ios-xctest-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!