easierbycode
6/17/2012 - 7:15 PM

Jasmine PhantomJS event based exit

Jasmine PhantomJS event based exit

console.log('Loading a web page');
var page = new WebPage();
//This was tricky, this is the way to open LOCAL files
var url = "file://localhost/Users/danmerino/test/teamcity_reporter.html";
phantom.viewportSize = {width: 800, height: 600};
//This is required because PhantomJS sandboxes the website and it does not show up the console messages form that page by default
page.onConsoleMessage = function (msg) {
    console.log(msg);

    if (msg && msg.indexOf("##jasmine.reportRunnerResults") !== -1) {
        phantom.exit();
    }
};
//Open the website
page.open(url, function (status) {
    //Page is loaded!
        if (status !== 'success') {
                console.log('Unable to load the address!');
            } else {
                 //Using a delay to make sure the JavaScript is executed in the browser
               window.setTimeout(function () {
                    page.render("output.png");
                    phantom.exit();
               }, 200);
                }
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Console Reporter Spec</title>
 
    <link rel="stylesheet" href="../ext/jasmine.css" type="text/css" />
 
    <script type="text/javascript" src="../ext/jasmine.js"></script>
    <script type="text/javascript" src="../ext/jasmine-html.js"></script>
    <script type="text/javascript" src="../src/jasmine.teamcity_reporter.js"></script>
</head>
<body>
    <script type="text/javascript">
        describe("Basic Suite", function() {
            it("Should pass a basic truthiness test.", function() {
                expect(true).toEqual(true);
            });
 
            it("Should fail when it hits an inequal statement.", function() {
                expect(1+1).toEqual(3);
            });
        });
 
        describe("Another Suite", function() {
            it("Should pass this test as well.", function() {
                expect(0).toEqual(0);
            });
        });

        var OnCompleteReporter = _.extend(function () {}, jasmine.Reporter);
        OnCompleteReporter.prototype.reportRunnerResults = function () {
            console.log("##jasmine.reportRunnerResults");
        };
 
        jasmineEnv.addReporter(new OnCompleteReporter());
        jasmine.getEnv().addReporter(new jasmine.TeamcityReporter());
        jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
        jasmine.getEnv().execute();
    </script>
</body>
</html>