1

我遇到了一个问题,如果我运行一个包含 6 个步骤、3 个通过、1 个失败、2 个跳过的测试。在我的范围报告中,它将始终报告为已通过。我正在使用克洛夫。是否有可能我没有正确配置报告?如果是这样,是否有人有解决此问题的建议。

public class MyRunner {


@BeforeClass
public static void initialize(){
    d = new Date();

    ExtentProperties extentProperties = ExtentProperties.INSTANCE;

    extentProperties.setKlovServerUrl("http://localhost");
    extentProperties.setKlovProjectName("Test Project");
    extentProperties.setKlovReportName("Test Report: " + d);

    extentProperties.setMongodbHost("localhost");
    extentProperties.setMongodbPort(27017);
    extentProperties.setMongodbDatabase("klov");

}
}
@AfterClass
public static void teardown(ITestResult result){


    extent.flush();
}

}

这是我的测试,它只是一个打开谷歌登录页面的简单测试,只是为了确保范围报告能够完成我需要的一切

public class Google {
WebDriver driver;

@Given("^that I am on the webpage Google$")
public void thatIAmOnTheWebpageGoogle() throws Throwable {
    System.setProperty("webdriver.chrome.driver","\\\\hiscox.nonprod\\profiles\\Citrix\\XDRedirect\\CaseyG\\Desktop\\chromedriver.exe");

    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.get("https://accounts.google.com/signin/v2/identifier?hl=en&passive=true&continue=https%3A%2F%2Fwww.google.com%2F&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
    driver.manage().window().maximize();
    MyRunner.logger.log(Status.INFO, "Opened Google login page")
    //throw new PendingException();
    //extent.createTest("Step 1").pass("log");
}

@When("^I try to login$")
public void i_try_to_login() throws Throwable {
    // find the username field and enters the username, then clicks next
    WebElement username = driver.findElement(By.xpath("//input[@id='identifierId']"));
    username.sendKeys("********");
    driver.findElement(By.id("identifierNext")).click();

    //finds the password field and enters the password
    WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
    password.sendKeys("**********");
    Assert.assertFalse(true);
    driver.findElement(By.id("passwordNext")).click();
    //throw new PendingException();
    //extent.createTest("Step 2").pass("log");
}

@Then("^I will be successfully logged into Google$")
public void i_will_be_successfully_logged_into_Google() throws Throwable {

    String account = "Google Account: greg casey  \n" + "(klovtest@gmail.com)";
    //WebElement loggedInUser = driver.findElement(By.xpath("//*[@id="gbw"]/div/div/div[2]/div[4]/div[1]"))

    //*[@id="gbw"]/div/div/div[2]/div[4]/div[1]/a

    //extent.createTest("Step 3").pass("log");
    throw new PendingException();
}
}
4

1 回答 1

0

您应该在侦听器类上使用 ITestListener 接口,在该类中创建范围报告的测试 OnTestStart 并执行下面描述的其他操作(我已使用 Thread 进行并行设备测试,因此您可以忽略,休息很有用)

public class Listeners extends Utilities implements ITestListener {

    ExtentReports extent = ExtentReporterTool.getReport();enter code here
    ExtentTest test;
    ThreadLocal<ExtentTest> testObjects = new ThreadLocal<ExtentTest>();
    
@Override
    public void onTestStart(ITestResult result) {
        test = extent.createTest(result.getMethod().getMethodName());
        testObjects.set(test);
    }
    @Override
    public void onTestSuccess(ITestResult result) {
        testObjects.get().log(Status.PASS, "Test Case passed");
    }
    @Override
    public void onTestFailure(ITestResult result) {
        testObjects.get().fail(result.getThrowable());
        WebDriver driver;
        try {
            driver = (WebDriver) result.getTestClass().getRealClass().getSuperclass().getField("driver")
                    .get(result.getInstance());         testObjects.get().addScreenCaptureFromPath(takeScreenshot(result.getMethod().getMethodName(), driver),
                    result.getMethod().getMethodName());
        } catch (IOException | IllegalArgumentException | SecurityException | IllegalAccessException
                | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onTestSkipped(ITestResult result) {

        testObjects.get().log(Status.SKIP, "Test Case skipped");
    }
    @Override
    public void onFinish(ITestContext context) {
        extent.flush();
    }
}
于 2021-08-08T17:54:02.967 回答