Forum Discussion

littlebear's avatar
littlebear
Contributor
4 months ago

String Variable not including necessary newline characters (/n)

Hello everyone,

I have started using variables in my test checkpoints, and I have run into the following problem, the solving of which I would really appreciate.

I am testing to see whether the text on an element of one page is the same as the text of the element of another page with the following generalized process:

  1. Set a variable (DevValue) equal to the contentText of an object (obj.source).
  2. Compare DevValue to the contentText property of a different object (obj.match).

Pretty simple. However, even when the text is the same on both obj.source and obj.match, the test will still fail, because of the newline character. See the following checkpoint report:

obj.match.contentText (Actual value) vs. obj.source.contentText (Expected value)

There is an unexpected turn, however. The following process yields a successful checkpoint with matching values:

  1. Create a new checkpoint. In the checkpoint wizard, select obj.source (the expected value is set to obj.source.contentText).
  2. Change the checkpoint's object to obj.match (remember this does not change the expected value) and run the test to compare the values.

Again, this yields a success, and it is because now the obj.source.contentText contains the newline characters, in the form of '/n's

The DevValue variable, which should be the contentText of obj.source and the second checkpoint, which is the contentText of obj.source.

I can provide more pictures if necessary. This one has really stumped me.

Thanks a lot!

  • From my experience when \n exist in your text to move around as you are doing, you face restrictions as you experience.

    A suggestion, in your case would it be possible to use two variables?
    Read from page A element and save to variable A.
    Read from page B element and save to variable B.
    Compare variable A with Variable B.

    Hope that helps.

  • Hassan_Ballan's avatar
    Hassan_Ballan
    Frequent Contributor

    From my experience when \n exist in your text to move around as you are doing, you face restrictions as you experience.

    A suggestion, in your case would it be possible to use two variables?
    Read from page A element and save to variable A.
    Read from page B element and save to variable B.
    Compare variable A with Variable B.

    Hope that helps.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Use the following: \r for carriage return and \n for new line, within a string.

    You have to use backslash and not forward-slash, and also check for space characters too.

    • littlebear's avatar
      littlebear
      Contributor

      Thanks for your advice!

      Because the above solution is working I'm gonna refrain from adding code snippets to the Keyword test, but I'll keep that in mind in case I need it for something similar later. The new line/carriage return chars are not important for what we're testing.

  • Right now my problem is not in anything I am actually typing out. It is that the value stored into my String Variable 'DevValue' (the contentText of obj.source) is not equal to the value that is captured if I just create a checkpoint (for the contentText of obj.source) using the Operations tab.

    • littlebear's avatar
      littlebear
      Contributor

      The \n and other marks are added automatically when the object is selected to create a checkpoint, but they disappear when set them to a String Variable.

      • rraghvani's avatar
        rraghvani
        Champion Level 3

        TC is only doing what you instruct it, to do. You have to check and verify the contents are captured correctly, in order to perform the comparison. Also, ensure that your variable is not modified afterwards.

        Your approach is correct, and it's what I use all the time when scripting. 

        Here's a simple example,

        Keyword stepsOutput results