我目前正在使用ExtentReport和TestNG来构建基于Page Object Models的框架 . 框架刚刚启动,所以我需要投入很多东西 . 我在另一个包中有Page Object类 . 我在另一个包中有测试用例 . 测试用例通过使用TestNG实现 .

所以,现在我有一个测试用例 . 我使用了ExtentReports来生成HTML报告 . 这就是我使用它的方式:

public class LoggingIn extends Constants{

    //Create Objects for constants, ExtentReports and ExtentTest
    Constants constants = new Constants();
    private static ExtentReports extent;
    private static ExtentTest test;

    //This starts the reports object which runs a method called attachReporter which takes the HTML file has input to write
    @BeforeSuite
    public void runBeforeEverything() {
        extent = new ExtentReports();
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("/Users/nicole/IdeaProjects/SAPFramework/test-output/Extent.html");
        extent.attachReporter(htmlReporter);
    }

@BeforeMethod
    public void runBeforeTest(Method method) {
        test = extent.createTest(method.getName());
    }

@BeforeTest
    public void setUp() {
        driver = cpq.setup();
    }

@AfterMethod
    public void runAfterTest(ITestResult result) throws IOException{
        switch (result.getStatus()) {
            case ITestResult.FAILURE:
                test.fail(result.getThrowable());
                test.fail("Screenshot below: " + test.addScreenCaptureFromPath(takeScreenShot(result.getMethod().getMethodName())));
                break;
            case ITestResult.SKIP:
                test.skip(result.getThrowable());
                break;
            case ITestResult.SUCCESS:
                test.pass("Passed");
                break;
            default:
                break;
        }
        extent.flush();
    }

@AfterSuite
    public void afterSuiteMethod() {
        extent.flush();
    }

@Test
////

@Test ---->This is planned but I would make a class for each test case
///

@Test ----> Same

}

这里有几件事:

  • Constants类具有可重用的方法,如WebDriver等待,或调用驱动程序方法,驱动程序拆卸方法等 .

  • 我最初考虑过使用@Test注释将所有测试用例一个接一个地放入 . 但放弃了这个想法,因为那将是一个糟糕的主意 . 所以现在他们将成为不同的 class .

  • 我想在Constants类中包含ExtentReports相关的东西,它包含所有可重用的变量 . 但由于我的OOP知识不足,我不知道如何在这种情况下改变"private"访问修饰符 .

我想我需要一个我在测试期间调用的ExtentReport内容的Listener类 . 或者我应该将@BeforeSuite,@ ByMethod,@ AfterMethod,@ AfterSuite的每一个东西移动到Constants类中并从那里调用它们到Test Case类中?

喜欢:

@BeforeSuite
    public void startHTMLReporting(){
    runBeforeEverything();
    }

@BeforeMethod
////

@AfterSuite
////

或者它应该是另一种方式?你们对此有什么建议?