【问题标题】:How to get the status for each test steps using Java Selenium ? I would like to know whether the test step is pass, fail or skipped [closed]如何使用 Java Selenium 获取每个测试步骤的状态?我想知道测试步骤是通过、失败还是跳过[关闭]
【发布时间】:2019-11-28 11:10:43
【问题描述】:

我需要获取每个步骤的测试状态。我使用 TestListenerAdapter 来获取测试套件和测试用例状态的状态。现在我需要知道使用监听器是否可以获取测试步骤状态。

【问题讨论】:

  • TestListenerAdapter 仅提供跟踪测试用例级别的功能。如果您需要了解测试用例中每个步骤的状态,您需要将每个步骤记录到日志文件中。然后,一旦测试用例失败,您就可以跟踪失败步骤。另一种方法是使用断言使测试因有意义的原因而失败。
  • 解决方案是为每次执行添加日志文件,对吗? @SenalWeerasinghe
  • 只需检查 log4j 日志模块。它有不同种类的滚动策略。您可以为整个测试套件制作一个日志文件并将所有步骤记录到其中。也记录下 TestListnerAdapter 的日志。
  • 另一个问题,如何从监听方法List<Date> date = new ArrayList<Date>(); public void onStart(ITestContext context) { Date SuiteStartTime= context.getStartDate(); System.out.println("Starting Time of the TestSuite"+SuiteStartTime); date.add(SuiteStartTime); }返回值
  • @KogulSelvanathan 不要在 cmets 中发布代码。相反,请更新您的问题,或创建一个新问题。除非两个问题密切相关,否则它们应该放在单独的问题中。

标签: java eclipse selenium-webdriver listener


【解决方案1】:
  • TestListenerAdapter 仅提供跟踪测试用例级别的功能。如果您需要了解测试用例中每个步骤的状态,您需要将每个步骤记录到日志文件中。然后,一旦测试用例失败,您就可以跟踪失败步骤。另一种方法是使用断言使测试因有意义的原因而失败。
  • TestListner 示例
import org.apache.log4j.Logger;
import org.testng.*;


public class TestStatusListener extends TestListenerAdapter {

    private static final Logger logger = Logger.getLogger(TestStatusListener.class);
    private static final TestStatusListener testStatusListener;
    private ITestNGMethod iInvokedMethod;

    public ITestNGMethod getInvokedMethod() {
        return iInvokedMethod;
    }

    public void setInvokedMethod(ITestNGMethod iInvokedMethod) {
        this.iInvokedMethod = iInvokedMethod;
    }

    static {
        testStatusListener = new TestStatusListener();
    }

    public static TestStatusListener getTestStatusListener(){
        return testStatusListener;
    }

    @Override
    public void onStart(ITestContext testContext) {
        super.onStart(testContext);
        logger.info(String.format("TEST SUITE STARTED |{%s}|",testContext.getSuite().getName()));
    }

    @Override
    public void onFinish(ITestContext testContext) {
        super.onFinish(testContext);
        logger.info(String.format("TEST SUITE FINISHED |{%s}|",testContext.getSuite().getName()));
    }

    @Override
    public void onTestSuccess(ITestResult tr) {
        super.onTestSuccess(tr);
        setInvokedMethod(tr.getMethod());
        if (tr.getName() != null) {
            logger.info(String.format("TEST_END_SUCCESSFULLY |{%s},{%s}|",returnMethodName(),
                    tr.getEndMillis() - tr.getStartMillis()));
        }
    }

    @Override
    @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
    public void onTestFailure(ITestResult tr) {
        super.onTestFailure(tr);
        setInvokedMethod(tr.getMethod());
        if (tr.getThrowable() != null)
            logger.error(String.format("Test completed with exception |{%s}|",returnMethodName(),
                    tr.getEndMillis() - tr.getStartMillis()));
    }

    @Override
    public void onTestSkipped(ITestResult tr) {
        super.onTestSkipped(tr);
        setInvokedMethod(tr.getMethod());
        logger.debug(String.format("Test skipped |{%s}|",returnMethodName()));
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
        super.onTestFailedButWithinSuccessPercentage(tr);
    }

    @Override
    public void onConfigurationFailure(ITestResult itr) {
        super.onConfigurationFailure(itr);
    }

    @Override
    public void onConfigurationSkip(ITestResult itr) {
        super.onConfigurationSkip(itr);
    }

    @Override
    public void onConfigurationSuccess(ITestResult itr) {
        super.onConfigurationSuccess(itr);
    }

    @Override
    public void onTestStart(ITestResult result) {
        super.onTestStart(result);
        setInvokedMethod(result.getMethod());
        logger.info(String.format("Test started |{%s}|",returnMethodName()));
    }

    public String returnMethodName() {
        ITestNGMethod method = getInvokedMethod();
        return method.getRealClass().getSimpleName() + "." + method.getMethodName();
    }
}
  • Lo​​g4j.xml 示例
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
                     xmlns:log4j='http://jakarta.apache.org/log4j/'>


    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <appender name="file" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="10MB" />
        <param name="maxBackupIndex" value="10" />
        <param name="file" value="./log/SurvTest.log" />

        <rollingPolicy class="org.apache.log4j.RollingFileAppender">
            <param name="FileNamePattern" value="./log/SurvTest.%d.log"></param>
        </rollingPolicy>

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </root>

</log4j:configuration>

【讨论】:

  • 谢谢,我会照做的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多