【问题标题】:ExtentReport how to show all the test casses from test class in one raport?范围报告如何在一个报告中显示测试类中的所有测试用例?
【发布时间】:2021-01-17 21:13:02
【问题描述】:

当我从测试类运行所有测试用例时,我只得到一个测试用例结果,而其他两个没有显示在范围报告上。如何在一份报告中获取所有测试用例结果?

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import org.junit.jupiter.api.*;
import org.openqa.selenium.WebDriver;
import pl.me.automation.config.WebDriverType;
import pl.me.automation.page.ContactPage;
import pl.me.automation.page.HomePage;
import pl.me.automation.utils.TestDataReader;
import static org.junit.jupiter.api.Assertions.assertTrue;

我有一个测试类 ContactPageTest:

public class ContactPageTest extends TestDataReader {
    private WebDriver webDriver;
    private HomePage homePage;
    private ExtentHtmlReporter reporter;
    private ExtentReports reports;

@BeforeEach 和 @AfterEach 函数是否可能是我只获得最新执行的测试结果的原因? 我试图为所有测试用例创建基类 @BeforeMethod@AfterMethod @BeforeTest 和@AfterTest,但也没有用。

 @BeforeEach
    public void init() {
        webDriver = WebDriverType.CHROME.create();
        webDriver.get("https://www.store.com");
        homePage = new HomePage(webDriver);
        homePage.clickCookie();
        reporter = new ExtentHtmlReporter("src//main//resources//reports//index.html");
        reports = new ExtentReports();
        reports.attachReporter(reporter);

    }

    @AfterEach
    public void destroy() {
        webDriver.close();
        reporter.flush();
        reports.flush();
    }

我想在一份报告中显示三个测试用例:

    @Test
    public void shouldFillInAndSendForm() {
        reports.createTest("ContactPageTest - Fill in and send form");
        ContactPage contactPage = homePage.clickContact();
        contactPage.enterUserName(contact.getContactPageEnterUserName());
        contactPage.enterUserLastName(contact.getContactPageEnterUserLastName());
        contactPage.selectMessageSubject(contact.getContactPageSelectMessageSubject());
        contactPage.enterEmailAddress(contact.getContactPageEnterEmailAddress());
        contactPage.enterMessage(contact.getContactPageSelectMessageSubject());
        contactPage.clickSendMessageButton();
        assertTrue(contactPage.isContactMessageDisplayed());
    }

    @Test
    public void shouldSendEmptyForm() {
        reports.createTest("ContactPageTest - Send Empty Form");
        ContactPage contactPage = homePage.clickContact();
        contactPage.clickSendMessageButton();
        assertTrue(contactPage.isEmailValidationErrorDisplayed());
        assertTrue(contactPage.isNameValidationError());
        assertTrue(contactPage.isMessageValidationError());
    }

    @Test
    public void shouldSendFormWithIncorrectEmail() {
        reports.createTest("ContactPageTest - Send Form With Incorrect Email");
        ContactPage contactPage = homePage.clickContact();
        contactPage.enterUserName(contact.getContactPageEnterUserName());
        contactPage.enterUserLastName(contact.getContactPageEnterUserLastName());
        contactPage.selectMessageSubject(contact.getContactPageSelectMessageSubject());
        contactPage.enterEmailAddress(contact.getContactPageEnterIncorrectEmailAddress());
        contactPage.enterMessage(contact.getContactPageEnterMessage());
        contactPage.clickSendMessageButton();
        assertTrue(contactPage.isEmailValidationErrorDisplayed());
    }

}

【问题讨论】:

    标签: java selenium extentreports selenium-extent-report


    【解决方案1】:

    这就是我解决这个问题的方法。我添加了@TestInstance(Lifecycle.PER_CLASS)

    @TestInstance(Lifecycle.PER_CLASS)
    public class ContactPageTest extends Forms {
        private WebDriver webDriver;
        private HomePage homePage;
        private ExtentHtmlReporter reporter;
        private ExtentReports reports;
    

    And 比@BeforeAll、@BeforeEach @AfterEach、@AfterAll 注解

       @BeforeEach
        public void init() {
            webDriver = WebDriverType.CHROME.create();
            webDriver.get("https://www.store.com");
            homePage = new HomePage(webDriver);
            homePage.clickCookie();
    
        }
    
        @BeforeAll
        public void TearUpReporter(){
            reporter = new ExtentHtmlReporter("src//main//resources//reports//index.html");
            reports = new ExtentReports();
            reports.attachReporter(reporter);
        }
    
        @AfterEach
        public void destroy() {
            webDriver.quit();
        }
    
        @AfterAll
        public void tearDownReporter(){
            reporter.flush();
            reports.flush();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多