5

I've been messing around with jsdom and I can't figure out how to run functions from the html page. For example, I've got a simple page like this:

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

And I'd like to execute the test function from jsdom. How do I go about doing this? I've tried simply calling the function, but node complains that it doesn't exist.

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);
    }
});
Ron
  • 1,989
  • 2
  • 17
  • 33

1 Answers1

5

Well apparently the correct way to do this is with window.test(), and the appropriate features:

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