Apostrophe in search string??

Best practices, code snippets for common functionality, examples, and guidelines.
larvaman
Posts: 2
Joined: Fri Jul 28, 2017 1:50 pm

Apostrophe in search string??

Post by larvaman » Fri Jul 28, 2017 2:02 pm

Here is my method:

public void FindByText(Element view, string textValue, bool report = true)
{
try
{
cuObject = view.FindSingle(".//text[@text='" + textValue + "']");
Mouse.MoveTo(cuObject);
if(report)
Report.Success("Validation", "Object with text value '" + textValue + "' was found within the view");
}
catch
{
if(report)
Report.Failure("Validation", "Could not find the object with text value: " + textValue + ".");
cuObject = null;
}
}

Here is my code calling the method:

var statusBar = repo.ConfigurationUtility.HomeScreen.ProjectList.StatusBar;
string statusbar = "Default Routes can't be deleted";
text.FindByText(statusBar, statusbar, true);



This works fine for string that don't contain the apostrophe. I have tried:

string statusbar = "Default Routes can't be deleted";
string statusbar = "Default Routes can\'t be deleted";
string statusbar = @"Default Routes can't be deleted";
string statusbar = "Default Routes can" + @"'" + "t be deleted";
string statusbar = "Default Routes can" + '\'' + "t be deleted";

no c# escape sequence will work but when it prints to my report as failing the string is correct with all the escape sequences.

Please help have spent too many hours on this.

User avatar
qwertzu
Posts: 284
Joined: Wed Jan 25, 2017 11:08 am

Re: Apostrophe in search string??

Post by qwertzu » Mon Jul 31, 2017 1:58 pm

Hello larvaman,

Please try using two apostrophes instead of one:
string statusbar = "Default Routes can''t be deleted";

regards,

qwertzu

ahoisl
Certified Professional
Certified Professional
Posts: 192
Joined: Fri Sep 07, 2007 8:16 am

Re: Apostrophe in search string??

Post by ahoisl » Mon Jul 31, 2017 8:27 pm

Apostrophe do not need escaping in C#, there are taken literally - otherwise your project would not even build.
If you get a failing validation, it is most probably not an apostrophe but another character, e.g. "´" or "`".

Could you please share the exact error message you get?

Regards,
Alex
Ranorex Team

User avatar
Stub
Posts: 515
Joined: Fri Jul 15, 2016 1:35 pm

Re: Apostrophe in search string??

Post by Stub » Tue Aug 01, 2017 8:34 am

I had problems putting apostrophe characters into variables for use via a repository item (@accessiblename=$SomeVariable), so I stopped doing that and used regular expressions instead. Or maybe I used a > comparison for starts-with. Either way I avoided apostrophes when searching for items in my AUT.

larvaman
Posts: 2
Joined: Fri Jul 28, 2017 1:50 pm

Re: Apostrophe in search string??

Post by larvaman » Wed Aug 02, 2017 1:43 pm

Thanks qwertzu,

The double apostrophes works for all three of my strings I am searching for.


Solution Found,

Larvaman