我正在尝试使用Nightmare.js和Mocha运行示例测试,并且我一直收到上面的错误。这是完整的输出:
Logcat:FATAL EXCEPTION: main
Process: com.nonexistent.rs.guessthenumber, PID: 4388
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nonexistent.rs.guessthenumber/com.nonexistent.rs.guessthenumber.main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185)
at com.nonexistent.rs.guessthenumber.main.<init>(main.java:141)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
07-14 10:17:04.616 6030-6030/com.nonexistent.rs.guessthenumber E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nonexistent.rs.guessthenumber, PID: 6030
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nonexistent.rs.guessthenumber/com.nonexistent.rs.guessthenumber.main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
at com.nonexistent.rs.guessthenumber.main.<init>(main.java:142)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这是我正在运行的代码:
$ mocha nightmare-chai-example.js
Nightmare demo
Start page
1) should show form when loaded
0 passing (716ms)
1 failing
1) Nightmare demo Start page should show form when loaded:
Uncaught TypeError: Cannot read property 'apply' of undefined
at Nightmare.done (/home/user/testing/node_modules/nightmare/lib/nightmare.js:313:14)
at Nightmare.next (/home/user/testing/node_modules/nightmare/lib/nightmare.js:291:35)
at /home/user/testing/node_modules/nightmare/lib/nightmare.js:301:46
at EventEmitter.<anonymous> (/home/user/testing/node_modules/nightmare/lib/ipc.js:93:18)
at ChildProcess.<anonymous> (/home/user/testing/node_modules/nightmare/lib/ipc.js:49:10)
at handleMessage (internal/child_process.js:695:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
来自this gist。
我正在Oracle VM VirtualBox上运行Ubuntu 16.04 LTS。
答案 0 :(得分:3)
.run()
期待一个回调,并且如果没有它,将会失败(没有太大的输出,如你所注意到的)。这是reported和fix has been proposed。
它也可能值得指出.evaluate()
不能按照您提供的要点所描述的方式工作,至少对于版本&gt; 2.x而言。 .evaluate()
方法将尝试在求值函数(第一个参数)之后发送参数作为该函数的参数。
修改it
来电的内部:
new Nightmare()
.goto(url)
.evaluate(function() {
return document.querySelectorAll('form').length;
})
.run(function(err, result){
result.should.equal(1);
done();
});
值得指出.run()
仅供内部使用,并建议deprecation支持承诺 - 比如使用.then()
实现:
new Nightmare()
.goto(url)
.evaluate(function() {
return document.querySelectorAll('form').length;
})
.then(function(result){
result.should.equal(1);
done();
});