Forum Discussion

msap's avatar
msap
Frequent Contributor
6 years ago

If else statement issue

Hi 

inserted below code , it does not move to else statement in case of the object does not exists. Gives an error messageform3 any suggestions pls

 

if (MessageBoxForm3.Exists){
MessageBoxForm3.SimpleButton.ClickButton();
ExcelImportForm.Close();
Log.Message("Import is successful")
}
else
if (MessageBoxForm4.Exists)
{ MessageBoxForm4.SimpleButton.ClickButton();
ExcelImportForm.Close();
MessageBoxForm7.SimpleButton.ClickButton();
Log.Error("Import fails");
}
ClickItem("Refresh");
}

 

 

 

 

 

 

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    You can't check for the existance of an object if the object does not exist... "Exists" is a property of the object... so, in order to read the property, the object needs, first, exist.

    so... what you need to do first is use something like "FindChild" or "WaitAliasChild" or something similar to retrieve the MessageBoxForm3 object...  Either of those two methods, if the object does not exist, return a "stub" object that only has one property: Exists... with a value of false.

     

    See https://support.smartbear.com/testcomplete/docs/app-objects/common-tasks/checking-existence.html?#checking-whether-an-object-exists-from-scripts

     

     

    • msap's avatar
      msap
      Frequent Contributor

      Thanks alot martin.

      but now the if statement doesnot run after implementing below code.else statement runs but if statement doesnot. any suggestions pls

       

      var PropArray, ValuesArray, p, w;

      // Creates arrays of property names and values
      PropArray = new Array("Aliases.Framework.XtraMessageBoxForm3", "Aliases.Framework.XtraMessageBoxForm4");
      ValuesArray = new Array(true, true);

       

      p = ExcelImportForm;
      w = ExcelImportForm.FindChild(PropArray, ValuesArray, 5);


      if (w.Exists)
      {

      MessageBoxForm3.SimpleButton.ClickButton();
      ExcelImportForm.Close();
      Log.Message("Import is successful")
      }

      else
      {

      MessageBoxForm4.SimpleButton.ClickButton();
      ExcelImportForm.Close();
      MessageBoxForm7.SimpleButton.ClickButton();
      Log.Error("Import fails"); }

      }

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        By definition, .FindChild() looks for some child of some parent.

        "Aliases.Framework.XtraMessageBoxForm3" represents a full object's name and thus it cannot be used with .FindChild().

        Possible implementation:

         

        w = Aliases.Framework.WaitAliasChild("XtraMessageBoxForm3", 10000);

        if (w.Exists)

          ...

        else

          ...