在testng中设置testfailure会给出正回报

时间:2016-04-04 15:06:10

标签: java selenium testng

如果出现错误,我想将testng中方法的测试结果设置为失败。我发现这行代码应该可以解决问题

Reporter.getCurrentTestResult().setStatus(ITestResult.FAILURE);

但是,如果我引发错误,我仍然不会从我的整个测试运行中恢复失败

===============================================
LoginTesten
Total tests run: 4, Failures: 0, Skips: 0
===============================================

在上面的运行中应该有2次失败,但什么都没有。设置测试结果的规则是在运行测试的不同类中。它设置在一个类别中,我把我的检查页代码放在一起,所以我没有夸张的测试。

设置的代码

public void checkTextOnPage(String text, boolean expected,String errorMessage, ArrayList<WebDriver> browsers, Logger Logger, String map) throws IOException {
        for(WebDriver driver: browsers){
            try {
                Assert.assertEquals(driver.getPageSource().contains(text), expected, errorMessage);
            }catch (AssertionError e){}
            Capabilities dataBrowser = ((RemoteWebDriver)driver).getCapabilities();
            if(driver.getPageSource().contains(text) == expected){
                Logger.info("[" + this.dateFormat.format(this.date) + "]" +" Browser:" + dataBrowser.getBrowserName() + " -- Version:" + dataBrowser.getVersion() + " -- Text : " + text + " => PRESENT");
            }else if (driver.getPageSource().contains(text) != expected){
                Logger.warning("[" + this.dateFormat.format(this.date) + "]" +" Browser:" + dataBrowser.getBrowserName() + " -- Version:" + dataBrowser.getVersion() + " -- Text : " + text + " => NOT PRESENT");
                File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(screenshot, new File(map + "text_" + text + "_NotPresent_" + this.screenshotCount + "_" + dataBrowser.getBrowserName() + ".png"));
                this.screenshotCount++;
                Reporter.getCurrentTestResult().setStatus(ITestResult.FAILURE);
            }
        }
    }

这是我正在运行的测试

public void loginIMWWelcomeNOT() throws InterruptedException, IOException {
    this.getLogin().goToPage(this.getDriverList(), TestConstants.Test_URL);
    this.getBrowserMovement().typeTextElementXpath(".//*[@name='username']", this.getDriverList(), "intix1");
    this.getBrowserMovement().typeTextElementXpath(".//*[@name='password']", this.getDriverList(), "Jasmine11");
    this.getBrowserMovement().clickElementXpath(".//*[@name='logon']", this.getDriverList());
    this.getCheckPage().checkTextOnPage("grazjfnzeofnez", true, "We komen niet terecht op de welkom pagina", this.getDriverList(), this.getLOGGER(), this.getScreenshotMap());
    System.out.println(Reporter.getCurrentTestResult());
}

我从该代码得到的输出是

apr 04, 2016 4:50:37 PM Data.CheckPage checkTextOnPage
WARNING: [2016-04-04 165030] Browser:chrome -- Version:49.0.2623.110 -- Text : grazjfnzeofnez => NOT PRESENT
[TestResult name="" status=FAILURE method=LoginTesten.loginIMWWelcomeNOT()[pri:0, instance:Data.Tests.LoginTesten@1e643faf] output={null}]

所以它正在被设定,但它没有被任何东西所吸引。这是我在其他课程中设置的问题吗?

2 个答案:

答案 0 :(得分:0)

您的测试不应直接与您的报告互动。要在JUnit中指示错误,只需抛出异常或asset in your tests

答案 1 :(得分:0)

@stevedc捕获错误,然后在测试结束时将其作为错误消息在Assert failure中输出。像

这样的东西
public void checkTextOnPage(String text, boolean expected,String errorMessage, ArrayList<WebDriver> browsers, Logger Logger, String map) throws IOException {
    for(WebDriver driver: browsers){
        try {
            Assert.assertEquals(driver.getPageSource().contains(text), expected, errorMessage);
        }catch (AssertionError e){}
        Capabilities dataBrowser = ((RemoteWebDriver)driver).getCapabilities();
        if(driver.getPageSource().contains(text) == expected){
            Logger.info("[" + this.dateFormat.format(this.date) + "]" +" Browser:" + dataBrowser.getBrowserName() + " -- Version:" + dataBrowser.getVersion() + " -- Text : " + text + " => PRESENT");
        }else if (driver.getPageSource().contains(text) != expected){
            Logger.warning("[" + this.dateFormat.format(this.date) + "]" +" Browser:" + dataBrowser.getBrowserName() + " -- Version:" + dataBrowser.getVersion() + " -- Text : " + text + " => NOT PRESENT");
            File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshot, new File(map + "text_" + text + "_NotPresent_" + this.screenshotCount + "_" + dataBrowser.getBrowserName() + ".png"));
            this.screenshotCount++;
            Assert.assertEquals(true, false, e);
        }
    }
}