【发布时间】:2021-05-20 11:07:23
【问题描述】:
我有这个 SwiftUI 视图(将删除不相关的部分)
struct LoginView: View {
var body: some View {
VStack {
Spacer()
InputField(title: "Email", text: $email)
InputField(title: "Password", text: $password, showingSecureField: true)
InputField(title: "Store", text: $userStore)
CallToActionButton(
title: newUser ? "Register User" : "Log In",
action: userAction
)
Button(action: { newUser.toggle() }) {
HStack {
CheckBox(title: "Create Account", isChecked: $newUser)
Spacer()
}
}
Spacer()
}
}
我可以使用以下代码为该按钮编写 UI 测试:
func testClickLoginButton() throws {
let app = XCUIApplication()
app.launch()
let startButton = app.buttons["Log In"]
XCTAssertTrue(startButton.waitForExistence(timeout: 1))
XCTAssertTrue(startButton.isEnabled)
startButton.tap()
}
而且它有效,因为屏幕上有一个带有该标题的按钮。
但是我想自动在这些 InputTexts 中放入一些文本并测试它们,但找不到它们,因为它们既不是 UITextField 也不是 UITextView。我怎样才能做到这一点?谢谢!
func testAddEmail() throws {
let app = XCUIApplication()
app.launch()
// fails as can't find any text field containing "Email"
let emailText = app.textFields["Email"]
XCTAssertTrue(emailText.waitForExistence(timeout: 1))
emailText.typeText("testemail@realm.com")
XCTAssertEqual(emailText.value as! String, "testemail@realm.com")
}
【问题讨论】:
标签: swift swiftui xcode-ui-testing