【发布时间】: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