【问题标题】:Automation screenshot自动化截图
【发布时间】:2016-04-26 10:16:15
【问题描述】:

我目前正在从事一个自动化项目,我创建了这个简单的程序来报告测试信息并截取屏幕截图。

package ReportTest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

import ReportTest.Utitlity;
public class ReportTest {


ExtentReports report;
ExtentTest logger;
WebDriver driver;


@Test
public void verifyBlogTitle()
{

    report =new ExtentReports("C:\\Users\\reganc3\\desktop\\report\\LearnAutomation.html");


    logger=report.startTest("VerifyBlogTitle");

    driver = new FirefoxDriver();

    driver.manage().window().maximize();

    logger.log(LogStatus.INFO, "Browser started");

    driver.get("http://www.learn-automation.com");

    logger.log(LogStatus.INFO, "Application is up and running");

    String title = driver.getTitle();

    Assert.assertTrue(title.contains("Selenium"));

    logger.log(LogStatus.PASS, "Title Verified");
}

@AfterMethod
public void tearDown(ITestResult result)
{
    if(result.getStatus()==ITestResult.FAILURE)
    {

    String screenshot_path = Utitlity.captureScreenshot(driver, result.getName());
    logger.log(LogStatus.FAIL, "TitleVerification", screenshot_path);

    }

    report.endTest(logger);
    report.flush();

    driver.get("C:\\Users\\reganc3\\desktop\\report\\LearnAutomation.html");
}
}

这是我的实用程序类,它被调用来截取屏幕截图

package ReportTest;



import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import java.util.*;

public class Utitlity {



public static String captureScreenshot(WebDriver driver, String screenshotName)
{
    try
    {
        TakesScreenshot ts=(TakesScreenshot)driver;

        File source=ts.getScreenshotAs(OutputType.FILE);

        String dest = " C:\\Users\\reganc3\\desktop\\ " +screenshotName+ ".png";

        File destination=new File(dest);

        FileUtils.copyFile(source, destination);

        System.out.println("Screenshot taken");

        return dest;

    }
    catch (Exception e)
    {
        System.out.println("Exception while taking screenshot "+e.getMessage());
        return e.getMessage();
    }

}
 }

我收到以下错误

**截图 null 时出现异常

我对 Selenium 和 TestNG 还很陌生,所以我基本上是在找人来阐明这一点,并给我一些正确方向的指示,并了解我的代码实际发生了什么引发此错误。

提前谢谢你。

【问题讨论】:

  • 您确定您在 captureScreenshot 中提供的路径有效
  • 嗨@rajNishKuMar 你的意思是这行吗? String dest = " C:\\Users\\reganc3\\desktop\\ " +screenshotName+ ".png"; " 我按下了那行代码只是指出创建后将保存屏幕截图的位置?
  • 还有一个很棒的视频来帮助你如何使用范围报告youtube.com/watch?v=dBj7pxhZXhY
  • @rajNishKuMar 谢谢。我实际上已经使用该视频来获取我现在拥有的代码,并且我的代码是相同的
  • 好的,那么请确认您在代码中用于保存屏幕截图的路径是真实的,即存在于您的电脑上

标签: java selenium selenium-webdriver automation testng


【解决方案1】:

我目前正在使用这种方法在我的测试中截取屏幕截图:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

 public void takeScreenShot(WebDriver driver, String methodName) {
        File screenshotFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            try {
                FileUtils.copyFile(screenshotFile,new
 File("Screenshots\\"+methodName+" "+GetTimeStampValue()+".png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

还有这个获取时间戳的方法:

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

public  String GetTimeStampValue()throws IOException{
    Calendar cal = Calendar.getInstance();       
    Date time=cal.getTime();
    String timestamp=time.toString();
    String systime=timestamp.replace(":", "-");
    return systime;
}

【讨论】:

    【解决方案2】:

    我为我的代码使用了错误版本的 ExtentReport。

    我使用的代码是 ExtentReport 的最新版本,而我的 maven 依赖项指向的是旧版本。当我将依赖项更新到最新版本时,代码本身运行良好。

    【讨论】:

      【解决方案3】:
      Robot r=new Robot();
      r.keyPress(KeyEvent.VK_PRINTSCREEN);
      r.keyRelease(KeyEvent.VK_PRINTSCREEN);
      

      【讨论】:

        猜你喜欢
        • 2012-08-12
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 2011-06-12
        • 1970-01-01
        • 2022-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多