locked
coded UI is not providing control value corretly RRS feed

  • Question

  •  

    Hello All, 

    I have noticed that when I run my coded UI test in few of the test cases Assertion is failing as Control on which I had added the assertion is not providing the same value every time. It's adding the trailing space few times and few times it breaks longer text to multiple lines but every time in the different way. For e.g. for the first test play the control value is:

     

    VImal test\nMy Test

     

    and in the next run it shows as

    Vimal\n testMy test

     

    Due to this my assertion fails as I am using Equals. My problem is I cannot use Contains Assertion. Could anyone let me know why control value is not consistent for every run and possible way to solve the issue. 

    Regards,

    Vimal


    vimal

    Monday, August 20, 2012 11:16 AM

Answers

  • Hi Vimal,

    While I don't know why the property in question is interpreted differently each time you run the test, I can suggest a couple of ways you can manipulate the value so you would be able to use the Assert method. 

    First off - leading and trailing spaces. They can easily be removed by using string.Trim(). 

    Just using this method your Assert method might look something like this:

    Assert.AreEqual(ExpectedStringValue, ActualStringValue.Trim());

    In order to get rid of the extra characters inside the string you might have to do a little more tweaking. If all that gets added to the property value is some form of a white space (\n, \t, etc.) you can do the following:

    char[] newActualValue = YourActualPropertyValue.ToCharArray();
    
    for (int i = 0; i < newActualValue.Length; i++)
                {
                    if (char.IsWhiteSpace(newActualValue [i]))
                    {
                        newActualValue [i] = ' ';
                    }
                }
    string finalActualValue = new string(newActualValue).Replace(" ", string.Empty);
    
    Assert.AreEqual( ExpectedValue.Replace(" ", string.Empty), finalActualValue.Trim());

    This is kind of redundant in the cases where the white space is actually a space but it helps get rid of all the new line and tab characters. As you can see, once you're done with that, you can use string.Replace() to get rid of all the spaces in both the actual and expected values. All that's left to do is compare them. 

    I know this is a lot of work but it might be worth it if this assertion is a show-stopper for your test.

    Hope this helps,

    Nat 

    Thursday, August 23, 2012 5:45 PM

All replies

  • Hi Vimal,

    Thank you for posting in the MSDN forum.

    If the property generally keeps changing, I’m afraid that it is not a good idea to assert on it. My suggestion is that we would change the value with the hand-code for this property. Or add assert to other property if we want to check it for a control.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, August 22, 2012 9:00 AM
    Moderator
  • Hi Jack,

    Thanks for your responce.

    It seems that my question is not clear. I applogies for the same.

    What i want to say that in some cases Coded UI is not provding the Property value consistant. The application which I am testing is providing the correct and same property value.

    FOr now i had removed the asseation but i want to understand why in some cases coded UI is adding trialing spaces and new line.

    Regards,

    Vimal


    vimal

    Thursday, August 23, 2012 2:06 PM
  • Hi Vimal,

    While I don't know why the property in question is interpreted differently each time you run the test, I can suggest a couple of ways you can manipulate the value so you would be able to use the Assert method. 

    First off - leading and trailing spaces. They can easily be removed by using string.Trim(). 

    Just using this method your Assert method might look something like this:

    Assert.AreEqual(ExpectedStringValue, ActualStringValue.Trim());

    In order to get rid of the extra characters inside the string you might have to do a little more tweaking. If all that gets added to the property value is some form of a white space (\n, \t, etc.) you can do the following:

    char[] newActualValue = YourActualPropertyValue.ToCharArray();
    
    for (int i = 0; i < newActualValue.Length; i++)
                {
                    if (char.IsWhiteSpace(newActualValue [i]))
                    {
                        newActualValue [i] = ' ';
                    }
                }
    string finalActualValue = new string(newActualValue).Replace(" ", string.Empty);
    
    Assert.AreEqual( ExpectedValue.Replace(" ", string.Empty), finalActualValue.Trim());

    This is kind of redundant in the cases where the white space is actually a space but it helps get rid of all the new line and tab characters. As you can see, once you're done with that, you can use string.Replace() to get rid of all the spaces in both the actual and expected values. All that's left to do is compare them. 

    I know this is a lot of work but it might be worth it if this assertion is a show-stopper for your test.

    Hope this helps,

    Nat 

    Thursday, August 23, 2012 5:45 PM