Bishma Stornelli
Bishma Stornelli

Reputation: 2569

Testing rails app with cucumber + capybara + selenium leads to blank pages

I'm trying to test a confirmation dialogue in javascript with cucumber 1.2.1, capybara 1.1.2 and selenium. Ruby is 1.9.3 and RoR is 3.2.6.

My features/env.rb is:

require 'cucumber/rails'

Capybara.default_selector = :css
Capybara.default_driver = :selenium

ActionController::Base.allow_rescue = false

begin
  DatabaseCleaner.strategy = :transaction
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end

Cucumber::Rails::Database.javascript_strategy = :truncation

# For testing email
require 'email_spec/cucumber'

# Load FactoryGirl methods
World(FactoryGirl::Syntax::Methods)

require 'declarative_authorization/maintenance'
World(Authorization::TestHelper)

# To handle popups
require 'prickle/capybara'
World(Prickle::Capybara)

My steps are:

# To test popups. The popup object is define by the gem prickle
Entonces /^debería ver "([^"]*)" en un cuadro de diálogo$/ do |text|
  popup.message.should have_content(text)
end

Cuando /^(cancelo|confirmo) el cuadro de diálogo$/ do |option|
  popup.send(option == "cancelo" ? "dismiss" : "confirm")
end

The gem prickle open those popups using page.driver.browser.switch_to.alert (it's just a shorcut).

My installed gems are:

group :test do
  gem 'cucumber-rails'
  # database_cleaner is not required, but highly recommended
  gem 'database_cleaner'
  gem 'capybara'
  # To test email delivery with cucumber
  gem 'email_spec'
  # Rspec
  gem 'rspec-rails'
  gem 'rspec'
  # To create instance of objects in the database
  gem 'factory_girl_rails'
  # To generate random data
  gem 'faker'
  # To open pages with capybara
  gem 'launchy'

  #To test popups
  gem 'prickle'
  # To open web browser while testing and test javascript
  gem 'selenium'
end

When I run my tests it doesn't show anything in my browser (firefox). It says "the connection was reset" and of course capybara can't find any element in the page. If I remove the line:

Capybara.default_driver = :selenium

from env.rb, tests without javascript works ok but when trying to run the steps for popups it says:

undefined method `switch_to' for #<Capybara::RackTest::Browser:0x00000005424cf8> (NoMethodError)

What can I try? I've been a lot of hours trying different methods but no one works. I also tried webkit but I couldn't install it because of a mysterious error.

UPDATED TO SHOW CONTENT OF FEATURE

    #language: es
    Característica: Eliminar usuario

    Antecedentes: Usuarios registrados

        Dado que los siguientes usuarios existen:

           | email                     | password      |
           | [email protected]     | b12345        |
           | [email protected]      | e12345        |

        Y estoy autenticado como "[email protected]"


    Escenario: Eliminación exitosa    
        Dado que estoy autorizado para "eliminar usuario"
        Cuando visito la página "ver usuario" para "[email protected]"
        Y presiono "Eliminar usuario"
        Entonces debería ver "¿Está seguro que desea eliminar el usuario [email protected]?" en un cuadro de diálogo
        Cuando confirmo el cuadro de diálogo
        Entonces debería estar en la página "listar usuarios"
        Y debería ver "El usuario '[email protected]' ha sido eliminado exitosamente"
        Y no debería existir un "usuario" con "email" "[email protected]"

Upvotes: 0

Views: 2094

Answers (1)

mmrobins
mmrobins

Reputation: 13775

I had this same problem when my test database wasn't migrated to be current, but it wasn't obvious what was happening because errors weren't displaying in the console, I just got a "connection reset" error like you were getting. I was finally able to get the stack trace for the problem to appear by setting the following in features/support/env.rb

Capybara.app = Rack::ShowExceptions.new(YourRailsApp::Application)

Upvotes: 3

Related Questions