Sending reports to email through selenium webdriver

765 views
Skip to first unread message

Aksh K

unread,
Jan 17, 2022, 8:53:37 AM1/17/22
to testng...@googlegroups.com
Hi Team,

I am able to send the reports to email through selenium but the issue is getting the Previous report instead of current report.

Thanks,
Akshaya

Krishnan Mahadevan

unread,
Jan 17, 2022, 10:15:47 PM1/17/22
to testng...@googlegroups.com
Akshaya 

Not sure what the question is and how it is related to anything in TestNG. Can you please elaborate and share any sample that can elaborate the problem?

Note:

Neither Selenium NOR TestNG for that matter has anything to do with emailing. Selenium is confined to just letting you interacting with a web app via a browser and TestNG is a test runner ( something that helps you with test discovery, execution and result reporting). 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

From: testng...@googlegroups.com <testng...@googlegroups.com> on behalf of Aksh K <akshaya...@gmail.com>
Sent: Monday, January 17, 2022 7:23:19 PM
To: testng...@googlegroups.com <testng...@googlegroups.com>
Subject: [testng-users] Sending reports to email through selenium webdriver
 
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/CAF4EsLFmOEbf85s6%3DGbgqRrJEzLOZTEMpGAsG3yRgY%3DCzKEPdA%40mail.gmail.com.

Aksh K

unread,
Jan 18, 2022, 6:22:23 AM1/18/22
to testng...@googlegroups.com
Hi sir,

I have created a method in a class to send html report through email.
I'm calling that method inside @Aftertest. So that the method should run after all the test methods are executed.
Once all the tests are executed, @Aftertest Method will execute and i will receive the html report.
But the issue is, it is not sending the latest html report. It is sending a previous html report.

Below is the code snippet

DemoTest class
Public static void Demoemail()
{
System.out.println("before report sending");
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D:\\Test\\test-output\\emailable-report.html");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Report QA automation report");
attachment.setName("Report QA");
MultiPartEmail email1 = new MultiPartEmail();
Email email = new SimpleEmail();
email1.setHostName("smtp.gmail.com");
email1.setSmtpPort(465);
email1.setAuthenticator(new DefaultAuthenticator("tes...@gmail.com", "pass23mds"));
email1.setSSLOnConnect(true);

email1.setFrom("tes...@gmail.com");
email1.setSubject("Test Automation report");
email1.setMsg("Sending email through selenium");
email1.addTo("asd...@gmail.com");
email1.attach(attachment);
email1.send();

System.out.println("Report sent");
}

in Test Class
{
@ Test
{}

@AfterTest
{
DemoTest. Demoemail();
}
}







Thanks,
Akshaya





⇜Krishnan Mahadevan⇝

unread,
Jan 18, 2022, 6:30:36 AM1/18/22
to testng-users
Akshaya,

Instead of using a "BeforeTest" annotated method, why not do the following ?

  1. Create a class that implements the below two interfaces from TestNG
    1. IReporter interface
    2. IExecutionListener interface
  2. Store the output directory that gets passed to you via the IReporter interface as a state into your class
  3. Now within your IExecutionListener you can have your email sending logic built.
  4. Refer to the documentation for Service loaders and add your listener as a mandatory listener so that it gets called all the time and doesn't need to be referred to in your suite file. If you want this email sending to be done only when running from Jenkins for e.g., you can add a JVM flag that can control this.
A skeleton implementation of (1) can look like below:
import java.util.List;
import org.testng.IExecutionListener;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.xml.XmlSuite;

public class SampleClass implements IReporter, IExecutionListener {
  
  private String testOutDir;

  @Override
  public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
      String outputDirectory) {
    this.testOutDir = outputDirectory;
  }

  @Override
  public void onExecutionFinish() {
    //Include code here to send an email. The reports will be available in the path that is stored
    // in "this.testOutDir"
  }
}

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

poobathy r

unread,
Jan 20, 2022, 10:18:26 PM1/20/22
to testng...@googlegroups.com
HI 

The issue might happen if you want to send the reports in email(emailablereports.hmtl) I  believe 
but if you send the mail immediately after your tests it will send the previuos report that is because the folder is not refreshed and it pick up the old report.
you have to refresh the folder where you are taking the report.

regards
Poobathy


Reply all
Reply to author
Forward
0 new messages