String comparison is not giving me right result.

Ranorex Spy, Recorder, and Studio.
ohm
Posts: 27
Joined: Thu Dec 03, 2009 7:27 pm

String comparison is not giving me right result.

Post by ohm » Thu Apr 15, 2010 6:45 pm

Here is the scenario.
The application I am testing has a 'Copy to clipboard' button which copies two strings, 'Work Instruction Order' and 'Summary'. When you paste it should look like following:

---------------------------------
Work Order Instructions

LineOne
LineTwo

Summary

LineThree
LineFour
----------------------------------

This is what I have in code:

Code: Select all

        			
string work_order_instructions_txt = "LineOne\r\nLineTwo";
string summary_txt = "LineThree\r\nLineFour"";   

//one string combining these two strings
string total_string = "Work Order Instructions"+"\r\n" + work_order_instructions_txt + "\r\n"+"Summary"+"\r\n" + summary_txt + "";
string new_total_string = total_string.Trim();
            
//capture clipboard
string clipboardText= System.Windows.Forms.Clipboard.GetText();
string new_clipboardText = clipboardText.Trim();

if (new_clipboardText.Equals(new_total_string))
        Report.Success("Copy to clipboard text has matched");
else
      {
        Report.Failure("Copy to clipboard text has not matched");
        Report.Error("Actual Text:\r\n" + new_clipboardText + "");
        Report.Error("Expected Text:\r\n" + new_total_string + "");
       }
This is what I am getting in report:

Copy to clipboard text has not matched
Actual Text:
Work Order Instructions
LineOne
LineTwo
Summary
LineThree
LineFour
Expected Text:
Work Order Instructions
LineOne
LineTwo
Summary
LineThree
LineFour

(Exact same actual and expect result, still test is failing).

Any idea why this is happening? Please help.
Thanks in advance.

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: String comparison is not giving me right result.

Post by Ciege » Thu Apr 15, 2010 7:51 pm

Only thing I can think of right off hand is that the Clipboard may not have the same \r\n characters that you intentionally put into the base string.

Maybe try doing a .Replace on both strings to remove any \r's and \n's and see if that compares then go from there.

It really seems like there is an un-printable character that is causing your issue since visually it looks right, but the compare is comparing both visible and invisible characters.

You could even try doing a .compare instead of a .equals and use the compare option IgnoreNonSpace so see if that helps.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

ohm
Posts: 27
Joined: Thu Dec 03, 2009 7:27 pm

Re: String comparison is not giving me right result.

Post by ohm » Fri Apr 16, 2010 4:35 pm

Wow. Great. Thanks. It's working.
I used .Replace method :

string new_total_string = total_string.Trim().Replace("\r\n", "");
string new_clipboardText = clipboardText.Trim().Replace("\r\n", "");

Thanks for your help Ciege.
You are the man :-)