Forum Discussion

royd's avatar
royd
Regular Contributor
5 years ago
Solved

Need help with 'GeneralEvents_OnLogError'

Hi All

 

I am trying to save the log when TestComplete encounters an error and stops. After much reading, searching I have this bit working.

 

function swceLogin(){
Browsers.Item("iexplore").Run("[APPLICATION URL]"); var browser = Sys.Browser("iexplore"); var page = browser.Page("*THC*"); var userName = page.FindChildEx("idStr", "txtUserName", 10, true, 10000); var userPass = page.FindChild("idStr", "txtPassword", 10); var btnLogin = page.FindChild("idStr", "lnkLogin", 10); userName.SetText("[USER]"); userPass.SetText("[PASSWORD]"); btnLogin.ClickButton(); var pName = page.FindChild("idStr", "Name", 15); //wrong valu for 'idStr' for the test to fail var btnSearch = page.FindChild("idStr", "ctl00_masterBodyContent_LinkButtonSearch", 15); pName.SetText("Morgan, Tom"); // test fails here btnSearch.ClickButton(); } function GeneralEvents_OnLogError(Sender, LogParams){ var sPath = "C:\\TestLog\\Log.mht"; if (aqFileSystem.Exists(sPath)) aqFileSystem.DeleteFile(sPath); Log.SaveResultsAs("C:\\TestLog\\Log.mht", lsMHT, false, lesCurrentTestItem); }

Problem is, the log does not contain the error message that caused the script to stop! Is there a way to save the log that includes the error message?

 

Thanks in advance for any help.

  • Hi,

     

    Do you need to export the log at the moment when error occurs?

    The reason of your problem is that the handler is called before the error is actually posted to the log. This gives you a chance to not post it to the log.

     

    You may consider to set your Test Item to stop on error and move your code from the OnLogError handler to the OnStopTest one.

     

5 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Do you need to export the log at the moment when error occurs?

    The reason of your problem is that the handler is called before the error is actually posted to the log. This gives you a chance to not post it to the log.

     

    You may consider to set your Test Item to stop on error and move your code from the OnLogError handler to the OnStopTest one.

     

    • royd's avatar
      royd
      Regular Contributor

      Hi Alex

       

      Thanks for your insight. Just tried it and it worked!! :D I really appreciate your help.

       

      Regards.

       

      Dave

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The OnLogError event handler interrupts the log process.  IT is not until AFTER the event handler executes that the error gets written to the log.  So, by doing the export within the handler, you won't get the error in the log without explicitly putting it there.  You could try adding your export to the OnStopTest event handler instead.

    I would suggest, also, before you do the export call Log.SaveToDisk to make sure you've flushed the latest entries to disk before you do the export.

    • royd's avatar
      royd
      Regular Contributor

      Hi Robert

       

      Thanks for your and Alex's insight. I got it to work. To your point "... make sure you've flushed the latest entries to disk before you do the export.". I have done this:

        var sPath = "C:\\TestLog\\Log.mht";
      
        if (aqFileSystem.Exists(sPath)) 
        aqFileSystem.DeleteFile(sPath);

      I thought it should delete the last 'Log.mht' file. If I am wrong, can you please tell me how I should do just that?

       

      Regards.

       

      Dave

       

      • cunderw's avatar
        cunderw
        Community Hero

        Correct, that will delete the old file. tristaanogre's suggestion was to call the save to disk so that the logs are already written when you export instead of residing only in memory.