Search code examples
rubyrspecphantomjscapybarapoltergeist

Javascript trigger when using poltergiest/capybara outside of rails


I'm using Capybara, rspec and poltergeist, outside of rails, to run some headless integrations tests. The scenario is that, there are 2 select fields. If I select a value in the first select field, the 2nd select field is populated based on the value of the first select field. If I run my spec using poltergeist in Mac OSX, the spec works. But in ubuntu, it fails, it seems that the 2nd select field is not populated. I also have js: true on my specs.

Here's my spec_helper.rb:

require 'capybara/poltergeist'
require 'capybara'
require 'capybara/rspec'
require 'pry'
require 'support/session_helper'
RSpec.configure do |config|

  config.include Capybara::DSL
  config.include Capybara::Poltergeist
  config.include SessionHelper

  Capybara.run_server = false
  Capybara.default_driver = :poltergeist
  Capybara.javascript_driver = :poltergeist
  Capybara.app_host = "http://vps-staging.dropmysite.com"

  options = { js_errors: false }
  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app, options)
  end

  config.expect_with :rspec do |expectations|

    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

end

EDIT Adding failing spec

require 'spec_helper'

feature 'vps-staging', js: true do
  background do
    visit '/'
  end

  let(:timestamp) { Time.now.strftime('%Y-%m-%d_%H-%M-%S') }


  feature 'create private server' do
    background do
      sign_in '[email protected]'
    end

    it 'successfully creates server' do
      find(:xpath, "//a[@href='/en/private_servers/new']").click

      directory_field = all(".tagit-new")[0].find("input")
      select 'Ubuntu', from: 'distribution'
      select '15.04', from: 'version'
      fill_in 'private_server_profile_name', with: "Auto Test #{timestamp}"
      directory_field.set "/home"
      click_button 'Save'

      visit '/en/dashboard'

      expect(page.text).to have_content "Auto Test #{timestamp}"
    end
  end
end

EDIT another thing I found out is that, this bug only happens on phantomjs version 1.9.8 and below. 2.0 works fine.


Solution

  • As Tom Walpole pointed out, updating poltergeist from 1.6.0 to 1.7.0 fixed most of my problem but I still need to tweak a bit to fully solve my problem. What I did is to create a js file to hold the polyfill and edit my spec_helper.rb and tell poltergeist to use the options, extensions and include the polyfill.js thus changing this code from

    options = { js_errors: false }
      Capybara.register_driver :poltergeist do |app|
        Capybara::Poltergeist::Driver.new(app, options)
      end
    

    to

    options = { js_errors: false, extensions: ["spec/support/polyfill.js"] }
      Capybara.register_driver :poltergeist do |app|
        Capybara::Poltergeist::Driver.new(app, options)
      end