Testing iOS - How to escape the apostrophe character

I have an experiment that checks for the existence of a string of characters. The problem comes when the string contains an apostrophe ', my verify function can’t return true even though I’m sure it exists before

String text = “The transition contains the 'apostrophe character”
boolean verify = Mobile.waitForElementPresent(findTestObject(‘Object Repository/IOS/Public/Textview_Dynamic’, [(‘text’) : text]), 5)

if (verify) {
} else {

I tried adding a \ before that apostrophe
but it’s still not working. The strange thing is that this problem only appears on iOS

Someone please help me!

1 Like

Does anyone know the cause, Please !!!

Hi @thanh.tung.170861, thank you for sharing this.

We have tried to run those steps and it works in our projects, please see the video here.

We recommend you to use def verify = instead of boolean verify =
Please refer to this link for why should we use def https://www.baeldung.com/groovy-def-keyword.

Please let me know if this works for you.

Hi @thanh.tung.170861,

We checked the functions based on your example with “true” and “false” and these words do not contain apostrophe. Could you share with us the an actual example that you are testing now with apostrophe (e.g: aime’e). Also have you tried using double quote like “aime’e”.

Please let me know how it goes. Thank you.

Hi Vu,

Do you see my text string?

String text = “The transition contains the 'apostrophe character"

In that string there is an apostrophe, my code is checking the text string. As for true or false, I simply want to annotate when the verification function has results

Thank you.

1 Like

Hi @thanh.tung.170861,

Thank you for your information. Could you share with us the project so that we could reproduce the solution for it? It would take an expected longer time to create one ourselves that would be similar to yours. If my suggestion is possible, please share it to our email community@katalon.com.

Thank you.

Hi @vu.tran ,

My test is to check if the text snippets appear correctly on the app.
There are so many strings on the app, I can’t drag each string into Object Repository. So I have to use a dynamic object form (refer Parameterize Mobile Test Object Properties).
The problem only appears when validating the text containing the apostrophe (see the red arrow in the image). As for the text that does not contain apostrophes, the verification function works properly.

Question is: Why is only text containing apostrophe not found (I’m sure that text already appears in the app), and this problem only occurs with app installed on iOS?

Note: There are many types of apostrophes. Problems encountered with the type of apostrophe used to format text (see the red arrow in the image)

I found another solution for my testing, however this solution should only be used a few times. My application has a lot of text containing apostrophes, so this solution is very inconvenient(see the image). I don’t want use this solution!

Thank you!

Please show how your test object IOS/Public/txt_Dynamic is defined.
Please show how its locator is defined.
Take its screenshot like this:

Hi @kazurayam

Locate my objtect

You have a locator coded like this:

//*[@type='XCUIElementTypeStaticText' and @value = '${value}']

Then, your test script goes:

String input_Text = "Message d'accueil"
...  driver.findElementsByClassName('XCUIElementTypeStaticText', [('value')] : input_Text], 5)

The placeholder value in the locator will be interpolated with a value Message d'accueil. So the locator will be translated to be the following string:

//*[@type='XCUIElementTypeStaticText' and @value = 'Message d'accueil']

Now look at this carefully; it has this portion:

@value = 'Message d'accueil'

This is syntactically wrong according to XPath syntax rule. This locator falls to be invalid. This locator will not work.

1 Like

’ and " in a String variable requries you to do special treatment. It’s enevitable. You need to manage it.

If you escape the apos in the input_Text like this:

String input_Text = "Message d\\'accueil"

The locator will work.

Or just trim it to

String input_Text = "Message d"

will be simpler to do.

1 Like

Hi @kazurayam

I know what you explained, I also used \\ as you suggested. But the verify function still can’t find that text. Not only with the verification function, the Tap function also suffers.

Summary, katalon can’t find that text to do any action (like verify, tap, ect)

As I commented above, this problem is only present on iOS and not on Android. So why not with Android ?

Image for Android

No. I was wrong.

The XPath 1.0 spec does not support escaping apos character with \ at all.

For your interest, have a look at the following post for an explanation by Michael Kay, the author of W3C XPath Spec :

Please try changing your locator XPath expression as:

//*[@type='XCUIElementTypeStaticText' and @value = "${value}"]

Please find that double quotes, instead of single quotes, enclose the placeholder ${value}. This XPath expression works with value of

Message d'accueil

. I made a test case myself and assured this idea works.

Please note that this XPath expression (with "${value}") will be broken with a string value that contains a double quote character, like

Message d"accueil.

For this, you need XPath expression with '${value}'.

What if your string value contains both of single quote ' and double quote "? ---- well, it introduces too much complexity.

XPath 1.0 can not deal with quotation characters in a string literal very good. Therefore XPath 2.0 introduced EscapeApos to solve the difficulty. However XPath 2.0 is not used in the industry. Browsers uses XPath 1.0 now and will stay so in future.

In this case, you should abandon the idea of locating an HTML element by a single XPath expression that tries to match text content. You should try to find an alternative approach.

By the way, you have already got an alternative.

Your alternative does not use XPath. Therefore it is free from the difficulty of handling apostrophe character. It looks good to me.

Why not you package your solution as a Custom Keyword so that you can reuse it easily?

1 Like

Internally you got an XPath expression:

//*[@type='XCUIElementTypeStaticText' and @value = 'Message d'accueil']

which is syntactically invalid. It is broken due to an apostrophe character ('). Therefore the system should have detected an error and report a warning.

You wrote, as for iOS an error was detected; which is appropriate.

You wrote, as for Android no error was detected; which is inappropriate.

The software for Android might have some bug in failure handling for an invalid XPath expression.

But I think that you should not spend your time debugging the software for Android. You can change your XPath valid, or create your own custom keyword. Then you can make your test case to function.

1 Like

Hi @kazurayam

I tried your solution, however it still doesn’t work. What did I do wrong ?


=> I have put my solution in Custom Keyword, I don’t want to use it because every time test run it works very time consuming

I don’t know.

I guess, you have more problems other than apostrophe. You should find out what’s your problem.

I found the problem why I failed using your solution => Caused by my object.

You see the 2 objects below, they use the same xpath but have a few differences

Object 1: Not working

Object 2: Worked

I don’t remember how I made them, Do you know why Object 2 works with your solution and Object 1 doesn’t ?

Katalon Studio asks you to choose one out of 4 types of “Test Object” as follows:

Your Object 1 seems to be a Test Object for WebUI testing.

Object 2 seems to be a Mobile Object.

1 Like

My problem is solved
Thank you very much @vu.tran and @kazurayam


1 Like