Swift UI 测试文本字段中的访问字符串
Swift UI Testing Access string in the TextField
我正在使用 Xcode 和 XCTest
中集成的 UI Test Case
class 来测试应用程序 UI。我想测试这样的东西:
app = XCUIApplication()
let textField = app.textFields["Apple"]
textField.typeText("text_user_typed_in")
XCTAssertEqual(textField.text, "text_user_typed_in")
我试过textField.value as! String
方法;这是行不通的。
我也尝试过使用 expectationForPredicate()
的新异步方法,它会导致超时。
知道如何使用 UI 测试无法进行这种验证,我只能编写黑盒测试吗?
我使用这段代码,它工作正常:
textField.typeText("value")
XCTAssertEqual(textField.value as! String, "value")
如果您正在做类似的事情但它没有运行,我会检查以确保您的 textField 元素确实存在:
XCTAssertTrue(textField.exists, "Text field doesn't exist")
textField.typeText("value")
XCTAssertEqual(textField.value as! String, "value", "Text field value is not correct")
斯威夫特 4.2。
您需要清除文本字段中的现有值并粘贴新值。
let app = XCUIApplication()
let textField = app.textFields["yourTextFieldValue"]
textField.tap()
textField.clearText(andReplaceWith: "VALUE")
XCTAssertEqual(textField.value as! String, "VALUE", "Text field value is not correct")
其中 clearText
是 XCUIElement
扩展的方法:
extension XCUIElement {
func clearText(andReplaceWith newText:String? = nil) {
tap()
press(forDuration: 1.0)
var select = XCUIApplication().menuItems["Select All"]
if !select.exists {
select = XCUIApplication().menuItems["Select"]
}
//For empty fields there will be no "Select All", so we need to check
if select.waitForExistence(timeout: 0.5), select.exists {
select.tap()
typeText(String(XCUIKeyboardKey.delete.rawValue))
} else {
tap()
}
if let newVal = newText {
typeText(newVal)
}
}
}
以下适用于 Xcode 10.3 运行 on macOS 10.14.3,适用于 iOS app 运行 iOS 12.4:
XCTAssert( app.textFields["testTextField"].exists, "test text field doesn't exist" )
let tf = app.textFields["testTextField"]
tf.tap() // must give text field keyboard focus!
tf.typeText("Hello!")
XCTAssert( tf.exists, "tf exists" ) // text field still exists
XCTAssertEqual( tf.value as! String, "Hello!", "text field has proper value" )
我正在使用 Xcode 和 XCTest
中集成的 UI Test Case
class 来测试应用程序 UI。我想测试这样的东西:
app = XCUIApplication()
let textField = app.textFields["Apple"]
textField.typeText("text_user_typed_in")
XCTAssertEqual(textField.text, "text_user_typed_in")
我试过textField.value as! String
方法;这是行不通的。
我也尝试过使用 expectationForPredicate()
的新异步方法,它会导致超时。
知道如何使用 UI 测试无法进行这种验证,我只能编写黑盒测试吗?
我使用这段代码,它工作正常:
textField.typeText("value")
XCTAssertEqual(textField.value as! String, "value")
如果您正在做类似的事情但它没有运行,我会检查以确保您的 textField 元素确实存在:
XCTAssertTrue(textField.exists, "Text field doesn't exist")
textField.typeText("value")
XCTAssertEqual(textField.value as! String, "value", "Text field value is not correct")
斯威夫特 4.2。 您需要清除文本字段中的现有值并粘贴新值。
let app = XCUIApplication()
let textField = app.textFields["yourTextFieldValue"]
textField.tap()
textField.clearText(andReplaceWith: "VALUE")
XCTAssertEqual(textField.value as! String, "VALUE", "Text field value is not correct")
其中 clearText
是 XCUIElement
扩展的方法:
extension XCUIElement {
func clearText(andReplaceWith newText:String? = nil) {
tap()
press(forDuration: 1.0)
var select = XCUIApplication().menuItems["Select All"]
if !select.exists {
select = XCUIApplication().menuItems["Select"]
}
//For empty fields there will be no "Select All", so we need to check
if select.waitForExistence(timeout: 0.5), select.exists {
select.tap()
typeText(String(XCUIKeyboardKey.delete.rawValue))
} else {
tap()
}
if let newVal = newText {
typeText(newVal)
}
}
}
以下适用于 Xcode 10.3 运行 on macOS 10.14.3,适用于 iOS app 运行 iOS 12.4:
XCTAssert( app.textFields["testTextField"].exists, "test text field doesn't exist" )
let tf = app.textFields["testTextField"]
tf.tap() // must give text field keyboard focus!
tf.typeText("Hello!")
XCTAssert( tf.exists, "tf exists" ) // text field still exists
XCTAssertEqual( tf.value as! String, "Hello!", "text field has proper value" )