Forum Discussion

Nikolayev's avatar
Nikolayev
Contributor
5 years ago

How to manage Errors as not or Warnings with Testcomplete

Hi everyone in SmartBear Community,   Sorry for my aproximate poor, or roughly English. I hope you will understand me !   I have a simple Test Result question  with an "WPF" application called "P...
  • BenoitB's avatar
    5 years ago

    You can control by different ways:

    - use the event, General events -> onLogError and when the error message is the wanted to ignore then you can lock the log system

    - use the Log.Enabled property to switch off/on the logging system

    - use a try/catch/finally to intercepet already known errors

     

    Each one has advantages and disadvantages, try them to learn them and choose the best suited to you :)

     

     

    Below on on my complete event management, look the part about skip

     

    /* ---------------------------------------------------------------------------
     Gestionnaire des évènements d'erreur
     - Prend en charge les répétitions de messages identiques
     - Prend en charge les conditions d'ignorance de la remontée d'erreur
     - Prend en charge la transformation d'erreur d'object inexistant en simple avertissement
     - Transmet une capture écran au log
     - Applique une mise en forme spécifique
    --------------------------------------------------------------------------- */
    function GeneralEvents_OnLogError(Sender, LogParams) {
      // Message d'erreur identique alors on met de coté
      if (LogParams.MessageText == Globals.LASTERRORLOG)  {
        Globals.SAMEERRORCOUNT++;
        LogParams.Locked = true;
      }
      else {
        // Gérer les erreurs que l'on souhaite ignorer
        let skip = false;
        for (let i = 0; i < Globals.MESSAGETOSKIP.length; i++) {
          skip = skip == false ? aqObject.CompareProperty(LogParams.MessageText, cmpContains, Globals.MESSAGETOSKIP[i], false, lmNone) : true;
        }
        // Condition pour ignorer remplie
        if (skip) {
          Log.Message(LogParams.MessageText);
          LogParams.Locked = true;
        }
        else {
          // Transformer les erreurs d'objet inexistants en warning
          if ((Globals.IGNORENOTFOUND) && ((aqObject.CompareProperty(LogParams.MessageText, cmpContains, "Unable to find the object", false, lmNone)) || (aqObject.CompareProperty(LogParams.MessageText, cmpContains, "The object does not exist.", false, lmNone)))) {
            Log.Message(LogParams.MessageText, LogParams.AdditionalText, pmHigher, qa.system.logWarning, screen);
            LogParams.Locked = true;
          }
          else {
            // Gérer les répétitions
            if (Globals.SAMEERRORCOUNT > 1) {
              Log.Message("Erreur '" + Globals.LASTERRORLOG + "' répétée " + Globals.SAMEERRORCOUNT.toString() + " fois !", "", pmNormal, qa.system.logInfo);
            }
            Globals.LASTERRORLOG     = LogParams.MessageText;
            // Dérouter le gestionnaire d'évènement avant de faire un log d'erreur
            let f                    = GeneralEvents_OnLogError;
            GeneralEvents_OnLogError = null;
             // Récupérer la capture d'écran existante ou en générer une
            let screen               = qa.system.lastExceptionScreen != null ? qa.system.lastExceptionScreen : Sys.Desktop;
            let addInfo              = ((LogParams.AdditionalText == null) || (LogParams.AdditionalText == "")) ? qa.system.lastExceptionStack : (LogParams.AdditionalText != qa.system.lastExceptionStack) ? LogParams.AdditionalText + '\n' + qa.system.lastExceptionStack : LogParams.AdditionalText;
            Log.Error(Globals.LASTERRORLOG, addInfo, pmHighest, qa.system.logError, screen);
            Globals.TESTRESULT       = false;
            GeneralEvents_OnLogError = f;
            Globals.SAMEERRORCOUNT   = 0;
            LogParams.Locked         = true;
          }
          // Réinitilaliser la capture d'écran et la stack
          qa.system.lastExceptionScreen = null;
          qa.system.lastExceptionStack  = "";
        }
      }
    }
  • tristaanogre's avatar
    5 years ago

    In both sections of code, you should be using a "Wait" method before testing for "Exists".

     

    So, replace 

     

    if Aliases.PI_UI.FrmPIMenu.Exists

     

    with

     

    if Aliases.PI_UI.WaitAliasChild("FrmPIMenu", 20000).Exists

     

    and replace

     

    If Sys.Process("PI.UI.exe").Exists

     

    with

     

    Sys.WaitProcess("PI.UI", 20000).Exists

     

    Doing an "Exists" check the way you were doing it won't work... you can't check the "Exists" property of an object that does not exist...  So, the "Wait" processes return a "stub" object if the object is not found with the "Exists" property set to False.