PhantomJS - O Fantasminha Camarada
-
Upload
henrique-gogo -
Category
Technology
-
view
4.523 -
download
3
description
Transcript of PhantomJS - O Fantasminha Camarada
![Page 1: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/1.jpg)
PhantomJSO fantasminha camarada
Henrique Gogó@henriquegogo
![Page 2: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/2.jpg)
O que é PhantomJS?
Um "browser" webkit com API JavaScript completa que roda no console.
Suporte nativo a vários padrões web standards: DOM, seletores CSS, JSON, Ajax, Canvas e
SVG.
![Page 3: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/3.jpg)
Para que serve?
Rodar testes funcionais (de JavaScript) com frameworks como Jasmine, QUnit ou CasperJS.
Acessar e manipular páginas com acesso completo ao DOM e bibliotecas de JS como
jQuery.
![Page 4: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/4.jpg)
Por que não WebRat?
WebRat é um simulador de browser com capacidade de DOM, seletores CSS, de fácil
integração com RSpec, Cucumber, Test::Unit e outros.
Mas e o JavaScript?
![Page 5: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/5.jpg)
Por que não Selenium?
Selenium é contém uma API para você realizar testes de aceitação em diversos browsers. Para
isso ele levanta uma instância do browser.
![Page 6: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/6.jpg)
Usando PhantomJS comoengine JavaScript do Capybara
https://github.com/jonleighton/poltergeist
require 'capybara/poltergeist'Capybara.javascript_driver = :poltergeist
![Page 7: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/7.jpg)
Ou simplesmente QUnit
$ phantomjs run-qunit.js test/index.html
Tests completed in 1528 milliseconds. 1223 tests of 1223 passed, 0 failed.
![Page 8: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/8.jpg)
QUnit
test("a basic test example", function() { ok( true, "this test is fine" ); var value = "hello"; equal( value, "hello", "We expect value to be hello" );});
![Page 9: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/9.jpg)
Exemplos//teste.jsvar page = new WebPage();var url = "http://www.gogs.com.br/";page.open(url, function (status) { //Página carregada phantom.exit();});
$ phantomjs teste.js
![Page 10: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/10.jpg)
Manipulação de DOM
var page = new WebPage();console.log('Seu user agent é:' + page.settings.userAgent);page.settings.userAgent = 'SpecialAgent';page.open('http://www.httpuseragent.org', function (status) { if (status !== 'success') { console.log('Unable to access network'); } else { var ua = page.evaluate(function () { return document.getElementById('myagent').innerText; }); console.log(ua); } phantom.exit();});
![Page 11: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/11.jpg)
Listener de requisições
var url = phantom.args[0];
var page = new WebPage();page.onResourceRequested = function (request) { console.log('Request ' + JSON.stringify(request, undefined, 4));};page.onResourceReceived = function (response) { console.log('Receive ' + JSON.stringify(response, undefined, 4));};page.open(url);
![Page 12: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/12.jpg)
page.render()$ phantomjs rasterize.js http://www.google.com.br google.png
![Page 13: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/13.jpg)
Blá Blá Blá!Blá Blá Blá!Blá Blá Blá!Blá Blá Blá!Blá Blá Blá!
![Page 14: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/14.jpg)
Então...
Quer fazer testes de aceitação com fidelidade à engine WebKit (Chrome, Safari, Mobile
browsers)?
Quer testar JavaScript sem precisar abrir browser?
Quer integrar com Ruby usando Capybara, ou outras plataformas como C# (usando Chutzpah)?
![Page 15: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/15.jpg)
PhantomJS
![Page 16: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/16.jpg)
![Page 17: PhantomJS - O Fantasminha Camarada](https://reader033.fdocuments.net/reader033/viewer/2022061216/54b3725e4a795928798b456e/html5/thumbnails/17.jpg)
Perguntas?
@henriquegogo
www.gogs.com.br