Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 即使Testcase失败了,它也会继续运行;onTestSuccess(ITestResult tr)“;ListenerClass的方法_Java_Selenium Rc - Fatal编程技术网

Java 即使Testcase失败了,它也会继续运行;onTestSuccess(ITestResult tr)“;ListenerClass的方法

Java 即使Testcase失败了,它也会继续运行;onTestSuccess(ITestResult tr)“;ListenerClass的方法,java,selenium-rc,Java,Selenium Rc,我在测试代码中添加了一个问题ListenerClass,但即使我的测试失败,它也会进入onTestSuccess(ITestResult tr)方法ListenerClass,每次我通过测试用例,就像我使用selenium.isTextPresent(expectedResult),即使预期结果不存在,它也会进入onTestSuccess(ITestResult tr)。这是什么问题 Login.java package testPackage; import java.io.File; im

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

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

    }

}
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();
    }
}
我的excelsheet截图

在执行测试用例之后。TC2测试用例即将通过,但实际上它是一个失败的测试用例

请检查此结果屏幕截图


请参见控制台中的此处,结果为
False
,但它同时调用
onTestSuccess(ITestResult tr
)方法not
onTestFailure(ITestResult tr)
method

函数
verify
不会使测试失败

如果您想比较两个字符串、URL或任何您需要的内容,请使用
assert
函数使测试失败

例如,如果我正在检查由一系列操作产生的窗口内容,我将
断言()
窗口的存在,然后
验证()
内容


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

请为您的
@test
类提供代码。您在这里有很多工作要做。看看这里,帮我们帮你。