Robotium : Assert Text Containing Special Characters In Android Application While using solo.waitForText() and solo.searchText()

Robotium Provides some functions to assert texts that is reflected on the screen as a part of it or as a toast message. But the problem arises when Text contains some special characters in it. Assertion fails for verifying special characters in the Text.

robotium

For Example :

Suppose we want to verify Text : “Password(s) cannot be empty.” ,which renders as a toast message on the screen.
This text contains braces & dot as special characters .So if we want to assert this our Code may go like:

assertTrue(“Message not shown”,solo.searchText(“Password(s) cannot be empty.“));

But this assertion in this way fails and error message “Message not shown” may reflect  in the Log or in generated reports ,which indicates the the desired text “Password(s) cannot be empty.” is not reflected on the screen,while “Password(s) cannot be empty.” is actually reflected on the screen as a toast message.

Cause : The methods solo.searchText() and solo.waitForText() accept regex pattern.

Solution :

We have to use Pattern.quote(“Password(s) cannot be empty.”) for this purpose.

So the Code which will successfully assert Text containing special characters :

assertTrue(“Message not shown”,solo.searchText(Pattern.quote(“Password(s) cannot be empty.“)));