Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从CasperJS start()读取本地HTML文件_Javascript_Casperjs - Fatal编程技术网

Javascript 从CasperJS start()读取本地HTML文件

Javascript 从CasperJS start()读取本地HTML文件,javascript,casperjs,Javascript,Casperjs,我正在写一个简单的casperjs脚本来填充一个网站上相当复杂的表单。网站的HTML代码有点凌乱,我不想每次测试脚本时都通过导航步骤到达页面 我将表单页面保存为HTML文件,但我甚至无法将测试HTML文件正确加载到casperjs中。以下是代码、文件和结果: var casper = require('casper').create(); casper.start('file://test.html').then(function() { this.echo('started')

我正在写一个简单的casperjs脚本来填充一个网站上相当复杂的表单。网站的HTML代码有点凌乱,我不想每次测试脚本时都通过导航步骤到达页面

我将表单页面保存为HTML文件,但我甚至无法将测试HTML文件正确加载到casperjs中。以下是代码、文件和结果:

var casper = require('casper').create();

casper.start('file://test.html').then(function() {
    this.echo('started')
    this.echo(this.getPageContent())
});

casper.run(function(){
    this.echo('ended');
    casper.done();
});
测试文件:

<html>
    <head>
        <meta charset="utf-8">
        <title>My page</title>
    </head>
    <body>
        <h1 class="page-title">Hello</h1>
        <ul>
            <li>one</li>
            <li>two</li>
            <li>three</li>
        </ul>
       <footer><p>2012 myself</p></footer>
    </body>
</html>

我的页面
你好
  • 一个
  • 两个
2012年我自己

执行结果:

C:>started
<html><head></head><body></body></html>
ended
started
<html><head>
        <meta charset="utf-8">
        <title>My page</title>
    </head>
    <body>
        <h1 class="page-title">Hello</h1>
        <ul>
            <li>one</li>
            <li>two</li>
            <li>three</li>
        </ul>
       <footer><p>2012 myself</p></footer>


</body></html>
ended
C:>已启动
结束了

为什么HTML正文中的标记消失了?

所有操作都正常,具有绝对路径:

var casper = require('casper').create();
casper.start('file:///home/root2/pjs/test.html').then(function() {
    this.echo('started')
    this.echo(this.getPageContent())
});

casper.run(function(){
    this.echo('ended');
    casper.done();
});
执行结果:

C:>started
<html><head></head><body></body></html>
ended
started
<html><head>
        <meta charset="utf-8">
        <title>My page</title>
    </head>
    <body>
        <h1 class="page-title">Hello</h1>
        <ul>
            <li>one</li>
            <li>two</li>
            <li>three</li>
        </ul>
       <footer><p>2012 myself</p></footer>


</body></html>
ended

仅供参考,您可以使用以下函数获取相对文件的绝对文件uri:

function getAbsoluteFilePath(relativePath) {
  return "file:///" + currentDir() + relativePath;
};

// Courtesy https://github.com/casperjs/casperjs/issues/141
function currentDir() {
  var sep = "/";
  var pathParts = fs.absolute(casper.test.currentTestFile).split(sep);
  pathParts.pop();

  return pathParts.join(sep) + sep;
}
要在场景中使用该选项,请使用以下选项:

// ...
casper.start(getAbsoluteFilePath('test.html')).then(function() {
    this.echo('started')
    this.echo(this.getPageContent())
});
// ...

您可以使用fs模块获取工作目录的绝对路径,然后连接协议“file://”和相对路径

例如