Forum Discussion

la2texas's avatar
la2texas
Occasional Contributor
8 years ago

Wait on a UI refresh

We have a test that is expecting a DevExpress grid to contain one item, but it fails intermittently. 

 

It's all dependent on how fast the Web API responds with the list of items.

 

How can I instruct TestLeft to "wait a few seconds". Please note that the Window.TryFind() with a wait does not work, because the window is nearly immediately available, but the API call has not come back yet. We just need a few seconds of hold time to ensure the Web API call has returned before evaluating.

2 Replies

  • IgorM's avatar
    IgorM
    New Contributor

    In the simplest case the following function might be used:

     

    public static bool Wait(Func<bool> condition, int timeout)

    {

        var end = DateTimeOffset.UtcNow.Add(TimeSpan.FromMilliseconds(timeout));

        while (DateTimeOffset.UtcNow < end)

        {

            if (condition()) return true;

            Thread.Sleep(200);

        }

        return false;

    }

     

    Timeout variable defines here only the maximum timeout, if the condition is true, the execution will immediately continue (well, ok, it might take up to 200 ms :smileyhappy:).

     

    The call of the function might be something like:

     

    if (!WaitHelper.Wait(() => UIMap.OpenButton.Enabled && UIMap.MachineComboBox.Enabled)

    {

        Assert.Fail("After import the nesting properties are not loaded sucessfully");

    }

     

    Hope this helps.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Regular TestComplete/TestExecute (TestLeft is based on their engine) has aqUtils.Delay(<timeout in ms>) method. You may use Object Spy in TestLeft, search for this method and call it. (Sorry, cannot provide you with the more detailed code at the moment.) Obviously, this is a quick and dirty way.

    If your tested application allows it, my preference is like this:

    -- get reference to the target grid (wait for it);

    -- get its current number of (displayed) records;

    -- in a loop, with some small delay (e.g. 500ms), for some timeout, wait until the number of records is greater than zero and does not change (which means that the grid has been populated with data).