Forum Discussion

eric_robinson's avatar
eric_robinson
Occasional Contributor
14 years ago

Setting a timeout value for Exists method

I would like to use the Exists function to determine if a message box has appeared or not and if so, click the yes button so the document can continue loading.  The message box won't always appear so I can't just wait around for it before checking a property on another window that would mean the document has loaded.



My plan was to loop 150 times and inside the loop, call messageBox.Exists and otherWindow.WaitProperty and have them both time out after 1 second, then loop again. That way, I wait for up to 5 minutes which is enough time for the otherWindow to load which would mean there was not going to be a messageBox since the MessageBox would load first.



for (i = 0; i < 150; i++)

  {

    if (dlgSoftwareUpdateRecommended.Exists)

    {  

      dlgSoftwareUpdateRecommended.btnYes.ClickButton();

      break;

    }  

    

    //Wait for one second for the window to become focused.  

    isLoaded = mapWindow.WaitProperty("Focused", "True", 1000);  

    if (isLoaded)

      break;

  }



  if (!isLoaded)

  {

    var remainingWaitTime = (150 - i) * 2000

    Log.Message(remainingWaitTime, "Remaining time")   

    //Wait for up to remainingWaitTime for the window to become focused which would mean the document is loaded

    isLoaded = mapWindow.WaitProperty("Focused", "True", remainingWaitTime);

  }



My problem is, there is no timeout parameters for Exists.  I don't want to change the global timeout time to 1 second.



I see there is the ability using a keyword test to change the timeout value for Exists, but that leads to another problem.  I want the object I am calling Exists on to be a parameter I pass in to the keyword test, but I can't figure that out. When I use the "If Object" operation, it doesn't let me use a parameter for the operation column.  The value column must be "Exists".  I would like it to be parametrized so I can reuse the keyword test for other window object.



I am brand new to TestComplete so forgive me if there is an easy solution.  But thanks for any input!
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Instead of calling Exists directly as you have, you need to use a Wait method of some sort to find and return your dlgSoftwareUpdateRecommended object.  Check out http://smartbear.com/support/viewarticle/14496/ for more information.



    The WaitNNN method you choose, no matter which one, will have that timeout setting as one of the parameters.
  • eric_robinson's avatar
    eric_robinson
    Occasional Contributor
    Thanks!  That works, but tell me why



    if (Sys.Process("notepad").WaitWindow("#32770", "About Notepad", 1, 5000).Exists)



    takes 5 seconds but



    var window = Sys.Process("notepad").WaitWindow("#32770", "About Notepad", 1, 5000)

    if (window.Exists)



    takes 15 seconds when the default timeout for the project is 10 seconds and the window can't be found?  I assume the



    window.Exists



    methods takes up to the default timeout, which is 10 seconds.  So does the Exists in the first line of code not called or is it just treated differently?



    Whatever the reason, it does work.  I am just curious as to why.  Thanks again!
  • Hi Eric,


    The statement if (Sys.Process("notepad").WaitWindow("#32770", "About Notepad", 1, 5000).Exists) takes 5 seconds because you set the Timeout parameter of the WaitWindow method to 5000 milliseconds.


    Even if the Auto-wait timeout project property is set to 10, this statement will take 5 seconds, because this option affects neither the WaitWindow method nor the Exists method.


    I cannot reproduce the situation when var window = Sys.Process("notepad").WaitWindow("#32770", "About Notepad", 1, 5000)

    if (window.Exists)
    takes 15 seconds.

  • eric_robinson's avatar
    eric_robinson
    Occasional Contributor
    Does the Exists function not use the auto timeout?  I thought that was what was happening, however, I could have been mistaken.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Since the window object has already been resolved and assigned using the WaitWindow method, Exists is just a check against the property of the object rather than having to first find the object and then call exists.  Without WaitWindow, you would expect the Exists call to take the 10 seconds. 
  • Hi Eric,


    Robert is right. The Exists call can take 10 seconds (the auto-wait timeout) if you use, for example, the following code:




    var window = Sys.Process("notepad").Window("#32770", "About Notepad", 1)

    if (window.Exists)

      Log.Message ("Success")

  • eric_robinson's avatar
    eric_robinson
    Occasional Contributor
    That is probably what I was doing wrong then.  I may not have used WaitWindow and Exists together like I thought I had.  Thanks for all the input!