Event listeners & js-test-driver?

2019-09-06 10:55发布

问题:

My JS gets loaded after the DOM is ready. Once the DOM is ready, the JS checks certain aspects of the environment (window dimensions etc.), binds event listeners to specific elements and then, finally, runs.

How do I set this up in js-test-driver? I can use /*:DOC += ... */ to mock my HTML, but I'm not sure how to do that first, then load my script, and have it bind event listeners to elements in it.

Honestly, what I'd rather have is jstestdriver load my html page along with all the listed scripts, and use that as the starting point. Maybe there's a better tool? A plugin I can use?

回答1:

You can use jQuery's load() function to load the HTML page you want to test. In your JSTestDriver test function:

$('html').load('pageToLoad.html', function() {
    // load was performed - run your asserts here now
});

However, since this uses a callback function, you might have to use an Async Test Case.