屏幕截图未在Selenium中捕获

时间:2017-05-23 13:10:14

标签: selenium selenium-webdriver testng extentreports webpage-screenshot

请使用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;        
}

1 个答案:

答案 0 :(得分:0)

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

     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;        
     }