简体   繁体   English

如何在运行Xcode UI测试时禁用自动完成?

[英]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.

相关问题 Xcode自动完成功能为NSArray getter建议使用神秘的“ songsAtIndexes” - Xcode auto-complete suggests mysterious “songsAtIndexes” for NSArray getter 在 iOS 中禁用日语键盘的汉字自动完成 (henkan)? - Disable the kanji auto-complete (henkan) for Japanese keyboards in iOS? Xcode - 我更改了产品模块名称,现在自动完成将不再起作用(使用新模块名称时) - Xcode - I changed the Product Module Name, now auto-complete won't work anymore (when using the new module name) 确认UITextview自动完成 - Confirm UITextview auto-complete 如何在iOS 5.x上删除自动完成的词典数据? - How to delete auto-complete dictionary data on iOS 5.x? 如何在Firebase中为自动完成搜索构造数据 - How To Structure Data For Auto-Complete Search in Firebase 如何使用 Apple Map 套件实现地址自动完成 - How to implement auto-complete for address using Apple Map Kit 如何访问在XCUIApplication中设置的launchEnvironment和launchArguments,在XCode中运行UI测试? - How to access launchEnvironment and launchArguments set in XCUIApplication, running UI tests in XCode? 生成歌曲建议(自动完成) - Generate song suggestions (auto-complete) 在UItextfield中使用气泡来自动完成 - Use bubbles in UItextfield for auto-complete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM