Search code examples
nunitcruisecontrol.netwatin

What is better approach to wait for elements on a page Waitforcomplete() or system.threading.thread.sleep()


I am Using WaitforComplete() in watiN but it doesnt seems to work well. As it executes the next statement even if you have given longer time to wait. I am using thread.sleep() to stop my application until it gets the desired page or element. But the thing is pages are so much dynamic that sometimes it takes much longer time as specified.

Any better solution. Any thing that will catch the page return dynamically and dont go to execute next statments in application.

Sample of Code

    'Show Details page
    Assert.AreEqual("Confirmation", _internetExplorer.Title)

    If (_internetExplorer.Button(Find.ById(New Regex("btnFinish"))).Exists) Then
        _internetExplorer.Button(Find.ById(New Regex("btnFinish"))).Click()
    Else
        Assert.Fail("Could not Find Finish Booking Button on Confirmation Page")
    End If

    System.Threading.Thread.Sleep(100000)

'Show Booking Summary page Assert.AreEqual("Display Booking", _internetExplorer.Title)

I want something that detect the return of page dynamically. instead of giving some constant value.


Solution

  • The WaitForComplete method esentially moves on once the browser has set it's readystate to comllete and the busy state to false.

    What I typically do is to try and access what you need to, then perform a thread.sleep for say half a second, then try again. I also have a global timeout that quits after say 10 seconds.

        int timeout = 20;
        bool controlFound = false;
        for (int i = 0; i < timeout; i++)
        {
            if (_internetExplorer.Button(Find.ById(New Regex("btnFinish"))).Exists)
            {
                _internetExplorer.Button(Find.ById(New Regex("btnFinish"))).Click();
                controlFound = true;
                break;
            }
            else
            {
                System.Threading.Thread.Sleep(500);
            }   
        }
    
    
       if (!controlFound)
       {
            Assert.Fail("Control not found");
       }