jsdom:运行一个页面的javascript函数

我一直在搞jsdom,我不知道如何从HTML页面运行function。 例如,我有这样一个简单的页面:

<html> <body> Hello </body> <script> function test() { document.write("Bye bye") } </script> </html> 

我想从jsdom执行testfunction。 我怎么去做这个? 我试着简单地调用函数,但节点抱怨说它不存在。

 jsdom.env({ url : "http://localhost:8000/test.html", features : { FetchExternalResources : ['script'], ProcessExternalResources : ['script'] }, done : function (error, window) { test(); // I'd like to do something like this. console.log(window.document.innerHTML); } }); 

显然,正确的方法是使用window.test()和相应的function:

 jsdom.env({ url : "http://localhost:8000/test.html", features : { FetchExternalResources : ['script'], ProcessExternalResources : ['script'] }, done : function (error, window) { window.test(); console.log(window.document.innerHTML); } });