Skip to content

Instantly share code, notes, and snippets.

@obfusk
Last active February 23, 2018 11:10
Show Gist options
  • Select an option

  • Save obfusk/7803381 to your computer and use it in GitHub Desktop.

Select an option

Save obfusk/7803381 to your computer and use it in GitHub Desktop.
EventSource and phantomjs/poltergeist/capybara-webkit

I've been using capybara to test a small web app that uses EventSource. I ran into some issues with headless testing.

First attempt

visit 'http://localhost:9292'
  • selenium/firefox works just fine
  • poltergeist results in a Capybara::Poltergeist::TimeoutError
  • capybara-webkit hangs

PhantomJS

  • phantomjs a.js hangs
  • phantomjs b.js works
  • phantomjs c.js works
  • phantomjs d.js shows network timeout on EventSource stream URL (and results in a PhantomJS crash -- why ?!)
  • phantomjs e.js hangs, which suggest that the problem is that onLoadFinished is (obviously ?!) never called because the stream never finishes loading

Second attempt

visit 'about:blank'
page.execute_script 'document.location = "http://localhost:9292"'
find '.some-class', text: 'page has been loaded'
  • selenium/firefox works just fine
  • poltergeist works just fine
  • capybara-webkit still hangs
var page = require('webpage').create();
page.open('http://localhost:9292', function () {
page.render('a.png');
phantom.exit();
});
var page = require('webpage').create();
page.open('about:blank', function () {
page.evaluate(function () {
document.location = 'http://localhost:9292';
});
});
setTimeout(function () {
page.render('b.png');
phantom.exit();
}, 1000);
var page = require('webpage').create();
page.open('http://localhost:9292');
setTimeout(function () {
page.render('c.png');
phantom.exit();
}, 1000);
var page = require('webpage').create();
page.settings.resourceTimeout = 2000;
page.onResourceTimeout = function(e) {
console.log(e.errorCode);
console.log(e.errorString);
console.log(e.url);
phantom.exit(1);
};
page.open('http://localhost:9292', function () {
page.render('d.png');
phantom.exit();
});
var page = require('webpage').create();
page.open('http://localhost:9292');
page.onLoadFinished = function(status) {
console.log('Status: ' + status);
console.log('Loaded: ' + page.url);
page.render('e.png');
phantom.exit();
};
require 'coffee-script'
require 'haml'
require 'sinatra'
get '/' do
haml :index
end
get '/stream' do
content_type 'text/event-stream'
stream :keep_open do |out|
out << "data: Hi!\n\n"
i = 0; EM::PeriodicTimer.new(1) { out << "data: #{i += 1}\n\n" }
end
end
get '/foo.js' do
content_type :js
coffee :foo
end
__END__
@@index
!!!
%html
%head
%script{ type: 'text/javascript',
src: 'http://code.jquery.com/jquery-2.0.3.min.js' }
%script{ type: 'text/javascript', src: '/foo.js' }
%body
@@foo
$ ->
esrc = $ new EventSource '/stream'
esrc.on 'error', (e) -> console.log 'EventSource error:', e
esrc.on 'message', (e) ->
$('body').append $('<div>').text e.originalEvent.data
require 'capybara/poltergeist'
require 'capybara/rspec'
require 'capybara/webkit'
require 'rspec'
Capybara.run_server = false
Capybara.javascript_driver = (ENV['JS_DRIVER']||'poltergeist').to_sym
describe 'foo', type: :feature, js: true do
it 'works' do
visit 'about:blank'
page.execute_script 'document.location = "http://localhost:9292"'
find 'div', text: 'Hi'
end
it 'fails' do
visit 'http://localhost:9292'
end
end
@alexanderadam
Copy link

I think I run into the same issue (blocking Capybara) with chromedriver right now.
Did you have any solution for these cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment