Search code examples
coded-ui-tests

Can I force an Assert to refresh?


I have a wfp window that shows a list. The list has paging so if it is a long list I can click Next and look at the rows on page 2, 3, 4 etc.

I have an Assert that Asserts the date that is shown on each row. First I assert the date on the first row on the first page (which works OK), then I assert the date on the first row of page 2. The problems begin when I assert the first row of page 2, it actually asserts the first row on page 1. My pseudo code is...

//Assert date on page 1 row 1 is valid /*WORKS OK*/

//Click Next to move to page 2./*WORKS OK*/

//Assert date on page 2 row 1 is valid /*PROBLEM HERE*/

Out of curiosity, I changed my test, removing the first Assert and noticed that the assert of page 2, row 1 started working...

//Click Next to move to page 2. /*WORKS OK*/

//Assert date on page 2 row 1 is valid /*WORKS OK*/

So the code is correct and it is able to assert page 2, row 1 but it can only assert correctly once. On the second assert, it is holding on to the result from the first assert. Both Asserts have been created by recording them. When I record the second assert, it uses the same property that it used on the first assert. The property is read only so I can't find a way to flush it out.

I want both asserts to work properly, my basic question is how do I assert multiple times in the same list and get the up to date value each time? I think the answer will come when I work out how to record the second assert and either force the recorded assert to not use the same property that it used for the first assert OR alternatively there might be a way to flush out the old value before my second assert.

    public void Assert_Alerts_TimestampOfTopRowValid_Page1()
    {
        #region Variable Declarations
        WpfText uIItem04012017153411Text = this.UIOptimalMyClientShWindow.UIItemCustom1.UIItemCustom11.UIItem04012017153411Text;
        #endregion

        // Verify that the 'DisplayText' property is not null
        Assert.IsNotNull(uIItem04012017153411Text.DisplayText, "Can\'t assert the timestamp of first alert");
    }

    public void Assert_Alerts_TimestampOfTopRowValid_Page2()
    {
        #region Variable Declarations
        WpfText uIItem04012017153411Text = UIItem04012017153411Text;    
                                    \\I AM CONCERNED ABOUT WHY THIS IS RECORDED
                                    \\WITH THE SAME VARIABLE AS THE LAST ASSERT.
                                    \\HOW CAN I FLUSH THE VALUE OF THE VARIABLE
                                    \\AND MAKE THE CODED UI TEST ASSERT IT AGAIN
                                    \\OR RECORD WITH A DIFFERENT VARIABLE?
        #endregion

        // Verify that the 'DisplayText' property is not null
        Assert.IsNotNull(uIItem04012017153411Text.DisplayText, "Alert Timestamp on page 2 not valid.");
    }

My Property is ...

    private WpfText mUIItem04012017153411Text;

    public WpfText UIItem04012017153411Text
    {
        get
        {
            if ((this.mUIItem04012017153411Text == null))
            {
                this.mUIItem04012017153411Text = new WpfText(this);
                #region Search Criteria
                this.mUIItem04012017153411Text.SearchProperties[WpfText.PropertyNames.AutomationId] = "TxtBlockAutoID10";
                this.mUIItem04012017153411Text.WindowTitles.Add("MyApp");
                #endregion
            }
            return this.mUIItem04012017153411Text;
        }
    }

Solution

  • I think the problem is in the value of this.UIOptimalMyClientShWindow.UIItemCustom1.UIItemCustom11.UIItem04012017153411Text as assigned to WpfText uIItem04012017153411Text. This control refers to the date field on the first page. You can see that its old value will be used from the test of if ((this.mUIItem04012017153411Text == null)) and similar tests in the properties of the other names in this.UIOptimalMyClientShWindow.UIItemCustom1.UIItemCustom11.

    The solution is to call the Find() method of the control to force a re-evaluation of the searches. Your question is very similar to this one and the same answer applies.