如何使用XCode UI测试清除文本字段中的值

时间:2016-11-02 12:38:59

标签: swift xcode swift3 ios-ui-automation xcode-ui-testing

使用XCode UI测试执行自动化。我有一个场景,我需要清除文本字段中的值并输入任何新文本。我可以访问该文本,但无法清除它。

app.alerts["abc"].collectionViews.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element(boundBy: 1).children(matching: .textField).element.value

代码的上方部分为我提供了文本字段中的值。有没有办法可以使用 Swift 3

清除文本字段中的值

3 个答案:

答案 0 :(得分:2)

这是我在UI测试中输入信息之前用于清除字段的代码。如果有一个Clear Text按钮则使用它;否则,它会输入一串退格,足以清除该字段的内容。

    if app.buttons["Clear text"].exists {
        app.buttons["Clear text"].tap()
    } else if let fieldValue = field.value as? String {
        // If there's any content in the field, delete it
        var deleteString: String = ""
        for _ in fieldValue.characters {
            deleteString += "\u{8}"
        }
            field.typeText(deleteString)
    }

答案 1 :(得分:0)

value的{​​{1}}是readonly属性,无法以编程方式更改它(仅手动使用UI交互)。尝试

XCUIElement

答案 2 :(得分:0)

试试这个,

let app = XCUIApplication()
let userName = app.textFields["UserName - Login"] //Accessible label identifier
userName.tap()
userName.typeText("hello")