Skip to content

Instantly share code, notes, and snippets.

@rike422
Created October 22, 2013 07:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rike422/7096675 to your computer and use it in GitHub Desktop.
Save rike422/7096675 to your computer and use it in GitHub Desktop.
casper.js使い方

casper.jsとは

CasperJS is an open source navigation scripting & testing utility written in Javascript for the PhantomJS WebKit headless browser and SlimerJS (Gecko). It eases the process of defining a full navigation scenario and provides useful high-level functions, methods & syntactic sugar for doing common tasks such as:

参考資料

コマンド

  • -pre: 全ファイルの実行前に一度だけ実行されるスクリプトを指定。-postだと全ファイルの実行後となる
  • —includes: 各ファイルの実行前に毎回実行されるスクリプトを指定
  • —direct: 標準出力に直接出力する
  • —fail-fast: テストケースがひとつでもfailした時点で実行を終了する
  • —log-level: 出力する情報を選べる。error, warning, info, debugから一つ指定し、debugが最も情報量が多い
  • —xunit: テスト実行ディレクトリに、テスト結果のXMLファイルを出力する。Jenkinsに渡して利用
casperjs test test/suite/ --direct --log-level=debug --fail-fast --pre=test/common/casper_pre.coffee --includes=test/common/casper_inc.coffee --xunit=log.xml

上記だと
起動直後の一回のみtest/common/casper_pre.coffeeを実行
テストスクリプト実行前にtest/common/casper_inc.coffeeを実行
test/suite/配下のスクリプトをテストコードとして実行する。

#. casper_pre.coffee
#. casper_inc.coffee
#. test/suite/hoge1.coffee
#. casper_inc.coffee
#. test/suite/hoge2.coffee
#. casper_inc.coffee
#. test/suite/hoge3.coffee

見たいな感じ

tips

cookieでのloginセッションを持ち回したいとき

ログイン処理のcasper.runでファイルに書き出す。


casper.run →
fs = require ‘fs’
cookies = JSON.stringify casper.page.cookies
fs.write ‘./tmp/casper_cookies.txt’, cookies, ‘w’
casper.test.done()

それをスクリプトファイルごとに読み取りcasper.page.cookiesにJSON.parseして突っ込む
更新されるなら都度書き出す
※ —includesで指定したファイル内で読み取り関数つくって突っ込むとよい?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment