LoginSignup
66
61

More than 5 years have passed since last update.

概要

Nightmare

segment.io社の「俺が考えた最強のe2e API」を備えたphantom.jsラッパー。スクレイパーにも使える。

casper.jsみたいなもん。試してみた。

インストール

お使いのプロジェクトで npm install nightmare してください

URLを開く

Nightmare = require 'nightmare'
new Nightmare()
  .goto 'http://localhost:3000'
  .evaluate (-> location.href), (url) -> console.log url
  .run()

.evalueate は第一引数関数でクライアントでevalした結果を、第二引数のコールバックとしてもらってくる。

mochaのテストならこんな感じ

test/nightmare_example.coffee

Nightmare = require 'nightmare'
{ok} = require 'assert'

describe 'open', ->
  it 'should open localhost:3000', (done) ->
    new Nightmare()
      .goto 'http://localhost:3000'
      .evaluate (-> location.href), (url) -> console.log url
      .run()

./node_modules/.bin/mocha --compilers coffee:coffee-script/register --timeout 30000

タイムアウト多めに取っておいたほうがいい。

フォーム埋めてログインするならこんな感じ。

Nightmare = require 'nightmare'
{ok} = require 'assert'

describe 'post', ->
  it 'should login', (done) ->
    new Nightmare()
      .goto 'localhost:3000/login'
      # login
      .type 'input[name=username]', 'username'
      .type 'input[name=password]', 'password'
      .click '.submit'
      .wait()
      .evaluate (-> location.href), (url) ->
        ok url is 'http://localhost:3000/'
      .run()

waitを挟んでページ遷移を待つ必要がある。
.wait('.hoge') でセレクターの出現をまったり、wait(3000) で3000msまったりできる。

66
61
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
66
61