如何在TestNG Index.html报告中使用reporter.log()附加失败的测试用例屏幕截图



我使用静态方法截取屏幕截图,并使用reporter.log函数将屏幕截图附加到testNg的index.html报告中。下面是截图代码

public class GenericHelper extends CNLogin {
public static String takeScreenShot(String methodName){
    try {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // C:Users499290AppDataLocalTempscreenshot7520341205731631960.png 
        String FilePath = "C:\Users\499290\Downloads\CNProject1\CNProject\test-output\";
        new File(FilePath);  
        FileUtils.copyFile(scrFile, new File( FilePath +methodName +".jpg") );
        System.out.println("***Placed screen shot in "+scrFile+" ***");
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    return methodName+".jpg";
}

}

我在index.html报告

中使用以下代码附加屏幕截图
 String TakescreenShot =  GenericHelper.takeScreenShot("AddNewPr");
                    Reporter.log("<a href="" + TakescreenShot + ""><p align="left">Add New PR screenshot at " + new Date()+ "</p>");

当测试用例失败时,我不能截取屏幕截图,也不能将屏幕截图附加到报告中。

这是我的测试用例,如果它通过了我的截屏方法将采取截屏,并在报告中附加截屏,但当它失败时不确定如何采取截屏。

public void MultipleServiceDelete() throws InterruptedException {
         driver.findElement(By.id("page:frm:pageB:repeatUpper:0:repeat:0:chkIsDelete")).click();
         Thread.sleep(5000);
         driver.findElement(By.id("page:frm:pageB:btnDeleteMultipleServices")).click();
         String DeleteService =  ScreenShot.takeScreenShot("MultipleServiceDelete");
         Reporter.log("<a href="" + DeleteService + ""><p align="left"> Delete Service  screenshot at " + new Date()+ "</p>");

}

您将需要添加一个TestNG侦听器,它在测试失败时截取屏幕截图。下面是一些从我的Selenium Maven模板中获取的侦听器代码:

package com.lazerycode.selenium.listeners;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import static com.lazerycode.selenium.DriverFactory.getDriver;
public class ScreenshotListener extends TestListenerAdapter {
    private boolean createFile(File screenshot) {
        boolean fileCreated = false;
        if (screenshot.exists()) {
            fileCreated = true;
        } else {
            File parentDirectory = new File(screenshot.getParent());
            if (parentDirectory.exists() || parentDirectory.mkdirs()) {
                try {
                    fileCreated = screenshot.createNewFile();
                } catch (IOException errorCreatingScreenshot) {
                    errorCreatingScreenshot.printStackTrace();
                }
            }
        }
        return fileCreated;
    }
    private void writeScreenshotToFile(WebDriver driver, File screenshot) {
        try {
            FileOutputStream screenshotStream = new FileOutputStream(screenshot);
            screenshotStream.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));
            screenshotStream.close();
        } catch (IOException unableToWriteScreenshot) {
            System.err.println("Unable to write " + screenshot.getAbsolutePath());
            unableToWriteScreenshot.printStackTrace();
        }
    }
    @Override
    public void onTestFailure(ITestResult failingTest) {
        try {
            WebDriver driver = getDriver();
            String screenshotDirectory = System.getProperty("screenshotDirectory");
            String screenshotAbsolutePath = screenshotDirectory + File.separator + System.currentTimeMillis() + "_" + failingTest.getName() + ".png";
            File screenshot = new File(screenshotAbsolutePath);
            if (createFile(screenshot)) {
                try {
                    writeScreenshotToFile(driver, screenshot);
                } catch (ClassCastException weNeedToAugmentOurDriverObject) {
                    writeScreenshotToFile(new Augmenter().augment(driver), screenshot);
                }
                System.out.println("Written screenshot to " + screenshotAbsolutePath);
            } else {
                System.err.println("Unable to create " + screenshotAbsolutePath);
            }
        } catch (Exception ex) {
            System.err.println("Unable to capture screenshot...");
            ex.printStackTrace();
        }
    }
}

您可能最感兴趣的是名为onTestFailure的方法。这是测试失败时将触发的部分。我有一个驱动程序工厂,它提供了我对驱动程序对象的访问,对getDriver的调用是从工厂获取我的驱动程序对象。如果你有一个静态定义的驱动对象,你可以忽略这一行:

WebDriver driver = getDriver();

其他方法只是创建文件并将屏幕截图写入其中的方便方法。显然,你需要稍微调整一下,让它能够获取屏幕截图被写入的位置,并将其传递到你报告的日志中。

我建议让侦听器访问您的Reporter对象,并更改:

System.out.println("Written screenshot to " + screenshotAbsolutePath);

:

Reporter.log("<a href="" + screenshotAbsolutePath + ""><p align="left">Add New PR screenshot at " + new Date()+ "</p>");

在上面的代码中,保存截图的目录是使用一个名为"screenshotDirectory"的系统属性设置的。您要么需要设置他的系统属性,要么将以下行更改为您想要保存屏幕截图的硬编码位置。下面这行:

String screenshotDirectory = System.getProperty("screenshotDirectory");

将需要更改为:

String screenshotDirectory = "/tmp/screenshots";

或者如果你使用windows,像这样:

String screenshotDirectory = "C:\tmp\screenshots";

相关内容

最新更新