LoginSignup
78
78

More than 5 years have passed since last update.

PhantomJS のラッパー Nightmare を試してみる

Posted at

話題らしいので Nightmare を弄ってみる

インストール

npm install nightmare

サンプルコード (+ キャプチャ)

公式のサンプルに画面キャプチャを付け足してみた

Nightmare

example_nightmare.js
var Nightmare = require('nightmare');

new Nightmare()
  .goto('http://yahoo.com')
  .type('input[title="Search"]', 'github nightmare')
  .click('.searchsubmit')
  .screenshot('yahoo.png')
  .run();

CasperJS

ちなみに CasperJS で書くとこんな感じ

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

casper.start('http://google.fr/', function() {
    this.fill('form[action="/search"]', { q: 'casperjs' }, true);
});

casper.then(function() {
  this.capture('google.png');
});

casper.run();

実行

$ node sample_nightmare.js

キャプチャ画像

背景透明になる(サイズ大きいので縮小した)

yahoo.png

API

  • .goto(url)
  • .back()
  • .refresh()
  • .url(cb)
  • .click(selector)
  • .type(selector, text)
  • .upload(selector, path)
  • .evaluate(fn, cb, [arg1, arg2,...])
  • .wait()
  • .wait(ms)
  • .wait(selector)
  • .wait(fn, value, [delay])
  • .screenshot(path)
  • .useragent(useragent)
  • .viewport(width, height)
  • .use(plugin)
  • .run(cb)

プラグイン

nightmare-swiftly をインストールすると下記が使える模様

  • .login(email, password)
  • .task(instructions, uploads, callback)
  • .state(callback)
  • .wait(state, callback)
  • .download(taskUrl, path)
  • .approve(taskUrl)

感想

CasperJS にある Testing みたいの無いっぽい。
試してないけどプラグインが上手く使えればいい感じかも。

参考

78
78
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
78
78