Forum Discussion

tphillips's avatar
tphillips
Frequent Contributor
4 years ago

Disable warning about text being truncated

I am writing a test that checks that when it enters text in a text field that has a maxLength set, the text gets truncated. I noticed that TestComplete (helpfully) logs a Warning when it tries to enter text (using SetText()) which is too long into a field with maxLength set.

 

Short of disabling the Log before setting the text and re-enabling afterwards, is there any way to tell TestComplete I don't care about if the text gets truncated (I am validating that myself, I expect it to get truncated).

 

I could use an event to catch when it's about to log a warning and check the warning text, but I would like to avoid that if possible.

  • Use the onLogWarning event.

     

    2 ways, check the Sender object to filter on source basis or check the LogParams to filter on message basis.

     

    Below a sample of message basis filtering :

     

    var globals = {};
    
    globals.WARNINGTOSKIP = ['The text cannot be fully entered', 'The length is too long', 'Another message to skip']; // Array of the warnings message to skip
    globals.IGNOREWARNING = false; // True then convert Log.Warning into Log.Message
      function GeneralEvents_OnLogWarning(Sender, LogParams) {
        // Detect if, based on log message, it's a warning to be totally ignored
        let skip = false;
        for (let i = 0; i < globals.WARNINGTOSKIP.length; i++) {
          skip = skip == false ? aqObject.CompareProperty(LogParams.MessageText, cmpContains, globals.WARNINGTOSKIP[i], false, lmNone) : true;
        }
        if (skip) {
          Log.Message(LogParams.MessageText);
          LogParams.Locked = true;
        }
        else {
          // If just message instead of warning
          if (globals.IGNOREWARNING) {
            Log.Message(LogParams.MessageText, LogParams.AdditionalText);
            LogParams.Locked = true;
          }
          else {
            // Need to switch to avoid reentrancy
            let f                      = GeneralEvents_OnLogWarning;
            GeneralEvents_OnLogWarning = null;
            Log.Warning(LogParams.MessageText, LogParams.AdditionalText);
            GeneralEvents_OnLogWarning = f;
            LogParams.Locked           = true;
          }
        }
      }
    

     

     

  • BenoitB's avatar
    BenoitB
    Community Hero

    Use the onLogWarning event.

     

    2 ways, check the Sender object to filter on source basis or check the LogParams to filter on message basis.

     

    Below a sample of message basis filtering :

     

    var globals = {};
    
    globals.WARNINGTOSKIP = ['The text cannot be fully entered', 'The length is too long', 'Another message to skip']; // Array of the warnings message to skip
    globals.IGNOREWARNING = false; // True then convert Log.Warning into Log.Message
      function GeneralEvents_OnLogWarning(Sender, LogParams) {
        // Detect if, based on log message, it's a warning to be totally ignored
        let skip = false;
        for (let i = 0; i < globals.WARNINGTOSKIP.length; i++) {
          skip = skip == false ? aqObject.CompareProperty(LogParams.MessageText, cmpContains, globals.WARNINGTOSKIP[i], false, lmNone) : true;
        }
        if (skip) {
          Log.Message(LogParams.MessageText);
          LogParams.Locked = true;
        }
        else {
          // If just message instead of warning
          if (globals.IGNOREWARNING) {
            Log.Message(LogParams.MessageText, LogParams.AdditionalText);
            LogParams.Locked = true;
          }
          else {
            // Need to switch to avoid reentrancy
            let f                      = GeneralEvents_OnLogWarning;
            GeneralEvents_OnLogWarning = null;
            Log.Warning(LogParams.MessageText, LogParams.AdditionalText);
            GeneralEvents_OnLogWarning = f;
            LogParams.Locked           = true;
          }
        }
      }
    

     

     

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      BenoitB :

              // Need to switch to avoid reentrancy
              let f                      = GeneralEvents_OnLogWarning;
              GeneralEvents_OnLogWarning = null;
              Log.Warning(LogParams.MessageText, LogParams.AdditionalText);
              GeneralEvents_OnLogWarning = f;
              LogParams.Locked           = true;

      Great piece of code!

      Not sure if it works using any other but JavaScript language in TC... Need to check.

       

       

    • tphillips's avatar
      tphillips
      Frequent Contributor

      Yeah, I was hoping to do it without having to go all-out and use an event like this.

      I have ended up just disabling logging when I know I am setting text that's too long and re-enabling it afterwards.

      • BenoitB's avatar
        BenoitB
        Community Hero

        I understand that you want to keep things in KISS principle and i was like you until a day i lost several hours because an error occured inside Log.Enabled off section ..  :^)

        So ideally you should use a try .. finally pattern for the log off/on.

         

        Advantages of event method are :

        - Event exists for that kind of action

        - It keep things centralized (reduce cost of maintenability)

        - It allows you to add other stuff like making choice if real log made will be warning or message or even error or like avoiding multiple entries of same log or adding additionnal informations or ...

         

        Yes this is heavier but once ..