skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

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

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

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search