Home > Mobile >  Send Extent report email to stackholders after running all the test cases cucumber
Send Extent report email to stackholders after running all the test cases cucumber

Time:10-06

I want to send en email with extent report attachment that i have generated. I am using cucumber.Currently report is generated using latest timestamp with below name D:\DAAutomation1\NewFeature1\output\10062021_071218798\Report_10062021_071218798.html

Now i want to send this dynamically generated report in email.I am trying to send using the below code in SequentialRunnerTest. java class But it is not working

  1. How can i attach a dynamically generated report which stored in a dynamically generated folder

  2. From which location i need to call this code?

    @BeforeClass public static void Setup() {

         if (CustomFormatter.getReportInstance() == null) {
    
             Date d = new Date();
             String today = new SimpleDateFormat(Constants.SCREENSHOT_SDF).format(d);
             String reportName = String.format("Report_%s%s", today, Constants.HTML_EXTENSION);
    
             File dir = new File(today);
             dir = new File(Constants.REPORT_PATH   dir);
             if (!dir.exists()) {
                 dir.mkdir();
                 Variables.reportFolderName = dir;
             }
             reportPath = new File(dir   "/"   reportName);
             File folderPath = new File(dir   "/");
    
             CustomFormatter.initiateCustomFormatter(reportPath, folderPath);
    
             File extentConfig = new File(Constants.CONFIG_FILES_URI   Constants.EXTENT_FILE);
             CustomFormatter.loadConfig(extentConfig);
             CustomFormatter.addSystemInfo("user", System.getProperty("user.name"));
             CustomFormatter.addSystemInfo("os", System.getProperty("os.name"));
             CustomFormatter.addSystemInfo("browser", CONFIG.getProperty("browser"));
             CustomFormatter.addSystemInfo("Tenant", CONFIG.getProperty("application.url"));
    
         } else {
             CustomFormatter.initiateCustomFormatter();
         }
    

    @AfterClass public static void SendEmail() throws EmailException { // Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath(System.getProperty("user.dir") "output/folderPath/" reportPath); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription(" Test Execution Report"); attachment.setName("Automation Test Execution Report");

            // Create the email message
            MultiPartEmail email = new MultiPartEmail();
            email.setHostName("smtp.gmail.com");
            email.setSSLOnConnect(true);
            email.setSmtpPort(465);
            email.setAuthenticator(new DefaultAuthenticator("[email protected]", "xyz@123"));
            email.addTo("[email protected]", "Test");
            email.setFrom("[email protected]", "Me");
            email.setSubject("Automation Test Execution Report");
            email.setMsg("Automation Test Execution Report");
    
            // add the attachment
            email.attach(attachment);
    
            // send the email
            email.send();
      }
    

CodePudding user response:

From which location I need to call this code?

You have to call that under @AfterClass as you want to send report after executing all tests.

@AfterClass
    public static void sendReport()  {
        SendReport sendReport = new SendReport();
        sendReport.triggerMail("Report", "\\NewFeature1\\output\\10062021_071218798\\Report_10062021_071218798.html);
    }

How can I attach a dynamically generated report which stored in a dynamically generated folder

public class SendReport{

public String[] ToAdresses = { "[email protected]"
            ,"[email protected]" 

public void triggerMail(String reportName, String reportPath)
            throws IOException, AddressException, MessagingException {

    Properties sysmProp = System.getProperties();
    sysmProp.put("mail.smtp.starttls.enable", "true");
    sysmProp.put("mail.smtp.host", host);
    sysmProp.put("mail.smtp.user", from);
    sysmProp.put("mail.smtp.password", password);
    sysmProp.put("mail.smtp.port", "587");
    sysmProp.put("mail.smtp.auth", "true");

    /*Create session object*/
    Session session = Session.getInstance(sysmProp, null);

    /*Create MimeMessage object and add recipients */
    MimeMessage message = new MimeMessage(session);

    /* Setting the string value type as address */
            InternetAddress[] recipients = new InternetAddress[ToAdresses.length];

    for (int i = 0; i < ToAdresses.length; i  ) {
            recipients[i] = new InternetAddress(ToAdresses[i]);
        }

    /* Adding the recipients to the message object. */
    for (int j = 0; j < ToAdresses.length; j  ) {
            message.addRecipient(Message.RecipientType.TO, recipients[j]);
        }

     message.setSubject("Test report");
     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText("Body of email.")

    /* Adding the attachment to the mail. */
    File file = new File(System.getProperty("user.dir")   reportPath);
    BodyPart messageBodyPart_2 = new MimeBodyPart();
    DataSource source = new FileDataSource(file.getAbsolutePath());
    messageBodyPart_2.setDataHandler(new DataHandler(source));
    messageBodyPart_2.setFileName("Test_"   reportName   ".html");

    /* Clubbing the subject of mail. */
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    multipart.addBodyPart(messageBodyPart_2);
    message.setContent(multipart);

    /* Triggers mail. */
    Transport.send(message);

  }
}

CodePudding user response:

please write seperate simple java program that should be executed after your cucumber run.

After the complete execution only, you will see the latest report in your target folder. your secondary program should pick the report from the target folder and mail to them.

In My case, I have written separate java program and JAR packed that will do following actions,

  1. Zip screenshot, css and html report from target folder,
  2. Move them to separate folder with current date and time to identify
  3. Then mail the zip folder

My Execution like,

  1. Created a .bat/sh file
  2. added my cucumber execution
  3. added secondary program execution as JAR execution

mvn test -DCucumber.Options="--tags @temp"

java -jar ZippingAndEmailing.jar [reportLocation] [targetlocation] [emailReciptents]

java -jar ZippingAndEmailing.jar target/cucumber Results [email protected]

  • Related