3
votes

I am trying to run cucumber tests of my app over capybara with selenium driver. In the test steps i dynamically create users to login to the app . But the user login fails with the user i have created. I have set the user_transactional_fixture to false. Still the created records are not available to the selenium app.

Here is the code

Feature File

@browser Scenario: Admin alone can have access to admin pages Given "[email protected]" is a admin When I have logged in as "[email protected]" step definition file

Given /"([^\"]*)" is a admin/ do |email|

user = Email.active.find_by_address(email).try(:user) || User.new({}, :password =>

'Monkey_123', :password_confirmation => 'Monkey_123', :last_name => 'example', :first_name => 'admin')

user.update_attribute(:state, "active")

user.update_attribute(:terms_and_conditions_accepted, 1)

user.groups << Group.find(1)

user.primary_email ||= Email.new(:address => email, :state => Email::State::ACTIVE,

:email_type => Email::Type::PRIMARY)

user.save!

when i run the tests using capybara and selenium driveri am unable to login to my app using

the created user through the browser.

I tried using the ruby debugger, which showed the creation of the particular user.

Following is my config in the env.rb file

if defined?(ActiveRecord::Base)

begin

require 'database_cleaner'

DatabaseCleaner.strategy = :truncation

rescue LoadError => ignore_if_database_cleaner_not_present

end

end

Capybara.server_port = 9887 # Or whatever number you want?

Capybara.app_host = "http://localhost:#{Capybara.server_port}"

Capybara.default_wait_time = 4

Capybara.ignore_hidden_elements=false

Before('@browser') do Capybara.current_driver = :selenium end

Kindly suggest a solution for this. The gems versions are 1) cucumber -v 0.10.7

2)cucumber-rails 0.3.2

3)Capybara 0.4.1.2

4) database_cleaner 0.5.0

Thanks

1
I don't see a question. I also don't see any code, which would be helpful if you want people to figure out what you're doing wrong.Mark Thomas

1 Answers

1
votes

I had a similar problem with Cucumber, Capybara and Selenium. Devise somehow did not allow the login for created users. During the test Cucumber was unable to login to the app using the created user through the browser. When I set the Capybara driver to rack_test the test passed, but when I set it to selenium, it failed with 'Invalid email or password.' on the login page for Devise.

Finally I found the answer here and here. When using transactional fixtures, Selenium does not have access to information that has been written to the database. Therefore you have to switch transactional fixtures off and set the DatabaseCleaner strategy to :truncation. In your configuration files (in my case features/support/env.rb and spec/spec_helper.rb) define the following: in the cucumber configuration features/support/env.rb

DatabaseCleaner.strategy = :truncation

and in the RSpec configuration spec/spec_helper.rb

RSpec.configure do |config|
  config.use_transactional_fixtures = false
  config.before :each do
    DatabaseCleaner.start
  end
  config.after :each do
    DatabaseCleaner.clean
  end
end