Friday, January 31, 2014

Selenium: TestNG Error Reporting using IReporter (without halting)

 TestNG Error Reporting using IReporter (without halting)


1.Create a package inside your project.Ex:Reporter
2. Create a new Class CustomReporter  inside the package.
3. Copy the below code inside the Class as in the screenshot (Code below).


4. Goto your testng.xml file and add the following code as in the screenshot : 

5. My SampleTest file containing all the test Cases look like this .
6 . The result will look like this :







Code for - CustomReporter :

import java.util.List;
import java.util.Map;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.xml.XmlSuite;

public class CustomReporter implements IReporter {

  @Override
  public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
  //Iterating over each suite included in the test
   for (ISuite suite : suites) {
     //Following code gets the suite name
     String suiteName = suite.getName();
     //Getting the results for the said suite
     Map<String, ISuiteResult> suiteResults = suite.getResults();
     for (ISuiteResult sr : suiteResults.values()) {
       ITestContext tc = sr.getTestContext();
       System.out.println("Passed tests for suite '" + suiteName +"' is:" + tc.getPassedTests().getAllResults().size());
       System.out.println("Failed tests for suite '" + suiteName +"' is:" + tc.getFailedTests().getAllResults().size());
       System.out.println("Skipped tests for suite '" + suiteName +"' is:" + tc.getSkippedTests().getAllResults().size());
     }
   }
  }
}

Selenium : TestNG error Reporting using IListener (without halt)

Selenium : TestNG error Reporting using IListener (without halt)


  1. Create a package in your project     Example: logger
  2. Create  a Class CustomLogging inside your package.
  3. Paste the below code as mentioned in the screenshot (code below) :

4.  Open your Testng.xml and add the following code as mentioned in the screen shot:

5. Screen shot of the "SampleTest" which contains my test cases.
6. Result when i run the code.



Code Here - CustomLogging :

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;


public class CustomLogging implements ITestListener{
  
  @Override //Called when the test-method execution starts  
  public void onTestStart(ITestResult result) {
    System.out.println("Test method started: "+ result.getName()+ " and time is: "+getCurrentTime());    
  }

  @Override //Called when the test-method execution is a success
  public void onTestSuccess(ITestResult result) {
    System.out.println("Test method success: "+ result.getName()+ " and time is: "+getCurrentTime());   
  }
   
  @Override //Called when the test-method execution fails
  public void onTestFailure(ITestResult result) {
    System.out.println("Test method failed: "+ result.getName()+ " and time is: "+getCurrentTime());   
  }

  @Override //Called when the test-method is skipped
  public void onTestSkipped(ITestResult result) {
    System.out.println("Test method skipped: "+ result.getName()+ " and time is: "+getCurrentTime());    
  }

  @Override //Called when the test-method fails within success percentage
  public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    // Leaving blank   
  }

  @Override  //Called when the test in xml suite starts
  public void onStart(ITestContext context) {
    System.out.println("Test in a suite started: "+ context.getName()+ " and time is: "+getCurrentTime());   
  }
  
  @Override //Called when the test in xml suite finishes
  public void onFinish(ITestContext context) {
    System.out.println("Test in a suite finished: "+ context.getName()+ " and time is: "+getCurrentTime());   
  }
  
  //Returns the current time when the method is called
  public String getCurrentTime(){
    DateFormat dateFormat = 
        new SimpleDateFormat("HH:mm:ss:SSS");
    Date dt = new Date();
    return dateFormat.format(dt);    
  }
}





Java : @ Override

 Java : @ Override


The main reason @Override was created was to deal with simple (but nasty) typographical errors. For example, a method mistakenly declared as :  

public int hashcode(){
...



is in fact not an override - the method name has all lower case letters, so it doesn't exactly match the name of the hashCode() method. 

 It will however compile perfectly well. Such an error is easy to make, and difficult to catch, which is a dangerous combination. Using the @Override annotation prevents you from making such errors. 

http://www.javapractices.com/topic/TopicAction.do?Id=223

Thursday, January 30, 2014

Selenium : Screenshot in TestNG Report

Screenshot in TestNG Report





Code Here :


public class ff extends SeleneseTestBase {
public WebDriver driver;
StringBuffer verificationErrors=new StringBuffer();

@Test
public  void test2() throws InterruptedException {

     try{
    driver.findElement(By.xpath("//*[@id='containerrrr']/div[2]/h2"));
     }catch(Throwable  t)
     {
    verificationErrors.append(t.getMessage());
    Reporter.log("object not found"+"<br>");      
    //Take screen shot and give path in the report
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("C:\\1.jpg"));
    Reporter.log("<a href=\"" +"file:///C:/1.jpg" + "\">Screenshot</a>"); // give path in the report
     }
//System.out.println(driver.findElement(By.id("gbqfba")).isDisplayed());
     
     clearVerificationErrors();
if (!"".equals(verificationErrors.toString()))
fail(verificationErrors.toString());
}
}

Selenium : Report Error in TestNG without Halting the Execution

 Report Error in TestNG without Halting the Execution

 

 

Note:

  1. Extend your class with "SeleneseTestBase"
  2. Declare a member varibale of type "StringBuffer".
Ex: 
Class abc extends SeleneseTestBase{

StringBuffer verificationErrors=new StringBuffer();

@Test
----------
----------
----------


}

 Code Here :

@Test
    public  void test1() throws InterruptedException {
     try{
         driver.findElement(By.xpath("//*[@id='containerrrr']/div[2]/h2"));
     }catch(Throwable  t)
     {
         verificationErrors.append(t.getMessage());
         Reporter.log("test1: object not found"+"<br>");
     }
     String fail_reason=verificationErrors.toString();
     clearVerificationErrors();
     if (!"".equals(fail_reason))
         fail(fail_reason);  
    }