Forum Discussion

Raj_Qa's avatar
Raj_Qa
Contributor
14 years ago

Test Logs when emailed

I have Testcomplete setup to send out the test logs to a group when the tests fail. When the test log file(MHTML) is saved and opened it shows the tests have passed, but the actual log has a red cross, but in the emailed log it has a tick stating the test passed.



Sub GeneralEvents_OnLogError(Sender, LogParams)

  log.Message("Script execution stopped because of an error")

  FileName = Project.ConfigPath & "ExportedLog\MyResults.mht"



  Call Log.SaveResultsAs(FileName, lsMHT)

If SendMail("***********@gmail.com", "******", "***", "****net", "Notification", "This Email is sent from TestComplete.","C:\SmokeTests\Smoke_Test (1)\Smoke_Test\Smoke_Test\ExportedLog\MyResults.mht") Then

  Log.Message "Mail was sent"

Else

  Log.Warning "Mail was not sent"

End If

End Sub

9 Replies

  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    Hi Raj,


    As far as I understand, you run a project with several (8-10) test items, right? In this case, the OnStopTest event rises each time when the test item is stopped. That's why, TestComplete sends several messages via e-mail.


    To send only one message via e-mail, you need to specify appropriate code (that sends test results) only once in the project, for example, in the last test item.


    So, to solve this problem, I recommend that you do the following:

    1. Create a new script routine (for instance, SendingResults).

    2. Copy the text from the OnStopTest event handler and paste it to the SendingResults routine.

    3. Remove the OnStopTest event handler from your project.

    4. Add the SendingResults routine to the project test items and make sure that this item is specified at the end of the Test Items list. This will guarantee that the SendingResults routine is executed only once (at the end of the project execution) and thus the test results are send only once.


    I hope, this information helps :)

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    I think the problem is that you're sending the log within the OnLogError event handler.  As I understand it, the error is not written to the log yet when this event handler is fired so you actually have the log exported and sent at a point prior to the error.
  • Thanks for the reply Robert



    Can you suggest necessary changes in the script?

    I a using VbScript
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    As I mentioned in my reply, the problem is not in your script but in the "location" in your code where you are calling your script.  You're calling it within an event handler that "interrupts" the process of logging an error in order to do something else before the error gets logged.  So, anything you do within that event handler will not include the error that prompted your code to execute.



    To do precisely what you want requires you to export the log after the error has been written to the log.  This means that you cannot use the OnLogError event handler to do so but must move that export to some other handler or some other script code.



    A suggestion COULD be that, if the logging of the error stops a test, you could us "OnStopTest" event handler and place your code in there rather than in the "OnLogError" event handler.  That would be my first guess, actually, as to where to move things.



    I hope this helps.
  • I used the OnStopTest handler and it worked just fine!!!!!

    Thank You Robert.
  • Noticed a major problem with OnStop event handler. It sends out multiple emails instead of sending out just one email if the script fails. So I have a distribution list that receives about 8-10 emails for a test which takes about 7mins.



    Is there a way in which I can stop this from happening and just have it send one email when the test fails.
  • I understand what your saying, but the problem with your approach is if the test fails on the 2nd or 3rd test item it will not send out the error log

    I need the test to send out the error log in hopefully just one email.
  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    Hi Raj,


    Then, I recommend that you add the OnLogError and OnStopTest events to your test and use the following code:



    Dim IsErrorOccured

    IsErrorOccured = False



    Sub GeneralEvents_OnLogError(Sender, LogParams)

      IsErrorOccured = True

    End Sub



    Sub GeneralEvents_OnStopTest(Sender)

    'replace "TheLastTestItem" in the next line with the name of the last test item to run

    If Project.TestItems.Current.Name = "TheLastTestItem" Or IsErrorOccured Then

      Call Log.SaveResultsAs("File_Name", lsMHT)

      Call SendMail("ClareJ@clarejeffersoncorp.com", "mail.johnsmithcorp.com", "John Smith", "JohnS@johnsmithcorp.com", "Notification", "Hello Clare, Your application is nice.", "File_Name.mht")

    End If

    End Sub