Forum Discussion

tarleaa's avatar
tarleaa
Contributor
14 years ago

Email Subject / Body with passed / failed test

Hello, 


is there any way that when emailing the TC's  logs, in the subject or body message of the email somehow I could indicate automatically whether the test was passed / failed?


Thanks, 


Andrei

7 Replies


  • Hi Andrei,





    You can find a suitable script in the Get current Log Items How To article.





    The modified version that does exactly what you need is below. Posting the message to the log is replaced by sending an e-mail message.







    Sub Test()

      Call SendMail(<ToAddress>, <MailServerNameOrIP>, <FromName>, <FromAddress>, <Subject>, GetLogItems())

    End Sub





    Function GetLogItems()

      Dim tempFolder, xDoc, result

      tempFolder = aqEnvironment.GetEnvironmentVariable("temp") & "\" & GetTickCount() & "\"

      If 0 <> aqFileSystem.CreateFolder(tempFolder) Then

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

        GetLogItems = ""

        Exit Function

      End If

      If Not Log.SaveResultsAs(tempFolder, lsHTML) Then

        Log.Error("Log was not exported to the " & tempFolder & " temp folder")

        GetLogItems = ""

        Exit Function

      End If

      Set  xDoc = Sys.OleObject("MSXML2.DOMDocument.4.0")

      xDoc.load(tempFolder & "root.xml")                                            

      result = LogDataToText(xDoc.childNodes.item(1), 0, "  ")

      Call aqFileSystem.DeleteFolder(tempFolder, True)

      GetLogItems = result   

    End Function





    Function LogDataToText(logData, indentIndex, indentSymbol)

      Dim i, result

      If "LogData" <> logData.nodeName Then

        LogDataToText = ""

        Exit Function

      End If

      result = ""

      For i = 0 To indentIndex - 1

        result = result & indentSymbol

      Next

      result = result & "Name: " & logData.getAttribute("name") & ", status: " & GetTextOfStatus(logData.getAttribute("status")) & vbCrLf

      For i = 0 To logData.childNodes.length - 1

        result = result & LogDataToText(logData.childNodes.item(i), indentIndex + 1, indentSymbol)

      Next

      LogDataToText = result

    End Function





    Function GetTextOfStatus(statusIndex) 

      Select Case statusIndex

        Case "0" GetTextOfStatus = "OK"

        Case "1" GetTextOfStatus = "WARNING"

        Case "2" GetTextOfStatus = "FAILED"

        Case Else GetTextOfStatus = "UNDEFINED"

      End Select

    End Function

  • Hello Alex,

    thanks for the script. However, when I run the entire project or Project Suite, I get the following error:

    "Cannot create file "". The filename or extension is too long."

    I receive the error at the highlighted line of the following script that I use. I call the GetLogItems() function from another file, from withing a SendEmail() function, that is why the current script does not contain the SendEmail function.

    I also attached a screen shot with the error.



    function GetLogItems()

    {

      var tempFolder = aqEnvironment.GetEnvironmentVariable("temp") + "\\" +

                        GetTickCount() + "\\";

     

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

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

        return "";

      }

      if (!Log.SaveResultsAs(tempFolder, lsHTML)) {

        Log.Error("Log was not exported to the " + tempFolder + " temp folder");

        return "";

      }

     

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

      xDoc.load(tempFolder + "root.xml");                                            

      var result = LogDataToText(xDoc.childNodes.item(1), 0, "  ");

        

      aqFileSystem.DeleteFolder(tempFolder, true);

      return result;   

    }

     

    function LogDataToText(logData, indentIndex, indentSymbol)

    {

      if ("LogData" != logData.nodeName) {

        return "";

      }

     

      var result = "";

      for(var i = 0; i < indentIndex; i++) {

        result += indentSymbol;

      }

      result = result + "<br/>" +  "Name: " + logData.getAttribute("name") + ", status: " +

                GetTextOfStatus(logData.getAttribute("status"));

     

      for(var i = 0; i < logData.childNodes.lenght; i++) {

        result += LogDataToText(logData.childNodes.item(i), indentIndex + 1,

                                  indentSymbol);

      }

      return result;

    }

     

    function GetTextOfStatus(statusIndex)

    {

      switch(statusIndex) {

        case "0": return "OK";

        case "2": return "FAILED";

        

        default: return "UNDEFINED";

      }

    }



    Thanks,

    Andrei
  • Hi,



    It looks like tempFolder contains a very long path. Try changing the tempFolder value, for example, "C:\ExportedLog"



    Does this help?
  • Hello,

    One more question. When I run the script it lists every child in the test log.

    It looks something like this:



    Name: DBEDPublic_Critical, status: PASSED

    Name: FFRun, status: PASSED

               Name: Script Test Log [FF_Operations\FFPublic_MemberCookie], status: PASSED

    Name: FF_Close, status: PASSED

               Name: Script Test Log [FF_Operations\FFPublic_Close], status: PASSED

    Name: PackLog_SendEmail_DBEDRegistration, status: PASSED

               Name: Script Test Log [Mail\SendEmail_AtEnd], status: PASSED




    where DBEDPublic_Critical beeing my project name.

    The question is how can I modify this script so that it lists PASSED / FAILED only for the name of the test and not the for the script itself ?



    Basically, I want it to list something like this:



    Name: DBEDPublic_Critical, status: PASSED

    Name: FFRun, status: PASSED

    Name: FF_Close, status: PASSED

    Name: PackLog_SendEmail_DBEDRegistration, status: PASSED




    Thanks,

    Andrei

  • Hi Andrei,





    Modify the following code snippet of the LogDataToText function:







    for(var i = 0; i < logData.childNodes.length; i++) {

        result += LogDataToText(logData.childNodes.item(i), indentIndex + 1, indentSymbol);

    }







    in the following way:







    if (logData.childNodes.length > 2)

      for (var i = 0; i < logData.childNodes.length; i++) {

        result += LogDataToText(logData.childNodes.item(i), indentIndex + 1, indentSymbol);

    }