Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NightmareJS : hier j'ai fait un cauchemar

NightmareJS : hier j'ai fait un cauchemar

Petite présentation de NightmareJS, librairie qui simplifie l'utilisation de phantomJS et sérieux concurrent de CasperJS

Mickaël Andrieu

December 18, 2014

Transcript

9 slides
  1. $ npm install -g nigthmare /* nightmare a une dépendance

    * sur phantomjs */ $ npm install -g phantomjs
  2. /* le fameux hello world, à la sauce nightmare *

    & human talks */ var Nightmare = require('nightmare'); new Nightmare() .goto('http://news.humancoders.com/') .url(function(url){ console.log('nightmare accède à: ' +url); }) .title(function(title){ console.log('le title du site est: ' +title); }) .run();
  3. ➜ nightmare-prez node hello-world.js nightmare accède à: http://news.humancoders.com/ le title

    du site est: Human Coders News /* un debogueur est intégré */ ➜ nightmare-prez DEBUG=nightmare node hello-world.js nightmare queueing action "goto" +0ms nightmare queueing action "url" +3ms nightmare queueing action "title" +0ms nightmare run +0ms nightmare .setup() creating phantom instance with options {"timeout":5000,"interval":50,"weak":true,"loadImages":true, "ignoreSslErrors":true,"sslProtocol":"any","proxy":null,"proxyType":null, "proxyAuth":null,"cookiesFile":null,"webSecurity":true } +0ms nightmare .setup() phantom instance created +147ms nightmare .setup() phantom page created +4ms nightmare .goto() url: http://news.humancoders.com/ +1ms nightmare .goto() page loaded: success +1s nightmare .url() getting it +501ms nightmare accède à: http://news.humancoders.com/ nightmare .title() getting it +2ms le title du site est: Human Coders News nightmare .teardownInstance() tearing down +1ms
  4. // Test fonctionnel et d'interface avec nightmare/mocha/chai var Nightmare =

    require('nightmare'); var should = require('chai').should(); describe('Human talks demo', function () { this.timeout(5000); // timeout de 2s, mais on doit attendre le lancement de phantomjs var url = 'http://news.humancoders.com/'; describe('Start page', function () { it('Doit avoir le bon titre au chargement de la page', function (done) { new Nightmare() .goto(url) .title(function(title){ title.should.equal('Human Coders News'); done(); }) .run(); }); it('Doit avoir des news affichées au chargement de la page', function (done) { new Nightmare() .goto(url) .evaluate(function () { return document.querySelectorAll('#items').length; }, function (result) { result.should.be.above(0); done(); }) .run(); }); }); });
  5. ➜ nightmare-prez mocha test-hello-world.js Human talks demo Start page ✓

    should have the correct title when loaded (2812ms) ✓ should show news when loaded (2784ms) 2 passing (6s) ➜ nightmare-prez

More Decks by Mickaël Andrieu

Other Decks in Programming