zombie icon indicating copy to clipboard operation
zombie copied to clipboard

JavaScript errors from onclick handler not shown

Open msridhar opened this issue 11 years ago • 5 comments

It seems that JavaScript errors from code executed via an onclick handler are not collected in the browser.errors array. Here is a sample input:

<html><head></head><body>
<script>function foo() { 
  console.log("hello"); 
  throw new Error("uh oh"); }
</script>
<a onclick="javascript:foo()" id="link1">Link 1</a>
</body></html>

Here is code to load the file and click the link:

var Browser = require('zombie');
var assert = require('assert');

var browser = new Browser();

browser.debug = true;
browser.silent = false;
browser.visit("http://localhost:8181/foo.html", function() {
    browser.clickLink('#link1', function(err) {
        console.log(browser.errors);
        console.log(browser.window.console.output);
    });
});

When I run the above, I get the following output, which does not include any information about the error:

Zombie: Opened window http://localhost:8181/foo.html 
Zombie: GET http://localhost:8181/foo.html => 200
Zombie: Loaded document http://localhost:8181/foo.html
hello
[]
undefined
Zombie: Event loop is empty

If I change my HTML input to invoke foo() directly within the inline script:

<html><head></head><body>
<script>function foo() { 
  console.log("hello"); 
  throw new Error("uh oh"); }
foo();
</script>
</body></html>

Now I see a stack trace for the error, as expected:

Zombie: Opened window http://localhost:8181/foo.html 
Zombie: GET http://localhost:8181/foo.html => 200
Zombie: Loaded document http://localhost:8181/foo.html
hello
uh oh Error: uh oh
    at foo (http://localhost:8181/foo.html:script:3:9)
    at http://localhost:8181/foo.html:script:4:1
    at Contextify.sandbox.run (/Users/m.sridharan/git-repos/zombie-test/node_modules/zombie/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24)
    at DOMWindow.window._evaluate (/Users/m.sridharan/git-repos/zombie-test/node_modules/zombie/lib/zombie/window.js:188:25)
    at Object.HTML.languageProcessors.javascript (/Users/m.sridharan/git-repos/zombie-test/node_modules/zombie/lib/zombie/scripts.js:23:21)
    at define.proto._eval (/Users/m.sridharan/git-repos/zombie-test/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:1480:47)
    at executeInlineScript (/Users/m.sridharan/git-repos/zombie-test/node_modules/zombie/lib/zombie/scripts.js:54:22)
...

If there is some workaround I can use to capture errors thrown by scripts invoked via an onclick handler, or if I have misunderstood the APIs, please let me know. Thanks!

msridhar avatar Feb 18 '14 23:02 msridhar

Have you tried to output err variable content in your clickLink callback?

aik099 avatar Feb 19 '14 08:02 aik099

@aik099 I just tried that; err is undefined.

msridhar avatar Feb 19 '14 18:02 msridhar

Digging a bit deeper, the onclick handler script seems to be executed via this code in jsdom:

https://github.com/tmpvar/jsdom/blob/0.8.11/lib/jsdom/level2/events.js?source=cc#L196

The catch block at line 198 is reached, but e is undefined, so there's no stack available. Still, I see an error object being attached to the corresponding anchor node in the DOM and its enclosing document. Then, control returns to the invoking code in window.coffee:

https://github.com/assaf/zombie/blob/master/src/zombie/window.coffee#L450

Here, I don't see the catch block at line 455 being executed, just the finally block at line 457. So, no error is emitted. I'm not sure now if this is a bug in zombie or in jsdom.

msridhar avatar Feb 19 '14 19:02 msridhar

I'm having a similar issue. The following code worked in 2.5.1 (now I'm using version 3.1.1). Any error passed to the next function or some assertion using should.js would fail the test:


browser.resources.addHandler(function(request, response, next) {

    //this is checked but don't throw any error
    response.statusCode.should.be.equal(200);
    //this also
    next(new Error("not 200"))

});

browser.pressButton("#submit",function(err){
    //err is undefined
    //browser.errors is empty
});

adrianocola avatar Mar 28 '15 15:03 adrianocola

@assaf Why is this labeled a feature request? It is a bug.

How can I get debug info from my click handlers during a zombie test, when any error thrown is just caught?

TerjeBr avatar Nov 22 '17 15:11 TerjeBr