Even If Testcase failed It is going in “onTestSuccess(ITestResult tr)” method of ListenerClass

断了今生、忘了曾经 提交于 2019-12-13 04:47:17

问题


I got one issue I added ListenerClass with my test code but even if my test fails, it's going into onTestSuccess(ITestResult tr) method of ListenerClass and everytime I am getting Test case passed, like if I am using selenium.isTextPresent(expectedResult), even if expected result is not present its going in onTestSuccess(ITestResult tr) . What issue it is.

Login.java

package testPackage;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//import junit.framework.Test;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.SeleneseTestCase;

@Listeners(ListenerClass.class)
public class Login extends SeleneseTestCase {

    @BeforeClass
    public void setUp() throws Exception {
        try {
            System.out.println("in setup");
            SeleniumServer seleniumserver = new SeleniumServer();
            seleniumserver.boot();
            seleniumserver.start();
            // String
            // firefoxPath="C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe";

            setUp("http://localhost:8080/",
                    "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");

            // selenium.open("/");
            selenium.windowMaximize();
            selenium.windowFocus();

        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    @DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception {
        System.out.println("in DP1");
        Object[][] retObjArr = getTableArray(
                "test\\Resources\\Data\\dataExcel.xls", "Signup", "Login1");
        return (retObjArr);
    }

    @DataProvider(name = "DP2")
    public Object[][] createData2() throws Exception {
        System.out.println("in createData1");
        Object[][] retObjArr = getTableArray(
                "test\\Resources\\Data\\dataExcel.xls", "Signup", "Login2");
        return (retObjArr);
    }

    @DataProvider(name = "DP3")
    public Object[][] createData3() throws Exception {
        System.out.println("in createData1");
        Object[][] retObjArr = getTableArray(
                "test\\Resources\\Data\\dataExcel.xls", "Signup", "Logout");
        return (retObjArr);
    }

    @Test(dataProvider = "DP1")
    public void testLogin1(String testCases, String userName, String password,
            String expectedResult) throws Exception {

        System.out.println("in testLogin1");
        TestUtility.setTCInContext(testCases);

        selenium.open("Banking/Login.jsp/");
        Thread.sleep(1000);
        selenium.type("id=fname", userName);
        Thread.sleep(1000);
        selenium.type("id=Lname", password);
        Thread.sleep(1000);
        selenium.click("name=sub");
        Thread.sleep(1000);

        verifyTrue(selenium.isTextPresent(expectedResult));

    }

    @Test(dataProvider = "DP2")
    public void testLogin2(String testCases, String userName, String password,
            String expectedResult) throws Exception {
        TestUtility.setTCInContext(testCases);
        System.out.println("in testLogin2");
        System.out.println("TestCases: " + testCases + " userName: " + userName
                + " Password: " + password + " expectedResult: "
                + expectedResult);

        selenium.type("id=fname", userName);
        Thread.sleep(1000);
        selenium.type("id=Lname", password);
        Thread.sleep(1000);
        selenium.click("name=sub");
        Thread.sleep(1000);
        selenium.waitForPageToLoad("30000");
        Thread.sleep(5000);

        verifyEquals(selenium.getLocation(), expectedResult);
    }

    @Test(dataProvider = "DP3")
    public void testLogout(String testCases, String expectedUrl)
            throws Exception {
        TestUtility.setTCInContext(testCases);
        System.out.println("in testLogout");
        System.out.println("TestCases: " + testCases + " expectedUrl: "
                + expectedUrl);

        selenium.click("link=Logout");
        Thread.sleep(1000);
        selenium.waitForPageToLoad("30000");
        Thread.sleep(5000); // boolean res =

        verifyEquals(selenium.getLocation(), expectedUrl);
    }

    @AfterClass
    public void tearDown() {
        selenium.close();
        // this.seleniumSeleniumServer.stop();
    }

    /* this method read the data from excel sheet */
    HashMap<String, Integer> testName = new HashMap<String, Integer>();

    public String[][] getTableArray(String xlFilePath, String sheetName,
            String tableTagName) throws Exception {
        String[][] tabArray = null;
        System.out.println("excel read");
        System.out.println(" xlFilePath:" + xlFilePath + " sheetName: "
                + sheetName + "tableTagName: " + tableTagName);
        Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));

        Sheet sheet = workbook.getSheet(sheetName);
        int startRow, startCol, endRow, endCol, ci, cj;

        Cell tableStart = sheet.findCell(tableTagName);// find table name in
        // excel
        startRow = tableStart.getRow();
        startCol = tableStart.getColumn();

        Cell tableEnd = sheet.findCell(tableTagName, startCol + 1,
                startRow + 1, 100, 64000, false);

        endRow = tableEnd.getRow();
        endCol = tableEnd.getColumn();
        System.out.println("startRow=" + startRow + ", endRow=" + endRow + ", "
                + "startCol=" + startCol + ", endCol=" + endCol);
        tabArray = new String[endRow - startRow - 1][endCol - startCol - 1];
        // e.g tabArray=new String[3][4]
        ci = 0;

        for (int i = startRow + 1; i < endRow; i++, ci++) {
            cj = 0;
            for (int j = startCol + 1; j < endCol; j++, cj++) {
                // System.out.println("ci: "+ci+" cj: "+cj);
                // System.out.println("i: "+i+" j:"+j);
                tabArray[ci][cj] = sheet.getCell(j, i).getContents();

                // System.out.println("tabArray: "+ tabArray[ci][cj]);

                testName.put(tabArray[ci][cj], i);
                testName.put("lastColumn", endCol);

            }
        }
        TestUtility.setTcData(testName);
        return (tabArray);

    }

}

