有没有办法计算XCUIElement文本字段的字符?

时间:2016-06-17 21:52:35

标签: ios uitextfield xctest

在Swift中使用UITextfield类可以执行如下函数:

    private func signIn() {

    if emailTextField.text?.characters.count == 0 {
        invalidEmailLabel.text = NSLocalizedString("Required", comment: "Required text field")
        Constants.FormValidation.showValidationLabel(invalidEmailLabel)
        return
    }

有没有办法用XCUIElements做同样的事情或类似的事情?

我想做这样的事情:

func testExample() {

let app = XCUIApplication()
let emailTextField = app.textFields["username"]

if emailTextField.text.characters.count == "0"{
let emailTextField = app.textFields["username"]
    emailTextField.tap()
    emailTextField.typeText("example")

let passwordTextField = app.secureTextFields["password"]
    passwordTextField.tap()
    passwordTextField.typeText("whatever")
    app.buttons["Done"].tap()

}

if emailTextField.text.characters.count != "0"{

 let passwordTextField = app.secureTextFields["password"]
    passwordTextField.tap()
    passwordTextField.typeText("whatever")
    app.buttons["Done"].tap()
}

如何才能使其有效?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以通过执行以下操作来计算文本字段元素的字符:

let emailValue = emailTextField.value as! String
if emailValue.characters.count == 0 {
    // do something
}