Forum Discussion

oychw's avatar
oychw
Contributor
14 years ago

Could we clear Logs in scripts?

Could we clear Logs in scripts?

for example: When we run a test case, Clear the old logs in scripts.

8 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Rongzhong,



    Tools|Options|Engines|Log|Store all logs and Number of recent logs to keep (set to 0) - is this what you are looking for?
  • Alexei:

        thanks! This can do ,set it to do. can only keep 1 log.
  • Hi Alex,



    But is there any way to clear log during test run?

    I have a script that is set to repeat if during the test run any errors occurred. The problem with this approach is that in the final log, the test appears with red (error), and this ruins my results.

    What I need is somehow, between the 1st and 2nd run of the test (in case of errors), the current log for the running test to be cleared.



    Is this possible?



    Thanks,

    Andrei 
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Andrei,



    I'm really doubting that it is possible to clear logs dynamically during runtime.



  • Hello Andrei,





    Let me rephrase your problem a little. You have a test script that is run when any errors occur in the main test. Since the "Number of recent logs to keep" options is set to 1, TestComplete clears the log of the main test.





    Unfortunately, you cannot delete the log from within scripts. We are interested in any feedback from our customers, so could you please describe in detail what you need to remove logs automatically for, so I can add your explanation to the corresponding feature request. Thanks in advance.

  • Hello Alex,



    That approach to repeat a certain test item if errors are found in the main log was the first approach I tried when I started using TC. But as I understand, this cannot be implemented in JScript due to some problems with scripts communicating with each other, maybe some language limitation or something (this was the conclusion from a topic started about 10 months ago I think)



    So I picked another approach, which is not so elegant.

    In every script that is prone to error I have the following code:



    function test()

    {

    var  ErrCount = Log.ErrCount;

    ..........[script execution]

    if((Log.ErrCount > ErrCount) {

    //re execute current script

    }



    }



    This happens for the current test item, and it runs a maximum of 3 times.

    What I was looking for was a method that in case an error was posted to the log after the fist running attempt, before the second attempt on running the test was started, the log for the current test item to be deleted so that at the end of the test, in case that the 2nd or 3rd run was successful, the error messages from the 1st run not to be displayed, thus affecting the main log, although the test item (at the 2nd or 3rd  execution) was successful.

    Am I making sense?



    That is why I could use such a feature of TC, so that between 2 runs of the test item, the temp log for that specific test item to be deleted / erased and started a new one.






  • P.S.:  If relevant, my project is set to keep all logs.

  • Hello,





      Thank you for the detailed description of your problem. As far as I understand, you need to cancel posting entries (messages, warnings, errors, etc.) to the log if your test script runs with errors. That is, you need to remove particular entries from the log of the current test run, rather than remove the whole execution log (shown in the Project Explorer panel), right?





    If so, I can suggest the following solution:

    Handle TestComplete log events (OnLogMessage, OnLogWarning, etc.). In the event handler, you can block posting the current item to the Test Log, but write the item information to a temporary variable. If the test runs without errors, you'll be able to post the log information stored in the temporary variable to the TestComplete log from your script. If the test runs with errors, and you don't want its execution results to be present in the log, you just don't post the temporary variable contents to the log.

    When implementing this suggestion, you will need to handle all OnLog<...> events so that you can completely redefine the TestComplete log's logic.





    To help you understand my suggestion, I will demonstrate it with some code below. However, please note that the suggested approach has some restrictions:

    1. You cannot post pictures to the log.

    2. Log entries timestamps cannot be modified (the Time column in the Test Log panel), so you have to add a correct timestamp to an entry's Message contents from script.

    3. Navigation to script lines from the Test Log won't work.





    So, the sample script demonstrating my idea is below. Hopefully, it will be useful for you:







    // This script illustrates how to handle TestComplete Log events.

    // It uses Notepad as the tested application.

    // In order to make the script work, link the

    //   GeneralEvents_OnLogError and GeneralEvents_OnLogCheckpoint routines to the corresponding events.

    // Before running, add a new variable called UseMyLog to project temporary variables.





    var errCount = 0;

    var MY_LOG = new Array();





    function MyTest()

    {

      var edit = Sys.Process("notepad").Window("Notepad", "*").Window("Edit");

      aqObject.CheckProperty(edit, "wText", cmpEqual, "test", false);

    }





    function Main(){

      if(TestedApps.Count == 0)

        TestedApps.Add("Notepad", "C:\\Windows\\System32\\Notepad.exe");





      TestedApps.Items("Notepad").Run();

      var edit = Sys.Process("notepad").Window("Notepad", "*").Window("Edit");





      // Start tapping

      Project.Variables.UseMyLog = true;





      // Set incorrect text to emulate an error

      edit.SetText("fail");





      // Start the test

      MyTest() ;





      if(errCount >0){

        errCount = 0;

        MY_LOG =  new Array();  

        // Set a correct text and start the test again 

        edit.SetText("test");

        MyTest();

        }





      // End tapping

      Project.Variables.UseMyLog = false;

      if(errCount == 0)

        PostLog();

    }





    function PostLog()

    {

      for(i = 0; i< MY_LOG.length; i++){

        var attr = Log.CreateNewAttributes();

        attr.ExtendedMessageAsPlainText = false;

        var date =  MY_LOG["Date"];

        date = aqConvert.DateTimeToFormatStr(date, "%m/%d/%Y %H:%M");

        eval("Log." + MY_LOG["Type"] +

              "(date + \" | \" + MY_LOG[\"Str\"]," +

              "MY_LOG[\"StrEx\"]," +

              "MY_LOG[\"Priority\"]," +

              "attr)");

        }

    }





    function AddLogItem(Type, LogParams)

    {

      var rec = new Array();

      MY_LOG[MY_LOG.length] = rec;

      rec["Date"] = aqDateTime.Now();

      rec["Priority"]  =  LogParams.Priority;

      rec["Str"]  =  LogParams.Str;

      rec["StrEx"]  =  LogParams.StrEx;

      rec["Type"] =  Type;

    }





    function GeneralEvents_OnLogError(Sender, LogParams)

    {

      if(!Project.Variables.UseMyLog) 

        return;

      errCount +=1;

      LogParams.Locked = true;

      AddLogItem("Error", LogParams); 

    }





    function GeneralEvents_OnLogCheckpoint(Sender, LogParams)

    {

      if(!Project.Variables.UseMyLog) 

        return;

      LogParams.Locked = true;

      AddLogItem("Checkpoint", LogParams);           

    }