ローカルでは npm でインストール
$ npm install -g casperjs
$ casperjs --version
1.1.0-beta3
すでにTravisCI運用している gitリポジトリがある前提で簡単なテストを書いてみる
以下テストコードの元ネタは WEB+DB PRESS Vol.80 にあった CasperJS の紹介記事
tests/hoge_test.coffee
casper.test.begin "ほげでGoogle検索", 8, (test) ->
casper.start 'https://google.com', ->
test.comment "Googleトップページ: " + @getCurrentUrl()
test.assertHttpStatus 200
test.assertTitleMatch /^Google$/
test.assertExists 'form[action="/search"]', '検索フォームがある'
@fill 'form[action="/search"]', q:'ほげ', true
casper.then ->
test.comment "検索結果: " + @getCurrentUrl()
test.assertHttpStatus 200
test.assertSelectorHasText 'title', 'ほげ'
test.assertSelectorHasText 'h3.r a', 'ほげ', 'ほげのリンクを含んでいる'
@click 'h3.r a'
casper.then ->
test.comment "最上位の検索結果をクリック: " + @getCurrentUrl()
test.assertHttpStatus 200
test.assertSelectorHasText 'title', 'ほげ'
casper.run ->
test.done()
http://docs.travis-ci.com/user/installing-dependencies/#Installing-Projects-from-Source
にcasperjsの記述あったのでローカルで入れたcasperjsとバージョン合わせたいのでバージョンの記述だけ変えて travis.yml に以下を追記
travis.yml
before_script:
- wget https://github.com/n1k0/casperjs/archive/1.1-beta3.tar.gz -O /tmp/casper.tar.gz
- tar -xvf /tmp/casper.tar.gz
- export PATH=$PATH:$PWD/casperjs-1.1-beta3/bin/
script
- casperjs test tests/hoge_test.coffee
git push して TravisCI でジョブが実行されるのを待つ
実行結果は以下な感じで失敗していますね。
Test file: tests/hoge_test.coffee
# ほげでGoogle検索
# Googleトップページ: https://www.google.co.jp/?gfe_rd=cr&ei=O55QVLjkN8_U8gfk8YHwBw
PASS HTTP status code is: 200
PASS Subject is strictly true
PASS 検索フォームがある
# 検索結果: https://www.google.co.jp/search?hl=ja&source=hp&q=ほげ&gbv=2&oq=&gs_l=
PASS HTTP status code is: 200
PASS Find "ほげ" within the selector "title"
PASS ほげのリンクを含んでいる
# 最上位の検索結果をクリック: http://ja.wikipedia.org/wiki/メタ構文変数
PASS HTTP status code is: 200
FAIL Find "ほげ" within the selector "title"
# type: assertSelectorHasText
# file: tests/hoge_test.coffee:23
# code: test.assertHttpStatus 200
# subject: false
# selector: "title"
# text: "ほげ"
# actualContent: "メタ構文変数 - Wikipedia"
FAIL 8 tests executed in 4.057s, 7 passed, 1 failed, 0 dubious, 0 skipped.
Details for the 1 failed test:
In tests/hoge_test.coffee:23
ほげでGoogle検索
assertSelectorHasText: Find "ほげ" within the selector "title"