I don't believe you can turn off auto-correction through code from your UI Testing target.
You can, however, turn it off for the individual text view from your production code. To make sure auto-correction is still on when running and shipping the app, one solution would be to subclass UITextField
and switch on an environment variable.
First set up your UI Test to set the launchEnvironment
property on XCUIApplication
.
class UITests: XCTestCase {
let app = XCUIApplication()
override func setUp() {
super.setUp()
continueAfterFailure = false
app.launchEnvironment = ["AutoCorrection": "Disabled"]
app.launch()
}
func testAutoCorrection() {
app.textFields.element.tap()
// type your text
}
}
Then subclass (and use) UITextField
to look for this value in the process's environment dictionary. If it's set, turn auto-correction off. If not, just call through to super.
class TestableTextField: UITextField {
override var autocorrectionType: UITextAutocorrectionType {
get {
if NSProcessInfo.processInfo().environment["AutoCorrection"] == "Disabled" {
return UITextAutocorrectionType.No
} else {
return super.autocorrectionType
}
}
set {
super.autocorrectionType = newValue
}
}
}