2016-10-31 9 views
1

私は簡単なcasperjsスクリプトを書いて、ウェブサイト上でやや複雑なフォームを埋めています。ウェブサイトのHTMLコードはちょっと混乱しているので、スクリプトをテストするたびにページに到達するためのナビゲーションステップを実行したくありません。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> 

実行結果:

C:>started 
<html><head></head><body></body></html> 
ended 

HTML本体内のタグがなくなっているのはなぜ?

答えて

1

すべてが絶対パスで、正常に動作します:

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

実行結果:

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 

できるのは、このようなもtry to specify絶対パス:

file:///C://Full/Path/To/test.html 
FYI
1

、これらの関数を使用して相対ファイル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()) 
}); 
// ... 
関連する問題