ListenerClass.java

package testPackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.testng.IClass;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class ListenerClass extends TestListenerAdapter {

    @Override
    public void onTestStart(ITestResult tr) {
        log("Test Started....");
    }

    @Override
    public void onTestSuccess(ITestResult tr) {

        log("in onTestSuccess Method and the testcase is  PASSED");
        log("TestUtility '" + TestUtility.getTCInContext());
        // log("getTcData'"+TestUtility.getTcData());

        // System.out.println(TestUtility.getTcData().get(TestUtility.getTCInContext()));
        // TestUtility.getTcData();

        try {
            writeExcel("success");
        } catch (Exception e) {

            e.printStackTrace();
        }

        // This will print the class name in which the method is present
        // log(tr.getTestClass());

        // This will print the priority of the method. // If the priority is
        // not defined it will print the default priority as // 'o'
        // log("Priority of this method is " + tr.getMethod().getPriority());

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

    @Override
    public void onTestFailure(ITestResult tr) {
        log("FAiled");

        log("Test '" + tr.getTestClass() + "' FAILED");
        try {
            writeExcel("fail");
        } catch (Exception e) {

            e.printStackTrace();
        }

        log("Priority of this method is " + tr.getMethod().getPriority());
        System.out.println(".....");
    }

    @Override
    public void onTestSkipped(ITestResult tr) {
        log("Test '" + tr.getName() + "' SKIPPED");
        try {
            writeExcel("skip");
        } catch (Exception e) {

            e.printStackTrace();
        }

        //System.out.println(".....");
    }

    private void log(String methodName) {
        System.out.println(methodName);
    }

    private void log(IClass testClass) {
        System.out.println(testClass);
    }


    public void writeExcel(String type) throws Exception {

        int tcRow = TestUtility.getTcData().get(TestUtility.getTCInContext());
        String strSheetName ="Signup";

        Workbook wbook;
        WritableWorkbook wwbCopy;
        String ExecutedTestCasesSheet;
        WritableSheet shSheet;
        wbook = Workbook.getWorkbook(new File("test\\Resources\\Data\\dataExcel.xls"));
        wwbCopy = Workbook.createWorkbook(new File("test\\Resources\\Data\\dataExcel.xls"),
                wbook);
        shSheet = wwbCopy.getSheet(0);
        WritableSheet wshTemp = wwbCopy.getSheet(strSheetName);
        if (type == "success") {
            Label labTemp = new Label(1, tcRow, "Passed");
            wshTemp.addCell(labTemp);
        } else if (type == "fail") {
            Label labTemp = new Label(1, tcRow, "Failed");
            wshTemp.addCell(labTemp);
        } else {
            Label labTemp = new Label(1, tcRow, "Skipped");
            wshTemp.addCell(labTemp);
        }

        /*try {
            wshTemp.addCell(labTemp);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        wwbCopy.write();
        wwbCopy.close();
    }
}

My excelsheet screenshoot

After executed test cases. That TC2 testcase is coming passed but actually it is a failed testcase http://i.stack.imgur.com/m3oSE.png

Please check this result screenshot http://imgur.com/oayyLmi

See here in console the result is coming False but then also it is calling onTestSuccess(ITestResult tr) method not onTestFailure(ITestResult tr) method


回答1:


Function verify won't make a test fail.

If you want to compare two strings, urls or whatever you need to use assert function to make test fail.

For instance, if I'm checking the contents of a window resulting from a series of actions, I would assert() the presence of the window, and then verify() the contents.

That's why your test doesn't fail and OnTestSuccess method is being executed.



来源:https://stackoverflow.com/questions/29300800/even-if-testcase-failed-it-is-going-in-ontestsuccessitestresult-tr-method-of

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!