无法使用onTestFailure使用TestNG进行ScreenShot

时间:2018-02-02 02:00:46

标签: java selenium selenium-webdriver testng

我的听众类代码

package com.test.automation.uiAuotmation.customListner;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;
import com.test.automation.uiAuotmation.testBase.TestBase;

public class Listener extends TestBase implements ITestListener {

public void onFinish(ITestContext arg0) {
    // TODO Auto-generated method stub

}

public void onStart(ITestContext arg0) {
    // TODO Auto-generated method stub

}

public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
    // TODO Auto-generated method stub

}

public void onTestFailure(ITestResult result) {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yy_hh_mm_ss");

    String methodName = result.getName();

    if (!result.isSuccess()) {

        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        String reportDirectory = new File(System.getProperty("user.dir")).getAbsolutePath()
                + "/src/main/java/com/test/automation/uiAutomation/";

        File destFile = new File((String) reportDirectory + "/failure_screenshots/" + methodName + "_"
                + formater.format(calendar.getTime()) + ".png");

        try {
            FileUtils.copyFile(scrFile, destFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Reporter.log("<a href='" + destFile.getAbsolutePath() + "'> <img src='" + destFile.getAbsolutePath()
                + "' height='100' width='100'/> </a>");

    }

}

private void File(String property) {
    // TODO Auto-generated method stub

}

public void onTestSkipped(ITestResult arg0) {
    // TODO Auto-generated method stub

}

public void onTestStart(ITestResult arg0) {
    // TODO Auto-generated method stub

}

public void onTestSuccess(ITestResult arg0) {
    // TODO Auto-generated method stub

}

}

我的测试基本代码

package com.test.automation.uiAuotmation.testBase;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.test.automation.uiAuotmation.excelReader.Excel_Reader;
import org.apache.log4j.Logger;

public class TestBase {

public static final Logger log = Logger.getLogger(TestBase.class.getName());

public static WebDriver driver;
public String Browser = "Firefox";
public String url = "http://automationpractice.com/index.php";
Excel_Reader excel;

public void init() {
    selectBrowser(Browser);
    selectURL(url);
    String log4jConfPath = "log4j.properties";
    PropertyConfigurator.configure(log4jConfPath);
}

public void selectBrowser(String Browser) {
    if (Browser.equalsIgnoreCase("Firefox")) {
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/Drivers/geckodriver.exe");
        log.info("Creating object of Browser" + Browser);
        driver = new FirefoxDriver();
    }

    else if (Browser.equalsIgnoreCase("chrome")) {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/Drivers/chromedriver.exe");
        driver = new ChromeDriver();
    }
}

public void selectURL(String url) {
    log.info("Navigating to" + url);
    driver.get(url);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }

我的TestCase代码

package com.test.automation.uiAuotmation.homepage;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.test.automation.uiAuotmation.testBase.TestBase;
import com.test.automation.uiAuotmation.uiActions.HomePage;

public class TC002_VerifyRegistration extends TestBase {

HomePage homepage;

@BeforeTest
public void setUp() {
    init();
}

@Test
public void verifyRegistration() {
    homepage = new HomePage(driver);
    homepage.register("dig101@gmail.com", "fname", "lname", "digi11", "fname", "lname", "dell", "manyata park",
            "Florida", "00000", "12345", "aliasaddress");
    Assert.assertEquals("Welcome to your account. Here you can manage all of your personal information and orders.",
            homepage.registrationSuccess());
}
@AfterTest
public void endTest() {
    //driver.quit();
       }
    }

我故意通过更改对象标识来使测试用例失败。正如预期的那样,我的测试用例失败了(No SuchElementException)。但是,在测试用例失败时,我的TestNg无法拍摄屏幕截图。我无法弄清楚错误在哪里。 您可以在上面找到ITestListener,My Testbase Code和My Testcases的代码。

1 个答案:

答案 0 :(得分:0)

我不确定您的侦听器类是否已正确设置以处理事件。请通过以下链接进行设置 TestNg Listeners

之后在侦听器的onTestFailure函数中放入一个调试点,并检查控件是否到达该点。

另一个在失败时捕获快照的是将快照捕获逻辑放在@AfterTest方法中并将其设置为始终运行,即

@AfterTest(alwaysRun = true)
public void endTest() {
    //snapshot logic
}