[英]How to disable auto-complete when running Xcode UI Tests?
As part of my UI Tests, I'm generating a random string as titles for my objects. 作为我的UI测试的一部分,我正在生成一个随机字符串作为我的对象的标题。 The problem is that when this title is input via a keyboard (using XCUIElement.typeText()
), iOS sometimes accepts an auto-suggested value instead. 问题是当通过键盘输入此标题时(使用XCUIElement.typeText()
),iOS有时会接受自动建议的值。
For example, I may want it to type an auto generated string of "calg", but auto correct will choose "calf" instead. 例如,我可能希望它键入自动生成的“calg”字符串,但自动更正将改为选择“calf”。 When I try to look for this value later on with an assertion, it doesn't exist and fails incorrectly. 当我稍后尝试使用断言查找此值时,它不存在并且未正确地失败。
Is there a way to tell the UI tests that they shouldn't be using auto correct, or are there an workarounds I can use? 有没有办法告诉UI测试他们不应该使用自动更正,或者是否有我可以使用的变通方法?
I don't believe you can turn off auto-correction through code from your UI Testing target. 我不相信您可以通过UI测试目标中的代码关闭自动更正。
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. 为确保在运行和发布应用程序时自动更正仍然有效,一种解决方案是将UITextField
子类化并打开环境变量。
First set up your UI Test to set the launchEnvironment
property on XCUIApplication
. 首先设置你的UI测试设置launchEnvironment
物业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. 然后子类化(并使用) UITextField
在进程的环境字典中查找此值。 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
}
}
}
Unless you need auto-suggest for any test scenarios, did you try turning off auto-correction in device/simulator settings. 除非您需要针对任何测试场景进行自动建议,否则您是否尝试在设备/模拟器设置中关闭自动更正功能。
Settings-> General -> Keyboard -> Auto-Correction 设置 - >常规 - >键盘 - >自动更正
Here is how I disabled it in my UI Test 以下是我在UI测试中禁用它的方法
app.textFields.element(boundBy: 0).tap()
let keyboards = app.keyboards.count
XCTAssert(keyboards > 0, "You need enable the keyboard in the simulator.")
app.buttons["Next keyboard"].press(forDuration: 2.1)
let predictiveOn = app.switches["Predictive"].value as! String == "1"
if predictiveOn {
app.switches["Predictive"].tap()
} else {
app.buttons["Next keyboard"].tap()
}
app.buttons["Next keyboard"].press(forDuration: 2.1)
let predictiveOff = app.switches["Predictive"].value as! String == "0"
XCTAssert(predictiveOff, "Predictive mode is not disabled")
app.buttons["Next keyboard"].tap()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.