Forum Discussion

XBug's avatar
XBug
Contributor
8 years ago

While loop keeps on throwing an error

My while loop was setup as such:

 

While (Object_A.VisibleOnScreen=True AND Object_B.VisibleOnScreen=False) {

Delay = 2000ms, "Reloading..."

}

 

Note: Object_A appears before Object_B, they do not appear at the same time

 

Problem: Test Complete will throw an error when Object_B was not detected

 

I tried just using While (Object_A.VisibleOnScreen=True) but when the property returned False, Test Complete throws an error as well.

 

I am on Test Complete version 11.

 

Attached is an SC of the While loop and the Error TC is throwing.

 

 

  • Hi,

     

    You want to suppress the error when that object not exists in the screen since you are expecting object will get visible on the screen after some time.

     

    If my understanding correct, before checking VisbleOnScreen you need to check the Exists property in-order to suppress the object not exists error in the TC logs.

     

    Like below,

     

    if(Object_A.Exists)
    {
        Object_A.VisbleOnScreen;
    }
  • shankar_r's avatar
    shankar_r
    Community Hero

    Change your settings to like below in-order to loop to run.

     

    Un-check the Stop on error in Tools > Current project properties > Playback

     

    • XBug's avatar
      XBug
      Contributor

      Did just that but still the same. i am getting an error that the object (Object_B) does not exist.

      • shankar_r's avatar
        shankar_r
        Community Hero

        Hi,

         

        You want to suppress the error when that object not exists in the screen since you are expecting object will get visible on the screen after some time.

         

        If my understanding correct, before checking VisbleOnScreen you need to check the Exists property in-order to suppress the object not exists error in the TC logs.

         

        Like below,

         

        if(Object_A.Exists)
        {
            Object_A.VisbleOnScreen;
        }
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Correct... you cannot check any propertype of an object if it does not exist.

    You can consider using WaitChild or WaitAliasChild in your while loop conditionals. These will allow you to use the VisibleOnScreen property for your validations but, honestly, even with the Wait methods, you probably would be better checking Exists rather than Visible onscreen
    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      Just an FYI, I'd write your code more something like the following.  Note, I'm assuming you're using NameMapping and that ObjectA and OBjectB have the same parent object.

       

      While ((parent.WaitAliasChild(Object_A, -1).VisibleOnScreen) && (!parent.WaitAliasChild(Object_B, -1).VisibleOnScreen)) {
      Delay = 2000ms, "Reloading..."
      }

      As noted above, it might be better to check "Exists" rather than "VisibleOnScreen" as it sounds like it's more a matter of existance than visibility that determines what's going on with the objects.  But, for demonstration purposes, I'm posting code similar to what you originally posted.

  • rickm's avatar
    rickm
    Contributor

    Seems straightforward to me. If you want to wait until both are visible (and you're sure they both will be eventually), then:

     

    While (Object_A.Exists=False OR Object_A.VisibleOnScreen=False OR

               Object_B.Exists=False OR Object_B.VisibleOnScreen=False) {

        Delay = 2000ms, "Reloading..."

    }

     

    The test should stop checking and enter the block upon the first true test which is before it hits a test that throws an error. It won't break from the loop until all 4 conditions test false which is when both objects exist and both objects are visible.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      If Object_A.Exists is false, you can't check the properties of "Exists" or "VisibleOnScreen"... so, you will still throw an error.  This is why my suggestion to use "WaitChild" or "WaitAliasChild".... Those two methods will return a "stub" with all these properties set to "False" if the object does not exists... it will return the actual object if the object DOES exist.  It's a best practice method for detecting objects.

      • rickm's avatar
        rickm
        Contributor

        When I've done this it has been in a Keyword Test using the "If Object" operation. Its document states:

         

        Select Condition
        On this page you can specify the object state to be checked. 
        The page lists the following states: Exists Does not exist Enabled Disabled Visible Invisible

        and:

         

        Scripting Analogue
        The operation is a keyword test analogue to checking the object 
        properties in script with the if... then statement.

        In my Keyword test, it looks like:

         

        If Object...Object_A...Not Exists

         

        I found a While loop in a Keyword Test that is structured like this:

         

        While (Object_A Equals False OR Object_A.Visible Equals False)

         

        Note the first test does not specify a property. This definitely works, I just don't know what the script code is supposed to look like. Years ago TC had an option to convert a Keyword Test into script code but I can't find it now. Was it too deprecated?