-1

I need to assert the existence of a generated staticText on the screen. The staticText's label is the result of:

  1. An input text done by the user (variable!)
  2. Curly quotes that have been added at beginning and end by the app.

So if user inputs "abc" the staticText label will be “abc”. So I need to assert on a variable and not on a fixed string.

func test() throws {

//This part of the code I use to simulate the input.

let searchTerm = "abc"

app.typeText(searchTerm)

//This assert works but the input is variable so I need the assert to be based on a variable  
XCTAssertTrue(app.staticTexts\["“abc”"\].exists)

//So I build this string in order to search for this staticText

let staticTextLabel = "”\(searchTerm)”"

This assert fails indicating the staticText is not present

XCTAssertTrue(app.staticTexts\[staticTextLabel\].exists)

//This works normally if the label I am looking for does not contain curly quotes!

}

By breakpointing and executing this on the debug console I get:

po app.staticTexts --> StaticText, 0x7ff12af25b50, {...}, label: '“abc”'

po staticTextLabel --> "”abc”"

po app.staticTexts["“abc”"].exists --> True

po app.staticTexts[staticTextLabel].exists --> False

Can you assess on what I may be missing here? The assert works on other variable staticTexts that do not contain these curly quotes ”

enter image description here This is how the staticText is displayed in the app. In this case the search was not for abc but what is causing the issue is the closing curly quotes!

  • When in doubt, if you know the output to be correct, copy + paste what debug says the value to be into your test code. – Mike Collins Dec 08 '22 at 15:54
  • @MikeCollins can you clarify a bit more what additional info I can provide? – FY SerranoU Dec 08 '22 at 22:01
  • There had been an answer, but it now appears gone? Your values don’t match. There are two different types of quotes used. It’s hard to see with the naked eye, but your opening “extra quote” isn’t the same character value in your string versus the output. By copy and pasting from the debug output you can be sure to have the exactly right string. – Mike Collins Dec 09 '22 at 14:07
  • I've recreated the answer I saw yesterday, which was correct, with a bit more detail. – Mike Collins Dec 09 '22 at 14:25

1 Answers1

1

Your strings don't match.

staticTextLabel uses ”abc”, while app.staticTexts["“abc”"] uses “abc”.

If you put both of these into a diff tool you'll see the opening quote is not the same. The variable is a right double quote and the element is returning a left double quote. You can visually see the subtle difference by looking at the weights of the top and bottom of the character.

Mike Collins
  • 4,108
  • 1
  • 21
  • 28
  • 1
    That's what I said in my answer. But the OP told me I was wrong. So I deleted it. How can yours be right if mine is wrong? – matt Dec 09 '22 at 14:30
  • Ah, strange. I was curious where your answer had gone to. It is 100% correct unless they're providing the wrong data here. – Mike Collins Dec 09 '22 at 14:54
  • That is what the OP claimed; the OP said this was just a misprint in the question. So I deleted my answer, as I lost all belief in anything the OP says. – matt Dec 09 '22 at 15:33