Forum Discussion

Mia's avatar
Mia
Contributor
10 years ago
Solved

Send test result via e-mail in subject

Hello, I need to run my test project in TestComplete10 every night, and I need to be able to send result (Succeded/Error(s) occurred) as a part of a email subject. I run the test project...
  • marin's avatar
    10 years ago
    Hello Miriam,



    we had a similar requirement and here's how we tackled this:



    1. Create a global variable (e.g. "strTestResult")  that would hold the global test result. This is what will appear in your eMail subject later

    2. Override GeneralEvent_OnStopTest and  add sth. like this




    /*

    Function: GeneralEvents_OnStopTest()

    This method assigns global test result (FAILED, WARNING, OK) to test project according to single test results.

     

    Bubbles up to higher severity level, i.e. if WARNING is recorded in any of test cases and no FAILED recorded anywhere,

    global test outcome is WARNING. If FAILED is recorded in any test case, global result will be FAILED.

    */

    function GeneralEvents_OnStopTest(Sender)

    {

      if (Log.ErrCount > 0)

        strTestResult = "FAILED";

      else if (Log.WrnCount > 0 && Log.ErrCount == 0)

        strTestResult = strTestResult != "FAILED" ? "WARNING" : "FAILED";

      else

        strTestResult = strTestResult != "FAILED" ?

                          (strTestResult != "WARNING" ? "OK" : "WARNING") : "FAILED";

    }








    3. Export the test results from TestComplete to a temprary file after the run and then parse it for desired attributes.

    We have following method to do this, the output will be a small HTML table with a quick summary





    function GetTestSummary()

    {

      var tempFolder = "C:\\temp\\" + GetTickCount() + "\\";

     

      if (0 != aqFileSystem.CreateFolder(tempFolder))

      {

        Log.Error("The " + tempFolder + " temp folder was not created");

        return "";

      }

     

      Log.SaveResultsAs(tempFolder, lsXML);

     

      var xDoc = Sys.OleObject("MSXML2.DOMDocument.4.0");

      xDoc.load(tempFolder + "Description.tcLog");      

      // Warning count  

      var wrnC = VarToInteger(xDoc.selectSingleNode('Nodes/Node[@name="root"]/Prp[@name="warning count"]/@value').text);      

      //Error count  

      var errC = VarToInteger(xDoc.selectSingleNode('Nodes/Node[@name="root"]/Prp[@name="error count"]/@value').text);      

      //Execution time  

      var startTime = parseFloat(xDoc.selectSingleNode('Nodes/Node[@name="root"]/Prp[@name="start time"]/@value').text);  

      var stopTime = parseFloat(xDoc.selectSingleNode('Nodes/Node[@name="root"]/Prp[@name="stop time"]/@value').text);      

     

      aqFileSystem.DeleteFolder(tempFolder, true);  

     

      var cellStyle = "style=\"padding-left:5px; padding-right:5px";

      var errCntCellBg = "; background-color:" + (errC > 0 ?  "#FF704D\"" : "#ACD630\"");

      var wrnCntCellBg = "; background-color:" + (wrnC > 0 ?  "#FFFF75\"" : "#ACD630\"");

     

      var res = "<table border =\"1\">" +

                "<tr><td " + cellStyle + "\">Errors: </td><td " + cellStyle + errCntCellBg + "\">" + errC + "</td></tr>" +     

                "<tr><td " + cellStyle + "\">Warnings: </td><td " + cellStyle +  wrnCntCellBg + "\">" + wrnC + "</td></tr>" +    

                "<tr><td " + cellStyle + "\">Start Time: </td><td " + cellStyle + "\">" + aqConvert.DateTimeToStr(startTime) + "</td></tr>" +    

                "<tr><td " + cellStyle + "\">Stop Time: </td><td " + cellStyle + "\">" + aqConvert.DateTimeToStr(stopTime) + "</td></tr>" +    

                "<tr><td " + cellStyle + "\">Run Time: </td><td " + cellStyle + "\">" + aqConvert.DateTimeToFormatStr(stopTime - startTime, "%H:%M:%S") + "</td></tr>" +

                "</table>";

                  

     

      return res;   

     

    }







    4. Then call the method above and save the HTML output to a variable (see "strTestSummary"). Also save the .mht file to a directory of your choice on the server and store the path in the variable -> this will be the link to the file in the automatic eMail sent later (see below):





    function SaveAndEmailTestResults()

    {

      var FileName, NowValue, strNowValue, strTestSummary;

     

      strTestSummary = GetTestSummary();

     

     

      // Obtain the current date and time

      NowValue=aqDateTime.Now();

     

      // Convert the returned date/time value to string value

      strNowValue = aqConvert.DateTimeToFormatStr(NowValue, "%Y%m%d%H%M%S");

     

      FileName = strLogFilePath + "\\" + strNowValue + ".mht";

      Log.SaveResultsAs(FileName, 2);

     

         

       //Send eMail

       SendCDOEmail(strTestResult, strTestSummary, strAppVersionInfo, FileName);

      

       //re-initialize global test result flag

       strTestResult="";

     

    }











    5. Invoke automatic sending of eMail (see "SendCDOEmail" call above). Make sure to supply the correct input parameters (SMTP credentials etc.) :







    function SendCDOEmail(testResult,resultSummary, versionInfo, file)

    {

      var i, schema, mConfig, mMessage, mSubject, mBody;

     

      var strTestServerIP = "\\\\10.20.30.40";

     

      var strFileLink = aqString.Replace(file,"C:",strTestServerIP);

     

      mBody="Application " + versionInfo + "<br><br>" + resultSummary + "<br>Log file: <a href =\"" + strFileLink + "\">" + strFileLink + "</a>";

      mSubject= "Automated test notification: " + testResult;

      try

      {

        schema = "http://schemas.microsoft.com/cdo/configuration/";

        mConfig = Sys.OleObject("CDO.Configuration");

        mConfig.Fields.Item(schema + "sendusing") = 2; // cdoSendUsingPort

        mConfig.Fields.Item(schema + "smtpserver") = strLogSMTPServer; // SMTP server

        mConfig.Fields.Item(schema + "smtpserverport") = 25; // Port number

        mConfig.Fields.Item(schema + "MailboxURL")= strLogMailBoxUrl;

        //mConfig.Fields.Item(schema + "sendusername") = "user"; // User name (if needed)

        //mConfig.Fields.Item(schema + "sendpassword") = "pwd"; // User password (if needed)

        mConfig.Fields.Update();

     

        mMessage = Sys.OleObject("CDO.Message");

        mMessage.MimeFormatted = true;

        mMessage.Configuration = mConfig;

        mMessage.From = strLogSenderName;

        mMessage.To = strLogRecipientAddress;

        mMessage.Subject = mSubject;

        mMessage.HTMLBody = mBody;

       

       

        mMessage.Send();

       

      }

      catch (exception)

      {

        Log.Error("E-mail cannot be sent", exception.description);

        return false;

      }

      Log.Message("Message to <" + strLogRecipientAddress + "> was successfully sent");

     

     

      return true;

    }





     

    Hope this helps to point you to the right direction.



    Marin