You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Selenium Java中如何将失败的非测试方法名传入handleErrorLogs?

问题描述

我用Java开发Selenium测试,已定义handleErrorLogs方法,该方法接收执行失败的方法名及其他参数来格式化错误信息。现有测试方法AddFolderTest通过switch case调用多个非测试方法(如clickAddFolder等),需要实现:当这些非测试方法执行失败时,触发handleErrorLogs方法并传入对应方法名等参数。请指导修改下方代码实现该需求。

@Test(priority = 150, dataProvider = "getTestData")
public void AddFolderTest(String testCaseID, String testCaseName, String testCaseScope, String isExecutableFlag, String preconditionFlag, String userNameForTest, String passwordForTest, String actions, String expectedData, String inputData,String successToastMessages,String inputData2,String expectedData2,String inputData3,String inputData4,String inputData5,String expectedData3){
    try {
        test = extent.createTest(testCaseName);
        testMethodName = "AddFolder_Test"+testCaseID;
        testCaseIDForInternalUse = testCaseID;
        if(isProceed) {
            if (preconditionFlag.equalsIgnoreCase("yes")) {
                addPrerequisites(prereqesitesData, testCaseID);
            }
            handleLogin(userNameForTest, passwordForTest);
            waitForAngular();
            navigationPanel.clickReportsTab();
            waitForAngular();
            String[] actionList = getCommaSeperateValuesAsArray(actions);
            for (String singleAction : actionList) {
                System.out.println("---------------------------------------------------------------------------------------------");
                System.out.println("Action :- " + singleAction);
                logger.info("Action is :- " + singleAction);
                String methodNameExecution = "";
                    switch (singleAction) {
                        case "clickAddFolderButton":
                            addFolder.clickAddFolder();
                            break;
                        case "clickButtonActivate":
                            addFolder.clickButtonActivate();
                            break;
                        case "clickDeactivate":
                            addFolder.clickDeactivate();
                            break;
                        }
                }
            }else {
                currentMethodName = new Object(){}.getClass().getEnclosingMethod().getName();
            }
           System.out.println("************************************************************************************************");
            softAssertions.assertAll();
        }catch (Exception e){
            exceptionAssertFail(e);
            currentMethodName = new Object(){}.getClass().getEnclosingMethod().getName();
            handleErrorLogs(e,currentMethodName,testClzname,testCaseIDForInternalUse);
            softAssertions.assertAll();
        }
    }
}

修改方案

方案1:局部try-catch捕获方法异常

直接在每个switch case中,将非测试方法调用包裹在局部try-catch块中,捕获异常后立即调用handleErrorLogs并传入对应方法名,精准定位失败点:

@Test(priority = 150, dataProvider = "getTestData")
public void AddFolderTest(String testCaseID, String testCaseName, String testCaseScope, String isExecutableFlag, String preconditionFlag, String userNameForTest, String passwordForTest, String actions, String expectedData, String inputData,String successToastMessages,String inputData2,String expectedData2,String inputData3,String inputData4,String inputData5,String expectedData3){
    try {
        test = extent.createTest(testCaseName);
        testMethodName = "AddFolder_Test"+testCaseID;
        testCaseIDForInternalUse = testCaseID;
        if(isProceed) {
            if (preconditionFlag.equalsIgnoreCase("yes")) {
                addPrerequisites(prereqesitesData, testCaseID);
            }
            handleLogin(userNameForTest, passwordForTest);
            waitForAngular();
            navigationPanel.clickReportsTab();
            waitForAngular();
            String[] actionList = getCommaSeperateValuesAsArray(actions);
            for (String singleAction : actionList) {
                System.out.println("---------------------------------------------------------------------------------------------");
                System.out.println("Action :- " + singleAction);
                logger.info("Action is :- " + singleAction);
                switch (singleAction) {
                    case "clickAddFolderButton":
                        try {
                            addFolder.clickAddFolder();
                        } catch (Exception e) {
                            exceptionAssertFail(e);
                            handleErrorLogs(e, "clickAddFolder", testClzname, testCaseIDForInternalUse);
                            softAssertions.assertAll();
                        }
                        break;
                    case "clickButtonActivate":
                        try {
                            addFolder.clickButtonActivate();
                        } catch (Exception e) {
                            exceptionAssertFail(e);
                            handleErrorLogs(e, "clickButtonActivate", testClzname, testCaseIDForInternalUse);
                            softAssertions.assertAll();
                        }
                        break;
                    case "clickDeactivate":
                        try {
                            addFolder.clickDeactivate();
                        } catch (Exception e) {
                            exceptionAssertFail(e);
                            handleErrorLogs(e, "clickDeactivate", testClzname, testCaseIDForInternalUse);
                            softAssertions.assertAll();
                        }
                        break;
                }
            }
        } else {
            currentMethodName = new Object(){}.getClass().getEnclosingMethod().getName();
        }
        System.out.println("************************************************************************************************");
        softAssertions.assertAll();
    } catch (Exception e) {
        exceptionAssertFail(e);
        currentMethodName = new Object(){}.getClass().getEnclosingMethod().getName();
        handleErrorLogs(e, currentMethodName, testClzname, testCaseIDForInternalUse);
        softAssertions.assertAll();
    }
}

方案2:封装通用执行方法(减少重复代码)

如果后续有更多非测试方法需要处理,建议封装通用方法统一处理执行与异常逻辑,避免重复编写try-catch:

首先添加通用执行方法:

private void executeAction(Runnable action, String methodName) {
    try {
        action.run();
    } catch (Exception e) {
        exceptionAssertFail(e);
        handleErrorLogs(e, methodName, testClzname, testCaseIDForInternalUse);
        softAssertions.assertAll();
    }
}

然后修改switch部分代码:

switch (singleAction) {
    case "clickAddFolderButton":
        executeAction(() -> addFolder.clickAddFolder(), "clickAddFolder");
        break;
    case "clickButtonActivate":
        executeAction(() -> addFolder.clickButtonActivate(), "clickButtonActivate");
        break;
    case "clickDeactivate":
        executeAction(() -> addFolder.clickDeactivate(), "clickDeactivate");
        break;
}

关键说明

  • 两种方案都能确保非测试方法执行失败时,立即触发handleErrorLogs并传入对应方法名,而非等到外层catch统一处理,定位失败点更精准。
  • 若handleErrorLogs需要方法全限定名或其他自定义参数,可直接调整传入的methodName值。

内容的提问来源于stack exchange,提问作者Thisura Manjitha Samarakoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 16:52:08