Forum Discussion

Lee_M's avatar
Lee_M
Community Hero
7 months ago
Solved

If Object exists - Script and Keyword

HI,

 

The IF Object in either script or Keywords tests is not reliable for me.

 

When using ".Exists" or KW equivalent the code will execute and may bring back the correct results but the issue is the test will ALWAYS fail

 

i.e.

 

a button on my screen is called "clickMe" (for this example we can call the whole Name mapping "clickMe")

 

 

pseudo Code:

if clickMe.Exists 
  "Found the button"
   clickMe.clickButton()
else
  "Button has not been found"

 

 

 

CASE: Button exists: SUCCESS

   If the button exists the test will proceed to click the button

 

CASE: Button does NOT exist: FAIL

  If Object fails and the button will NOT be clicked

 

 

Issue, on the failed case, the test executes as expected but the Log will show an error on what would be a positive test

 - when I am running regression tests (380+) I can't have all the tests failing when in fact everything has passed (even 1 case this is unacceptable functionality)

 

This has forced me to instruct my team NOT to use this functionality and it has rendered  over 100 tests back to labour intensive manual testing

 

  • I've always found this annoying and confusing. When using Exists, we're checking the objects property value is either true or false (the object must not be null). But to validate the object state, you may need to check if the object exists in the system. Using the appropriate WaitNNN method, such as WaitChild method, gets around this problem.

     

    Here's an example, using WaitChild to find a button which doesn't exist. 

    It's not possible to use,

     

    if (btn !== null)
    // or
    if (btn != null)

     

    because WaitChild method is returning a stub object, btn will not be null.

3 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I've always found this annoying and confusing. When using Exists, we're checking the objects property value is either true or false (the object must not be null). But to validate the object state, you may need to check if the object exists in the system. Using the appropriate WaitNNN method, such as WaitChild method, gets around this problem.

     

    Here's an example, using WaitChild to find a button which doesn't exist. 

    It's not possible to use,

     

    if (btn !== null)
    // or
    if (btn != null)

     

    because WaitChild method is returning a stub object, btn will not be null.

  • Lee_M's avatar
    Lee_M
    Community Hero

    Thank you for this insight

     

    SmartBear engineers just said it was working as expected which didn't help me

    It is a pity that TestComplete doesn't make this function do what it says on the tin

     

    PS: .visiable working this way for me too - they needed a return value

     

     

    the annoying part is that it sometimes works if you have a statically Namemapped field

     

    e.g.

    working:

    function test_exist(){
      
      if(Aliases.browser.page.txt_loginUsername.Exists){
        Log.Message("found")
      } else {
        Log.Message("NOT FOUND")
      }
    }

     

     

    but his DOES NOT work

    function test_exist(){
      
      if(Aliases.browser.OBDX.FindElement("//el/comp//oj-input-text//input").Exists){
        Log.Message("found")
      } else {
        Log.Message("NOT FOUND")
      }
    }

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    The documentation of FindElement, mentions for Return Value - "If no matching object was found, the method returns an empty value (null in JavaScript, JScript, C#Script, and C++Script, None in Python, Nothing in VBScript, nil in DelphiScript) and posts an error message to the test log". 

     

    This doesn't work,

    because it's return a stub object!