Home > Net >  XCTest fails to compare two strings
XCTest fails to compare two strings

Time:03-31

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.exists 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."

The only way to make it work is by comparing hardcoded text with the value of textView and the XCTest will success, like this:

XCTAssertEqual("This is a test.", typedText, "The texts are not matching")

CodePudding user response:

your function seems to work correctly, the "." are not the same between the two texts (check on https://www.diffchecker.com/diff )

CodePudding user response:

When I convert your last 3 bytes which is [239, 191, 188], With using

    let array: [UInt8] = [239, 191, 188]
    if let output = String(bytes: array, encoding: .utf8) {
        print("-"   output   "-")
    }

The output is :

--

That means indicates that the last character of one of these strings represents an extra space character

Maybe you are adding to unwanted space to text or in your XCUIElement value adds it . Thats why equalization operation returns false

  • Related