How to listen for a JS page load event using intern

676 views Asked by At

The application under test emits 'page-load' event after every page load and I want to take screenshot after the page has successfully loaded. Tried to listen for this event using Leadfoot's execute method. But it does not seem to work. Can anyone point out if there is a way to successfully listen to events like page-load.

    return remote.get(URL) 
    .execute(function() {
                                        window.addEventListener('page-load',      function() {  
                                            console.log("Page ready");
                                        }, false);
                                    }, [])
    .takeScreenshot().then(function(data) {
                                        recordImage(newAddress, data);
                                    })
1

There are 1 answers

0
Ken Franqueiro On BEST ANSWER

Given that the event fires asynchronously and you want to wait to proceed until the event fires, you should use executeAsync instead of execute. In addition to any arguments passed through, executeAsync adds a callback argument that your code should call when the asynchronous operation completes.

return remote.get(URL) 
.executeAsync(function(done) {
    window.addEventListener('page-load', function() {  
        done();
    }, false);
}, [])
.takeScreenshot().then(function(data) {
    recordImage(newAddress, data);
})