Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take the console output of html file into phantomjs

I am running qunit Test using html file in one file and that html file i am running from phantom js.

When I am running html file through browser i am getting output in console but when i am trying to run using phantom js i am not getting the console output in another js file from where i am calling html file.

I am providing both Files: HTML File :

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>JUnit reporter for QUnit</title>
    <link rel="stylesheet" href="qunit.css">
    <script src="qunit.js"></script>
    <script>
        QUnit.config.reorder = false;
   </script>
<script src="qunit-reporter-junit.js"></script>
<script src=" http://requirejs.org/docs/release/2.2.0/minified/require.js"></script>
<script>

    QUnit.jUnitDone(function(data) {
        var console = window.console;
        if (console) {
            console.log(data.xml);
            }           
    });

</script>
<script src="qunit-reporter-junit.test.js"></script>
</head>
  <body>
    <div id="qunit"></div>
</body>
</html>

Js file :

var system = require('system');
var fs = require('fs');
var page = require('webpage').create();

 if (system.args.length === 1) {
    console.log('Pass the path/to/testfile.js as argument to run the test.');
    phantom.exit();
 } else {
   var url = "file:///C:/Users/Admin/Desktop/js/index.html"; // e.g. 'test/unit/tests.html'
console.log("Opening " + url);
   }
page.open(url, function (status) {
console.log("Status: " + status);
if (status === "success") {
    setTimeout(function () {
        var path = 'results.xml';

        var output = page.evaluate(function () {
            // wants to take console output from html page
            .....................................?

        });
        fs.write(path, output, 'w');
        console.log("Wrote JUnit style output of QUnit tests into " + path);
        console.log("Tests finished. Exiting.");
        phantom.exit();
    }, 3000);
} else {
    console.log("Failure opening" + url + ". Exiting.");
    phantom.exit();
}
});

can anyone suggest me how to take the console output from html file ?

Thanks In Advance.

like image 330
Jaydeep Bobade Avatar asked Nov 08 '22 06:11

Jaydeep Bobade


1 Answers

If your test is similar to this example then to get test results you should request the contents of #qunit-testresult element.

var output = page.evaluate(function(){
    return document.getElementById("qunit-testresult").innerText
});
like image 112
Vaviloff Avatar answered Nov 14 '22 21:11

Vaviloff