Search code examples
iosswiftuitextviewxctestxcuitest

XCTest fails to compare two strings


I am looking for a text given in parameter in my textviews. I am sure that it exists because I see it on the screen, but somehow comparing them fails.

Here is a method:

func checkText(_ text: String) {
        for tv in app.textViews.allElementsBoundByIndex {
            if let typedText = tv.value as? String {
                print(typedText)
                print(text)
                print(typedText == text)
            }
        }
    }

CONSOLE OUPUT:

This is a test.
This is a test.
false

I dont know how this is possible.
I have also tried this way:

if myTextView.exists {
    if let typedText = myTextView.value as? String {
        XCTAssertEqual(typedText, text, "The texts are not matching")
    }
}

But it gives an error saying that the texts are not matching, because "This is a test." is not equal to "This is a test."


Solution

  • As guys wrote in the comments, my textview was adding "Object Replacement Character" or "U+FFFC" after it finished editing.
    To make the strings equal, I needed to do this:

    let trimmedTypedText = typedText.replacingOccurrences(of: "\u{fffc}", with: "")
    

    So this will now succeed:

    if let typedText = myTextView.value as? String {
        let trimmedTypedText = typedText.replacingOccurrences(of: "\u{fffc}", with: "")
        XCTAssertEqual(trimmedTypedText, text, "The texts are not matching")
    }