Showing posts with label TestNG. Show all posts
Showing posts with label TestNG. Show all posts

Sunday, 28 September 2014

Sending e-mail notification using TestNG , ANT and WebDriver

Sending e-mail notifications for daily report of our Test Suite Execution has become an important business requirements in majorly all the on-going projects.

Email notification can be send easily without any typical Java code but via TestNG and WebDriver only.

We will send the notification using one of the features of TestNG and the code for that is :


<!-- @ Purpose: This function is for sending an auto email after sanity & regression
   run is complete. -->

<target name="send-email">
<mail
    tolist="abc@xyz.com"
    cclist="pqr@tuv.com"
    from="efg@hij.com" subject="Sanity & Regression Suite Automation Report"
    mailhost="smtp.gmail.com" mailport="465" ssl="true"
    user="efg@hij.com" password="*****">
   
    <message>Hi,

 PFA the HTML Report of Automation Sanity & Regression Suite Execution.

 Environment : ${baseURL}

 Regards,
 Automation Team

 *** This is an automatically generated email, please do not reply to this email id. ***
    </message>
<attachments>
 <fileset dir="${outputDir}">
      <include name="index.html" />
      <include name="emailable-report.html" />
 </fileset>
</attachments>
</mail>

<tstamp>
   <format property="send.time" pattern="MM/dd/yyyy hh:mm aa" unit="hour" />
</tstamp>

<echo message="Mail successfully sent at ${send.time}" />
</target>


Create a new target for sending e-mail in your build.xml and call that target name to send notification.

Also you need Java Mail API Jar. Download it from here and Activation Jar from here. Put these jars in the lib folder of ANT.

The Email send notification is configured using TestNG, ANT and WebDriver.

Happy E-mailing..!

Re-Run the failed Test Scripts in WebDriver using TestNG

Re-running the failed Test Cases in WebDriver has become  a Business Requirement now a days.
Because some times a test case fails at first due to network latency, or any other reason.

So, if we want to re-run the failed test cases automatically we need to write some Java code as follows:



public class RerunFailedTestScripts implements IRetryAnalyzer{

 private int count = 0;
   private int maxCount = 1; // Currently Failed Test Cases will run 1 time after failure

   @Override
   public boolean retry(ITestResult result) {
     if (!result.isSuccess()) {
       if (count < maxCount) {
         count++;
         result.setStatus(ITestResult.SUCCESS);
         String message = Thread.currentThread().getName() + ": Error in Method Name: " + result.getName() + " ."+" Hence, Retrying "
             + (maxCount + 1 - count) + " more time";
         System.out.println(message);
         Reporter.log(message);
         return true;
       } else {
         result.setStatus(ITestResult.FAILURE);
       }
     }
     return false;
   }
}


Here, we are using IRetryAnalyzer , Interface of TestNG to implement to be able to have a chance to retry a failed test. Also, you can configure in 'maxCount' that how many times to run the failed Test Cases.

Then, to re-run the test cases which you want to write the following in the Test Script class :


  @Test(groups={"sanity"},alwaysRun=true,priority = 1, retryAnalyzer=RerunFailedTestScripts.class)
 public class OpenGoogle
 {
  public void testOpenGoogle() throws InterruptedException{
   driver.get("https://www.google.co.in/");
   assertTrue(driver.getTitle().contains("Google"));

  }


Here, we are calling the above class that we made in @Test Annotation in the Test Script Class.

And You are done with the Re-Run Failed Test Cases Framework.