0

I have one requirement, I need to read the test data from the excel sheet and after the test case execution I need to write in to excel sheet as PASS/Fail/ Skip.

I have used TestNG framework,Below are sample code/approach Which I have followed but no luck I am always getting result as 1(SUCCESS),even test case fail also.Please some body help me

@AfterSuite
public void write(){
    int result = Reporter.getCurrentTestResult().getStatus();
    if(ITestResult.FAILURE == result){
        System.out.println("AFTER METHOD IN FAILURE:: "+result);
    }else if(ITestResult.SUCCESS == result){
        System.out.println("AFTER METHOD IN SUCCESS:: "+result);
    }
    else if(ITestResult.SKIP == result){
        System.out.println("AFTER METHOD IN SKIP :: "+result);
    }
Output : AFTER METHOD IN SUCCESS:: 1
2
  • The issue seems to be in the function which is passing the status to this method. Aug 5, 2015 at 12:01
  • Hello log_file, do you have any idea about resolving the issue ?
    – QAMember
    Aug 5, 2015 at 13:14

3 Answers 3

2

Slightly different approach, this worked for me

@AfterMethod
public void writeResult(ITestResult result)
{
    try
    {
        if(result.getStatus() == ITestResult.SUCCESS)
        {

            //Do your excel writing stuff here
        }
        else if(result.getStatus() == ITestResult.FAILURE)
        {
            takeScreenshot(dateTimeStamp,driver,methodName);
            System.out.println("Log Message:: @AfterMethod: Method-"+methodName+"- has Failed");
            //Do your excel writing stuff here

        }
        else if(result.getStatus() == ITestResult.SKIP)
        {
            System.out.println("Log Message::@AfterMethod: Method-"+methodName+"- has Skipped");

        }
    }
    catch(Exception e)
    {
        System.out.println("\nLog Message::@AfterMethod: Exception caught");
        e.printStackTrace();
    }

}
3

If you can alter your approach to include a configuration method using @AfterMethod, then you can pass in a paramater for ITestResult, which holds the results for the most recently completed test method. That class has a method called getStatus() that will give you the status of that test.

Here's an example of how to use this:

@AfterMethod
public void fetchMostRecentTestResult(ITestResult result) {

    int status = result.getStatus();

    switch (status) {
        case ITestResult.SUCCESS:
            //do something
            break;
        case ITestResult.FAILURE:
            //do something else
            break;
        case ITestResult.SKIP:
            //and something else
            break;
        default:
            throw new RuntimeException("Invalid status");
    }
}
0

Hi You can use Selenium+jxl+TestNG framework combination and it is possible using FileInputStream+FileOutputStream classes,like below example

Example:

public class Excelutility {

    public String Testcase;
    public WritableSheet writablesh;
    public WritableWorkbook workbookcopy;

@BeforeTest
public void queryParameterization() throws BiffException,IOException,RowsExceededException,WriteException, InterruptedException{

FileInputStream testfile = new FileInputStream("E:\\AppiumTutorials\\Selenium_Practice\\SeleniumYoutube\\Testdata\\TestData.xls");
//Now get Workbook
Workbook wbook = Workbook.getWorkbook(testfile);
//Now get Workbook Sheet
Sheet sheets = wbook.getSheet("Query_data");
int Norows = sheets.getRows();
//Read rows and columns and save it in String Two dimensional array
String inputdata[][] = new String[sheets.getRows()][sheets.getColumns()];
System.out.println("Number of rows present in TestData xls file is -"+Norows);

//For writing the data into excel we will use FileoutputStream class
FileOutputStream testoutput = new FileOutputStream("E:\\AppiumTutorials\\Selenium_Practice\\SeleniumYoutube\\Testdata\\TestData_results.xls");
System.out.println("creating file one");
//To Create writable workbook
workbookcopy = Workbook.createWorkbook(testoutput);
System.out.println("creating file 2");
//To Create Writable sheet in Writable workbook
writablesh = workbookcopy.createSheet("Query_data",0);
System.out.println("creating file 3");

//Using for loop to write all the data to new sheet
    for(int i=0;i<sheets.getRows();i++)
       {
        for(int k=0;k<sheets.getColumns();k++)
        {
         inputdata[i][k] = sheets.getCell(k, i).getContents();
         Label l = new Label(k, i, inputdata[i][k]);
         Label l2 = new Label(4,0,"Results");            
         writablesh.addCell(l);
         writablesh.addCell(l2);
        }
       }            
    }

@AfterTest
public void writeexcels(){
    try {
        workbookcopy.write();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        workbookcopy.close();
    } catch (WriteException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }   
}

Call this utility into another class using extends keyword.

Please read for more here How to write Test Case pass/fail using selenium

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.