1

请在下面找到我使用 ExtentReport 3.0.6 和 selenium 3.4.0 的代码。屏幕截图没有保存,但我没有得到任何例外。它不保存任何屏幕截图。我不确定为什么会发生这种情况,几天前它对我有用。

ExtentHtmlReporter htmlReporter;
ExtentReports extent;
ExtentTest test;
WebDriver driver;


@BeforeTest
public void setUp()
{
    //where we need to generate the report
    htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir")+"/test-output/MyReport.html");
    extent = new ExtentReports();
    extent.attachReporter(htmlReporter);

    // Set our document title, theme etc..
    htmlReporter.config().setDocumentTitle("My Test Report");
    htmlReporter.config().setReportName("Test Report");
    htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
    htmlReporter.config().setTheme(Theme.DARK); 
}

@Test
public void demoReportPass()
{
    test = extent.createTest("demoScreenshotTest");
    System.setProperty("webdriver.chrome.driver","C:\\SeleniumServer\\chromedriver.exe");
    driver=new ChromeDriver();      
    driver.get("https://www.google.com/");
    String title = driver.getTitle();
    Assert.assertEquals(title, "Goo");

}

@AfterMethod
public void getResult(ITestResult result) throws IOException
{
    if(result.getStatus()==ITestResult.FAILURE)
    {
        String screenshotPath = capture(driver, "screenshotname");
        //String screenshotPath = GetScreenshot.captureFullPage(driver, "screenshotname");
        test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + "Test Case failed due to below issues", ExtentColor.RED));
        test.fail(result.getThrowable());
        test.fail("Snapshot below: " + test.addScreenCaptureFromPath(screenshotPath));          
    }               
}

@AfterSuite
public void tearDown()
{
    extent.flush();
    driver.quit();
}

public String capture(WebDriver driver, String screenShotName) throws IOException
{
    TakesScreenshot ts = (TakesScreenshot)driver;
    File source = ts.getScreenshotAs(OutputType.FILE);      
    String dest = "../test-output/"+screenShotName+".png";
    File destination = new File(dest);
    FileUtils.copyFile(source, destination);                          
    return dest;        
}
4

1 回答 1

0

您正在从捕获方法返回 dest (目标路径)。你的 dest 被定义为

     String dest = "../test-output/"+screenShotName+".png"; 

这将返回硬编码的“../test-output/”并且不会将 .. 解析为“当前目录之上的一个目录”

使用 getCanonicalPath() 方法返回此抽象路径名的规范路径名字符串。getCanonicalPath() 方法通常涉及删除冗余名称,例如“.”。和路径名中的“..”,解析符号链接(在 UNIX 平台上),并将驱动器号转换为标准大小写(在 Microsoft Windows 平台上)。

在您的情况下,您可以使用 getCanonicalPath() 返回 dest,如下所示 -

     public String capture(WebDriver driver, String screenShotName) throws IOException
     {
       TakesScreenshot ts = (TakesScreenshot)driver;
       File source = ts.getScreenshotAs(OutputType.FILE);  

       File file = new File("../test-output/"+screenShotName+".png");
       String dest = file.getCanonicalPath();
       File destination = new File(dest);
       FileUtils.copyFile(source, destination);                          
       return dest;        
     } 
于 2020-07-25T14:12:44.397 回答