3
votes

The way I'm setting up the capybara driver seems to be causing phantomjs to not execute javascript. To confirm that this can work outside of capybara / poltergeist, I used raw watir-webdriver with the same phantomjs installation and it did run javascript.

So here is my code that creates the capybara driver using poltergeist:

require 'capybara'
require 'capybara/poltergeist'

include Capybara::DSL

Capybara.register_driver :poltergeist do |app|
  options = {
    :js_errors => true,
    :timeout => 120,
    :debug => false,
    :phantomjs_options => ['--load-images=no', '--disk-cache=false'],
    :inspector => true,
  }
  Capybara::Poltergeist::Driver.new(app, options)
end

Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.app_host = 'https://google.com'

visit('/')
fill_in('q', with: 'green cheese')
sleep 2

p all('span').map{|s| s.text}

When I run that I get out like:

["", "", "+You", "", "Search", "", "Images", "", "Maps", "", "Play", "", "YouTube", "", "News", "", "Gmail", "", "More", "More", "", "", "", "Sign in", "Sign in", "", "", "", "", "× A faster way to browse the web Install Google Chrome", "", "", "", "", "Advertising ProgramsBusiness Solutions+GoogleAbout Google © 2013 - Privacy & Terms"]

Which makes it clear that the search suggestions are not showing up, which means javascript is not running. I've confirmed this using screenshots.

Now, when I run the following code that uses only water-webdriver, it works:

require 'watir-webdriver'

b = Watir::Browser.new :phantomjs

b.goto 'https://google.com'
b.text_field(:name, 'q').set "green cheese"
sleep 2
p b.spans.map{|s| s.text }
b.close

and that gives the following output:

["", "", "+You", "", "Search", "", "Images", "", "Maps", "", "Play", "", "YouTube", "", "News", "", "Gmail", "", "More", "More", "", "", "", "Sign in", "Sign in", "", "", "", "", "×\nA faster way to browse the web\nInstall Google Chrome", "", "", "", "", "Advertising ProgramsBusiness Solutions+GoogleAbout Google\n© 2013 - Privacy & Terms", "green cheese", "green cheese strain", "green cheese weed", "green cheese enchiladas", "green cheesecake", "green cheese moon", "green cheese enchiladas recipe", "green cheese strain review", "green cheese media group", "green cheese penny", "", "", "", ""]

All that green cheese in there makes it clear that search suggestions are showing up, which means javascript must be running. Again, I have confirmed this using screenshots.

Can anyone help me understand how to configure Capybara/poltergeist so it works?

One final note: This has nothing to do with Rails. this is a standalone test suite, and you can find the full code for it here: https://github.com/rschultheis/rspec_capybara_starter

1
I realise it has been a while; bud did you ever find a resolution for this? I'd hate to have to abandon poltergeistE.E.33
Nevermind, I've found my solution, and added an answer below.E.E.33

1 Answers

0
votes

I was able to solve this issue by upgrading my phantomjs from 1.9.8 to 2.1.1. For instructions on doing this reference this http://phantomjs.org/download.html.