Page 1 of 1

String comparison is not giving me right result.

Posted: Thu Apr 15, 2010 6:45 pm
by ohm
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.

Re: String comparison is not giving me right result.

Posted: Thu Apr 15, 2010 7:51 pm
by Ciege
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.

Re: String comparison is not giving me right result.

Posted: Fri Apr 16, 2010 4:35 pm
by ohm
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 :-)