Forum Discussion

MrDysprosium's avatar
MrDysprosium
Contributor
8 years ago

Checking if an object exists causes an error if the object does not exist. Is there a work around?

  if Sys.Process("SWSetup-" + Project.Variables.versionNumber, 2).Window("#32770", "Setup", 1).WaitWindow("Button", "OK", 1, 120000).Exists:
    Sys.Process("SWSetup-" + Project.Variables.versionNumber, 2).Window("#32770", "Setup", 1).Window("Button", "OK", 1).Click()
  else:
    Log.Message("TEST")  

Here's a snippet of code... the window I'm checking only exists under certain circumstances. So, if that window exists, I need to do something slightly different.

I find it kind of ridiculous that TC just fails the project if this check fails. Shouldn't it just continue to the "else"? Even if I wrap this in a try/catch, TC still fails the project if that window doesn't exist.

Is there a work around? I certainly hope so!

2 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    So, you're doing a WaitWindow on the OK button?  Is that what you're checking for existance?  Or is it the Setup window before hand?  See, WaitWindow waits for the item being looked for in the WaitWindow method, in this case, an object with a class of button, caption of OK, an index of 1, and it ways 2 minutes for it to show up.

     

    If, however, the window that you need to wait for is the setup window, change your code to

      if Sys.Process("SWSetup-" + Project.Variables.versionNumber, 2).WaitWindow("#32770", "Setup", 1, 120000).Exists:
        Sys.Process("SWSetup-" + Project.Variables.versionNumber, 2).Window("#32770", "Setup", 1).Window("Button", "OK", 1).Click()
      else:
        Log.Message("TEST") 

    Note that we're waiting in this for the SetupWindow to exist before we go on to click the OK button.  This is the proper usage of WaitWindow.  You call it on the parent object of the window you want to wait.

     

    • KSQian's avatar
      KSQian
      Contributor

      Robert's solution will work.

       

      But if the window appears and the Ok button isn't ready to be clicked, then that solution will break.

       

      Here's a pure code take on it. Although in this case I may use the built-in waitwindow function. 

       


      const window = Sys.Process("SWSetup-" + Project.Variables.versionNumber, 2).WaitWindow("#32770", "Setup", 1, 120000)

      if (window.Exists) {
      const button = window.Find(["Button"]) //Edit find with correct parameters
      if ( button.Exists && button.Enabled) {
      button.Click()
      }
      else {
      Log.Message("window exists, but button cant be clicked.")
      }
      }
      else {
      Log.Message("window doesnt exist")
      }