即使Testcase失败它也会进入ListenerClass的“onTestSuccess(ITestResult tr)”方法

时间:2015-03-27 12:34:35

标签: java selenium-rc

我在测试代码中添加ListenerClass时出现了一个问题,但即使我的测试失败,也会进入onTestSuccess(ITestResult tr)的{​​{1}}方法,每次我都会通过测试用例,就像如果我使用ListenerClass,即使预期结果不存在,也会进入selenium.isTextPresent(expectedResult)。这是什么问题。

Login.java

onTestSuccess(ITestResult tr)

ListenerClass.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);

    }

}

我的excelsheet屏幕截图

执行测试用例后。 TC2测试用例即将通过但实际上它是一个失败的测试用例  http://i.stack.imgur.com/m3oSE.png

请检查此结果屏幕截图http://imgur.com/oayyLmi

在控制台中看到结果即将到来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(); } } 但是它也在调用False)方法而不是onTestSuccess(ITestResult tr方法

1 个答案:

答案 0 :(得分:0)

功能verify不会使测试失败。

如果您想比较两个字符串,网址或任何您需要使用assert函数来使测试失败。

例如,如果我检查由一系列操作产生的窗口内容,我会assert()存在窗口,然后verify()内容。

这就是为什么您的测试没有失败并且正在执行OnTestSuccess方法的原因。