如何将SOFTASSERT屏幕截图附加到魅力报告?

时间:2019-11-04 02:28:50

标签: java selenium testng pom.xml allure

目前,我是一名学习Selenium + Java的手动测试人员。多亏了Stack Overflow的出色解答,当SoftAssert失败时,我设法拍摄了屏幕截图,但现在我很难将这些屏幕截图附加到Allure报告中。有人可以告诉我在代码中添加些什么,以将这些屏幕快照附加到“魅力”报告中吗?非常感谢!

//Override my onAssertFailure:**
public class CustomSoftAssert extends SoftAssert{

     @Override
     public void onAssertFailure(IAssert<?> assertCommand, AssertionError ex)
    {
         WebDriver driver = TestBase.getDriver1();
         Reporting.takeScreenshot(driver);
    }

//takeScreenshot method:
public static void takeScreenshot(WebDriver driver) {

    try 
    {
    TakesScreenshot ts=(TakesScreenshot) driver;
    File source=ts.getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(source, new File("./screenshots/"+System.currentTimeMillis()+".png"));
    System.out.println("Screenshot taken");
    } 
    catch (Exception e)
    {

    System.out.println("Exception while taking screenshot "+e.getMessage());
    } 
}
//onTestFailure method in AllureTestListener**
    @Override
    public void onTestFailure(ITestResult iTestResult) {
        System.out.println("I am in onTestFailure method " + getTestMethodName(iTestResult) + " failed");
        Object testClass = iTestResult.getInstance();
        WebDriver driver = TestBase.getDriver1();

        // Allure ScreenShotRobot and SaveTestLog
        if (driver instanceof WebDriver) {
            System.out.println("Screenshot captured for test case:" + getTestMethodName(iTestResult));
            saveScreenshotPNG(driver);
        }
        // Save a log on allure.
        saveTextLog(getTestMethodName(iTestResult) + " failed and screenshot taken.");      
    }```

0 个答案:

没有答案