【发布时间】:2016-06-28 14:25:18
【问题描述】:
我写了一个 Java 类,它使用 selenium webdriver 快速通过网站并测试各种功能。我还编写了一个单独的类,用于执行 takeScreenshot() 方法。一旦测试命中执行屏幕截图方法的代码,浏览器就会关闭,J-Unit 测试失败并指向我调用 takeScreenshot() 方法的行。这是一个空指针异常,但我不知道出了什么问题。我已经阅读了很多文章,但找不到答案。我已经阅读了这里的所有帖子,这些帖子确定了如何使用 selenium 截屏......代码如下:
**** 截图类****
public class Screenshot {
private WebDriver webDriver;
File source;
public void takeScreenshot() {
try {
source = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File ("/Users/joshuadunn/Desktop/claimsScreenShot.png"));
System.out.println("Screenshot Taken!!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
然后我创建一个 Screenshot 对象并执行它的 takeScreenshot() 方法,如下所示:
@Test
public void testClaimsTestToCalcNode() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("becsStartCalculator")).click();
driver.findElement(By.id("MARITAL_STATUS_false")).click();
driver.findElement(By.id("btn_next")).click();
driver.findElement(By.id("HOME_ABROAD_false")).click();
*** This is where the null pointer is ***
*******************************
screenshot.takeScreenshot();
*******************************
driver.findElement(By.id("btn_next")).click();
driver.findElement(By.id("DETAILS_STUDENT_YOU_false")).click();
希望你能理解我的问题!
EDIT **** - 这不是重复的。我完全知道空指针是什么以及如何修复它。显然,我的代码中的某些内容是 null,但我不知道是什么...下面的屏幕截图是我得到的唯一错误(stacktrace)
【问题讨论】:
-
能否将堆栈跟踪添加为代码?
-
同上 - 我不清楚空指针是 at
screenshot.takeScreenshot()(在这种情况下是因为screenshot为空),还是 intakeScreenshot()(其中有许多选项可用于可能为空的事物)。 -
可能你的
screenshot元素是null?请提供堆栈跟踪。 -
@arizzle 这是
OSX上使用的文件路径,所以看起来是正确的。 -
@RemcoW 啊,是的,你是对的,我的错。