1

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

stackich
  • 3,607
  • 3
  • 17
  • 41
  • What happens if you compare Array(typedText.utf8) and Array(text.utf8) – Shadowrun Mar 31 '22 at 13:11
  • same, it fails @Shadowrun – stackich Mar 31 '22 at 13:20
  • What are the values of those - the difference between them will show what is different. Unicode has lots of ways of having strings that appear visually the same but aren't. – Shadowrun Mar 31 '22 at 13:22
  • You are trying to typecast a `Bool` (exists) to a String, typo? – Joakim Danielson Mar 31 '22 at 13:23
  • JoakimDanielson no, I have already pasted what XCTAssertEqual gives me - it says that the two same strings are not the same. @Shadowrun right, there is a difference at the end of the arrays: XCTAssertEqual failed: ("[84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 46]") is not equal to ("[84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 46, 239, 191, 188]") – stackich Mar 31 '22 at 13:27
  • 2
    So it's working fine, and it detected that one string has a rogue "U+FFFC" (239, 191, 188 in decimal utf-8) on the end which is the unicode character that is called "Object Replacement Character" – Shadowrun Mar 31 '22 at 13:57
  • @stackich Are you typing the text into the text view, or pasting it in from somewhere? – Itai Ferber Mar 31 '22 at 14:02
  • @ItaiFerber I am typing it using app.typeText() – stackich Mar 31 '22 at 14:09
  • `if let typedText = myTextView.exists as? String` – Joakim Danielson Mar 31 '22 at 14:15
  • it was a typo, sorry :) – stackich Mar 31 '22 at 14:22
  • 1
    @stackich And does your app insert anything else into the text view after/during typing? e.g. images, attachments, etc.? `U+FFFC` is the Unicode `OBJECT REPLACEMENT CHARACTER`, which is a placeholder that indicates a non-text object in a text stream. If your app does no such thing, then I'd consider trying to replicate this scenario in a standalone app, i.e., figure out whether it's somehow your typing that inserts the character in there; and if it doesn't happen in a separate app, it might help you narrow down what's unique to your app that's causing this. – Itai Ferber Mar 31 '22 at 14:36
  • Looks like it inserts a little "cursor space" at the end of textview when typing finishes. I need to trim it somehow. – stackich Mar 31 '22 at 14:49

3 Answers3

1

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

Omer Tekbiyik
  • 4,255
  • 1
  • 15
  • 27
0

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

Matthew Usdin
  • 1,264
  • 1
  • 19
  • 20
  • hmmm, how is this possible? – stackich Mar 31 '22 at 13:17
  • 2
    This isn't exactly correct — the period in both pieces of text is the same character; the first string has an additional invisible character _after_ the period that makes the strings not equal. – Itai Ferber Mar 31 '22 at 14:01
  • you are right @ItaiFerber. Looks like the code adds a cursor space at the end of the textview's text , which is invisible. – stackich Mar 31 '22 at 21:27
0

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")
}
stackich
  • 3,607
  • 3
  • 17
  • 41