【问题标题】:Unable to access TextEditor from XCUIApplication in XCTest无法从 XCTest 中的 XCUIApplication 访问 TextEditor
【发布时间】:2021-10-11 06:15:52
【问题描述】:

我正在尝试为使用新 TextEditor 视图的应用编写 UI 测试。

let app = XCUIApplication()
app.launch()

let textEditor = app.<accessor>["Editor"]
textEditor.typeText("Hello, world")

我在任何地方都找不到这个访问器应该是什么。可以预见,textFields 不起作用,textViews 也不起作用。

有没有我可以使用的通用访问器?

编辑:textViews 访问器确实有效。

【问题讨论】:

  • 您可以使用记录器并单击它以找到访问它的方法吗?此外,textFields 应该可以正常工作。

标签: swift swiftui xcuitest


【解决方案1】:

实际上你需要在输入文本之前点击使其聚焦,但它看起来在 Xcode 13 Simulator 中存在一个错误(TextEditor 默认情况下超出安全区域),因为所有工作 如果TextEditor 有一个定义的框架。

这是一个演示(使用 Xcode 13 / iOS 15 准备)

struct ContentView: View {

    @State private var text = "Test Text"

    var body: some View {
        TextEditor(text: $text)
            .accessibilityIdentifier("TextEditor")
            .frame(maxHeight: 400)                   // << limit !!
    }
}

现在是 UT:

    func testTextEditor() throws {
        let app = XCUIApplication()
        app.launch()

        let result = app.textViews["TextEditor"]
        XCTAssertTrue(result.exists)
        XCTAssertEqual(result.value as? String ?? "", "Test Text")

        result.tap()     // << here

        result.typeText("New value")
        XCTAssertTrue(String(result.value as? String ?? "").hasPrefix("New valueTest Text"))
    }

【讨论】:

    【解决方案2】:

    在尝试输入之前,您需要点按该字段或视图使其成为焦点。

    【讨论】:

      猜你喜欢
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2018-02-06
      • 2017-02-08
      • 1970-01-01
      相关资源
      最近更新 更多