【问题标题】:How to clear value in textfield using XCode UI tests如何使用 XCode UI 测试清除文本字段中的值
【发布时间】:2016-11-02 12:38:58
【问题描述】:
使用 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
清除文本字段中的值
【问题讨论】:
标签:
swift
xcode
swift3
ios-ui-automation
xcode-ui-testing
【解决方案1】:
这是我在 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)
}
【解决方案2】:
XCUIElement 的value 是只读属性,无法以编程方式更改它(只能手动使用 UI 交互)。试试
textFieldElement.buttons["Clear text"].tap()
【解决方案3】:
试试这个,
let app = XCUIApplication()
let userName = app.textFields["UserName - Login"] //Accessible label identifier
userName.tap()
userName.typeText("hello")