如何将HTML文件的控制台输出带入PhantomJS。

5

我正在使用一个HTML文件运行qunit测试,而且我是从PhantomJS中运行那个HTML文件。

当我通过浏览器运行HTML文件时,控制台会输出结果,但是当我尝试使用PhantomJS运行时,从我调用HTML文件的另一个js文件中,我没有得到控制台输出。

我提供两个文件:

HTML文件:
<!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文件:

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();
}
});

有谁能建议我如何从HTML文件中获取控制台输出?

先行致谢。

1个回答

1
如果您的测试类似于此示例,那么要获取测试结果,您应该请求#qunit-testresult元素的内容。
var output = page.evaluate(function(){
    return document.getElementById("qunit-testresult").innerText
});

是的,明白了。需要在 HTML 页面中声明 ID 部分。 将该变量设置为该 ID 部分。 并在 js 文件中获取该部分的值。 对的...?? - Jaydeep Bobade
谢谢,它正在工作。 - Jaydeep Bobade

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接