Sending logs via e-mail

The simplest method to send an e-mail in TestComplete is that of BuiltIn.SendMail; however, it is appropriate only in case the mail server requires no authentication.

This is why in the given recipe we will consider a universal method to dispatch logs with the help of Collaboration Data Objects (CDO). The example we use will employ the Gmail mail server.

How to do it...

We need to perform the following steps to send an e-mail:

  1. To begin with, we will write the function which sends the mail. To this end, we will need to correctly fill out all the fields of the CDO object. Configuration is to the following effect:
    function SendEmail(mFrom, mTo, mSubject, mBody, mAttach)
    {
      var schema, mConfig, mMessage;
      schema = "http://schemas.microsoft.com/cdo/configuration/";
      mConfig = Sys.OleObject("CDO.Configuration");
      mConfig.Fields.Item(schema + "sendusing") = 2;
      mConfig.Fields.Item(schema + "smtpserver") = "smtp.googlemail.com";
      mConfig.Fields.Item(schema + "smtpserverport") = 465;
      mConfig.Fields.Item(schema + "smtpauthenticate") = 1;
      mConfig.Fields.Item(schema + "smtpusessl") = true;
      mConfig.Fields.Item(schema + "sendusername") = "[email protected]";
      mConfig.Fields.Item(schema + "sendpassword") = "MY_PASSWORD";
      mConfig.Fields.Update();
    
      mMessage = Sys.OleObject("CDO.Message");
      mMessage.Configuration = mConfig;
      mMessage.From = mFrom;
      mMessage.To = mTo;
      mMessage.Subject = mSubject;
      mMessage.HTMLBody = mBody;
    
      aqString.ListSeparator = ",";
      for(var i = 0; i < aqString.GetListLength(mAttach); i++)
      mMessage.AddAttachment(aqString.GetListItem(mAttach, i));
      mMessage.Send();
    }
  2. Now we will archive the current log with the help of the slPacker object:
    slPacker.PackCurrentTest("c:\testresults.zip");
  3. Hence, we send the resulting archive with the help of the earlier written SendEmail function:
    SendEmail("[email protected]", "[email protected]", "Test results", "Email body", ["c:\testresults.zip"]);

    In the result, from the address of [email protected] to the address of [email protected], a letter will be sent with the archive attached, containing the tests results.

How it works...

In our case, the gmail.com server was used as an example, since it is one of the most frequently used mail services. However, the parameters of each of the servers vary, and this is why one should consult with the system administrator of your network (if you send letters from your corporate mail system).

The filled out object of CDO.Configuration is passed as a Configuration property to the object of CDO.Message. This same object has parameters of the letter preassigned (the address of the sender and the recipient, the theme and the body of the letter, and the files attached), after which the method Send is to be called to CDO.Message.

The method slPacker.PackCurrentTest allows archiving the results of the current test (this is necessary for more convenient manipulations of the attachments, as handling a single file is easier than several).

If all the parameters are signified correctly, the e-mail will be sent.

Naturally, instead of packing up logs, it is also possible to export them into the MHT file and send it via e-mail.

See also

  • To learn how to export logs into MHT format, read the Exporting log to MHT format recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.133.180