UI 测试删除文本字段中的文本 [英] UI Test deleting text in text field
问题描述
在我的测试中,我有一个文本字段,其中包含预先存在的文本.我想删除内容并输入一个新字符串.
In my test I have a text field with a pre-existing text. I want to delete the content and type a new string.
let textField = app.textFields
textField.tap()
// delete "Old value"
textField.typeText("New value")
使用硬件键盘删除字符串时,没有为我生成任何录音.用软件键盘做同样的事情后,我得到了:
When deleting string with hardware keyboard Recording generated for me nothing. After doing the same with software keyboard I got:
let key = app.keys["Usuń"] // Polish name for the key
key.tap()
key.tap()
... // x times
或
app.keys["Usuń"].pressForDuration(1.5)
我担心我的测试依赖于语言,所以我为我支持的语言创建了这样的东西:
I was worried that my test is language-dependent so I have created something like this for my supported languages:
extension XCUIElementQuery {
var deleteKey: XCUIElement {
get {
// Polish name for the key
if self["Usuń"].exists {
return self["Usuń"]
} else {
return self["Delete"]
}
}
}
}
代码看起来更好:
app.keys.deleteKey.pressForDuration(1.5)
但它非常脆弱.从模拟器退出后 Toggle software keyboard
被重置,我有一个失败的测试.我的解决方案不适用于 CI 测试.怎么解决才能更通用?
but it is very fragile. After quitting from Simulator Toggle software keyboard
was reset and I've got a failing test. My solution doesn't work well with CI testing. How can this be solved to be more universal?
推荐答案
我写了一个扩展方法来为我做这件事,而且速度非常快:
I wrote an extension method to do this for me and it's pretty fast:
extension XCUIElement {
/**
Removes any current text in the field before typing in the new value
- Parameter text: the text to enter into the field
*/
func clearAndEnterText(text: String) {
guard let stringValue = self.value as? String else {
XCTFail("Tried to clear and enter text into a non string value")
return
}
self.tap()
let deleteString = String(repeating: XCUIKeyboardKey.delete.rawValue, count: stringValue.count)
self.typeText(deleteString)
self.typeText(text)
}
}
这很容易使用:app.textFields["Email"].clearAndEnterText("newemail@domain.com")
这篇关于UI 测试删除文本字段中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!