Forum Discussion

delito's avatar
delito
Occasional Contributor
5 years ago

Best way to test if an element doesnt exist

Hi

 

What is the best way to test if an element doesn't exist in Test Complete?

 

I dont have any sample code to share, but here is a business requirement scenario:

  1. Testcase 1 - check a button is visible for admin
  2. Testcase 2 - check the same button is not visible for user

 

  • Exists is the only property that does not fail if not found.

     

    Another trick I use is adding the "VisibleOnScreen" property to the name mapping of the object so that it does not exist until its actually usable on screen.  Also the "Enabled" property but my particular application this can be unreliable.

     

    Another thing to watch out for is the time-outs on negative tests.  If the Project Default is applied you could be waiting a while for a test to pass.  I use 500-1000ms typically but this depends on the application/environment.

  • Wamboo's avatar
    Wamboo
    Community Hero

    Hi, there is a few possible options:

     

    1) WaitWindow

     

    var p = Sys.Process("app");
    
    Indicator.PushText('Checking object...');
    var obj = p.WaitWindow("propValue", "Value", -1, 1000);
    Indicator.Clear();
    
    if(obj .Visible)
        obj.Click();

     

    2) Aliases

     

    Aliases.app.obj1.RefreshMappingInfo();
    var activeObject = Aliases.app.obj1.FindAllChildren("propName", "*");
    
    if(aqObject.GetProperties(activeObject ).Count != 0)
        return true;
    else
        return false;

     

    3) Exist

     

    var p = Sys.Process("Impuls");
    var obj= p.WaitWindow("propName", "propValue", -1, 1000);
    if(obj.Exists == true) {
         obj.Find("propName", 'propValue', [treeValue]).Click();
    }
    
    

     

    4) Wait for element until it's exist

     

    function waitUntil(obj, licznik) {
      var licznik = licznik || 60;
      for (var i = 1; i <= licznik; i++) {
        if (obj.Exists == true) {
          aqUtils.Delay(1000);
        } else {
          break;
        }
      }
    }
  • delito's avatar
    delito
    Occasional Contributor

    Hi and thanks for the replies but how would wait or exist work ? Remember these are two different test cases. It should never be visible in Test case 2 so would it not fail as an element not found ?

     

    I will read up on Aliases and explore that route.

    • jr348's avatar
      jr348
      Contributor

      Exists is the only property that does not fail if not found.

       

      Another trick I use is adding the "VisibleOnScreen" property to the name mapping of the object so that it does not exist until its actually usable on screen.  Also the "Enabled" property but my particular application this can be unreliable.

       

      Another thing to watch out for is the time-outs on negative tests.  If the Project Default is applied you could be waiting a while for a test to pass.  I use 500-1000ms typically but this depends on the application/environment.

      • sonya_m's avatar
        sonya_m
        SmartBear Alumni (Retired)

        Awesome advice, everyone!

         

        delito Did the replies help? If so, could you mark the best one as a solution? Thank you.