assaf / zombie

Insanely fast, full-stack, headless browser testing using node.js
http://zombie.js.org/
MIT License
5.65k stars 518 forks source link

When error is thrown during browser.html(), error state gets leaked across browser instances #1136

Open sudowork opened 7 years ago

sudowork commented 7 years ago

When encountering an issue like #1061 during a call to browser.visit(), the call to browser.html() will throw an uncaught exception.

When this occurs, successive calls to browser.html() appear to fail with the same error, even when visiting other sites with different instances of Browser.

Here is a mocha test that demonstrates this issue with zombie@5.0.7:

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

describe('zombie', function() {
  let browser;

  beforeEach(function() {
    console.log('Creating new browser');
    browser = new Browser();
  });

  afterEach(function() {
    console.log('Destroying browser');
    browser.destroy();
  });

  describe('when calling .html()', function() {

    function happyPathTest(done) {
      this.timeout(10000);
      browser.visit('https://google.com', (err) => {
        let errorThrown;
        try {
          browser.html();
        } catch (e) {
          return done(e);
        }
        done();
      });
    }

    it('should not error out in happy path', happyPathTest);

    it('should error out due to missing XMLSerializer', function(done) {
      this.timeout(10000);
      browser.visit('http://www.innovativeproperties.com/', (err) => {
        let errorThrown;
        try {
          browser.html();
        } catch (e) {
          console.error(e);
          errorThrown = e;
        } finally {
          assert(!!errorThrown);
          assert(/XMLSerializer is not defined/.test(errorThrown.message));
          done();
        }
      });
    });

    it('should not error out in happy path (demonstrating leaking state)', happyPathTest);
    it('should not error out in happy path (demonstrating leaking state)', happyPathTest);
  });

});

Here's the output:

  zombie
    when calling .html()
Creating new browser
      ✓ should not error out in happy path (878ms)
Destroying browser
Creating new browser
JQMIGRATE: Migrate is installed, version 1.4.1
ReferenceError: XMLSerializer is not defined
    at .outerHTML (http://www.innovativeproperties.com/wp-content/themes/brandon-premium-wordpress-theme/brandon/js/jquery.swiper.min.js?ver=1.5.0:15:12641)
    at /Users/kevingao/test/node_modules/zombie/lib/index.js:565:20
    at Array.map (native)
    at Browser.html (/Users/kevingao/test/node_modules/zombie/lib/index.js:564:82)
    at browser.visit.e (/Users/kevingao/test/z.js:39:19)
    at done (/Users/kevingao/test/node_modules/zombie/lib/eventloop.js:589:11)
    at Timeout.timeout (/Users/kevingao/test/node_modules/zombie/lib/eventloop.js:601:113)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)
      ✓ should error out due to missing XMLSerializer (5042ms)
Destroying browser
Creating new browser
      1) should not error out in happy path (demonstrating leaking state)
Destroying browser
Creating new browser
      2) should not error out in happy path (demonstrating leaking state)
Destroying browser

  2 passing (7s)
  2 failing

  1) zombie when calling .html() should not error out in happy path (demonstrating leaking state):
     ReferenceError: XMLSerializer is not defined
      at .outerHTML (http://www.innovativeproperties.com/wp-content/themes/brandon-premium-wordpress-theme/brandon/js/jquery.swiper.min.js?ver=1.5.0:15:12641)
      at node_modules/zombie/lib/index.js:565:20
      at Array.map (native)
      at Browser.html (node_modules/zombie/lib/index.js:564:82)
      at browser.visit.e (z.js:24:19)
      at EventLoop.done (node_modules/zombie/lib/eventloop.js:589:11)
      at Immediate.<anonymous> (node_modules/zombie/lib/eventloop.js:688:71)

  2) zombie when calling .html() should not error out in happy path (demonstrating leaking state):
     ReferenceError: XMLSerializer is not defined
      at .outerHTML (http://www.innovativeproperties.com/wp-content/themes/brandon-premium-wordpress-theme/brandon/js/jquery.swiper.min.js?ver=1.5.0:15:12641)
      at node_modules/zombie/lib/index.js:565:20
      at Array.map (native)
      at Browser.html (node_modules/zombie/lib/index.js:564:82)
      at browser.visit.e (z.js:24:19)
      at EventLoop.done (node_modules/zombie/lib/eventloop.js:589:11)
      at Immediate.<anonymous> (node_modules/zombie/lib/eventloop.js:688:71)