Search code examples
swiftmacosnstextviewxctestcasexcuitest

XCTestCase - how to assert on a NSTextView containing String?


I have a macOS project that I'm creating UI tests for.

While it's relatively easy to find staticText, buttons, etc. by their text value. Using a subscript lookup on .textViews doesn't (seem to) work.

I've managed to get a reference to the NSTextView I want to inspect using .textViews.firstMatch but I can't figure out how to assert on it's string value.

I'm looking for something that works like this.

XCTAssertEqual(prefs.textViews.firstMatch.stringValue, "Enter text below")

Solution

  • Simply value should do.
    It's available on XCUIElementAttributes and is of type Any? that varies based on the type of the element.

    XCTAssertEqual(prefs.textViews.firstMatch.value as! String, 
                   "Enter text below")
    

    Ref: