0

当我通过 WebDriver 浏览我们正在测试的视频播放器应用程序时,我正在运行 Browsermob 代理,因此我可以读取我们的 Web 服务调用的标题和内容。具体来说,我想确保当驱动程序单击给定链接时传递了正确的视频文件名。

我想将 BrowserMob 生成的 HAR 对象转换为字符串,以便我可以搜索字符串video_filename.flv或其他内容。代理服务器具有writeTo()将其内容转储到 a file、 anOutputStream或 a 的功能Writer。现在,愚蠢地,我将结果转储到一个文件中,然后将文件重新摄取到一个字符串中并解析它:

    //...lots of WebDriver activity up here with BMP listening
    //...

    //get the HAR data
    Har har = server.getHar();

    //Output HAR to a file
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("C:\\output.txt");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        har.writeTo(fos);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String harString = readFile("C:\\output.txt", StandardCharsets.UTF_8);

    System.out.println(harString);

这行得通。

当我用Writeror尝试同样的事情时OutputStream,甚至在我担心将这些类型中的任何一种转换为字符串之前,我都会抱怨这些是空指针。考虑一下:

    //get the HAR data
    Har har = server.getHar();

    //Output HAR to a Writer
    Writer harWriter = null;
    try {
        har.writeTo(harWriter);
    } catch (IOException e) {
        e.printStackTrace();
    }

返回空指针异常。

同样对于OutputStream. 在我尝试之前应该设置harWriter一些非空值writeTo()吗?我该如何做到这一点?

看,堆栈跟踪:

java.lang.NullPointerException
at org.codehaus.jackson.impl.WriterBasedGenerator._flushBuffer(WriterBasedGenerator.java:1187)
at org.codehaus.jackson.impl.WriterBasedGenerator.close(WriterBasedGenerator.java:837)
at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2004)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1581)
at net.lightbody.bmp.core.har.Har.writeTo(Har.java:30)
at com.videoplayer.CorrectVideosAreSentToThePlayer.teardown(CorrectVideosAreSentToThePlayer.java:152)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
4

1 回答 1

0

是的,如果您尝试写入空值,您当然会得到 NullPointerException!为什么你会认为这会奏效?

你需要写一个StringWriter或类似的东西。那会给你一个字符串。

于 2013-11-12T21:35:24.013 回答