【问题标题】:Null Pointer Exception when trying to take a screenshot with selenium [duplicate]尝试使用硒截屏时出现空指针异常[重复]
【发布时间】: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)

J-Unit error message

【问题讨论】:

  • 能否将堆栈跟踪添加为代码?
  • 同上 - 我不清楚空指针是 at screenshot.takeScreenshot()(在这种情况下是因为 screenshot 为空),还是 in takeScreenshot() (其中有许多选项可用于可能为空的事物)。
  • 可能你的screenshot元素是null?请提供堆栈跟踪。
  • @arizzle 这是OSX 上使用的文件路径,所以看起来是正确的。
  • @RemcoW 啊,是的,你是对的,我的错。

标签: java selenium


【解决方案1】:

您正在为您的 Screenshot 类中的 WebDriver 创建一个新引用。这个WebDriver 永远不会被实例化,因此会给你一个NullPointerException。相反,您应该将 WebDriver 实例作为参数传递给方法。

public class Screenshot {

File source;

public void takeScreenshot(WebDriver webDriver) {

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

}

还有测试用例:

@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);
    *******************************


    driver.findElement(By.id("btn_next")).click();
    driver.findElement(By.id("DETAILS_STUDENT_YOU_false")).click();

编辑:或者你可以在Screenshot的构造函数中设置WebDriver

public void Screenshot(WebDriver webDriver){
    this.webDriver = webDriver;
}

【讨论】:

  • 您好,感谢您的快速回复!我仍然得到与您提到的更新相同的空指针...:/
  • @Josh 在这种情况下,我需要查看堆栈跟踪,否则我看不到 null 是什么。 screenshot 可能是 null。您在哪里/如何创建 Screenshot 类的实例?
  • @Josh 从您添加到帖子中的屏幕截图来看,我假设您在测试类的顶部会说:private Screenshot screenshot;。你永远不会实例化这个类。所以你的setUp()你应该添加:screenshot = new Screenshot();
  • 这成功了!!! :) 非常感谢!非常感谢您理解我的问题的帮助和速度!大大的竖起大拇指!!! :D
  • @Josh Glas 你搞定了 :)。如果这解决了您的问题,请随时接受我的回答,这样问题就不会永远保持开放。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 2020-11-13
相关资源
最近更新 更多