Home > OS >  Unable to access TextEditor from XCUIApplication in XCTest
Unable to access TextEditor from XCUIApplication in XCTest

Time:10-12

I am trying to write a UI test for our app that uses the new TextEditor view.

let app = XCUIApplication()
app.launch()

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

I cannot find anywhere what this accessor should be. Predictably, textFields doesn't work, and neither does textViews.

Is there a generic accessor I could use instead?

EDIT: The textViews accessor does in fact work.

CodePudding user response:

Actually you need to tap to make it focused before entering text, but it looks there is a bug in Xcode 13 Simulator (TextEditor is out of safe area by default), because all works only if TextEditor has a defined frame.

Here is a demo (prepared with 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 !!
    }
}

and now 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"))
    }

CodePudding user response:

You need to tap the textField into focus before attempting to type in it.

  • Related