Poltergeist seems to crash once the user is logged in with my cucumber test

148 views Asked by At

I'm trying to test a feature using cucumber, capybara and poltergeist, which needs to accept a confirm dialogue in order to delete a user. Even though it is possible for the other tests to navigate to the users page using the capybara Driver, with the Javascript test poltergeist seems to get as far as logging a user in and then stops working.

Ive tried using xpath and click_link once it is apparently on the welcome page, but none of this works.

Here is the error I get:

When I select a user to delete # features/step_definitions/edit_user.rb:37 Unable to find xpath "//a[@href='/users/2?page=1' and @data-confirm='Are you sure?']" (Capybara::ElementNotFound)

Bare in mind that this error occurs due to the fact that poltergeist never actually reaches the users page.

Here are the Feature steps:

#Feature: Delete User, Scenario: User successfully deletes another user
Given /^I am currently on the Users page to delete$/ do
    open_page "Users"
end

When /^I select a user to delete$/ do
    find(:xpath,'//a[@href="/users"]').trigger('click')
    save_and_open_page
    #page.evaluate_script('window.confirm = function() { return true; }')
    find(:xpath, "//a[@href='/users/2?page=1' and @data-confirm='Are you sure?']").click
end

And /^I confirm that I would like to delete the selected user$/ do
    page.driver.browser.switch_to.alert.accept
end

Then /^the user should no longer visible on the users page$/ do
    expect(page).to have_no_content "[email protected]" 
end

Here is the helper_methods file:

# Helper Methods
def create_user
    @user = User.new
end

def submit_user_details (login, password)
    visit new_session_path
    fill_in "login-input", :with => login
    fill_in "password", :with => password
    click_button "submit_login"
end

def is_guest_page 
    expect(page).to have_content "Welcome Guest"
end

def is_admin_page
    expect(page).to have_content "Welcome admin"
end

def check_test_email 
    exists = false
    test_email = "[email protected]"
    if User.exists?(:email => test_email) 
        exists = true
    end
    exists
end

def open_page (page)
    submit_user_details "admin", "taliesin"
    click_link page
end

Here is the feature file:

Feature: Delete User
In order to remove an exisiting user
As a user
I want to be able to remove a user from the database

@javascript
Scenario: User successfully deletes another user
    Given I am currently on the Users page to delete
    When I select a user to delete
    And I confirm that I would like to delete the selected user
    Then the user should no longer visible on the users page

Here is the env.rb:

require 'cucumber/rails'
require 'capybara/rails'

require 'capybara/poltergeist'

Capybara.javascript_driver = :poltergeist

Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app,
    :phantomjs_options => ['--debug=no', '--load-images=no', '--ignore-ssl-errors=yes', '--       ssl-protocol=TLSv1'], :debug => false)
end

Dir["../../spec/factories/*.rb"].each {|file| require_relative file }

ActionController::Base.allow_rescue = false

# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
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

Before do
DatabaseCleaner.start
load Rails.root.join('db/seeds.rb')
end

After do |scenario|
DatabaseCleaner.clean
end

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

Here is the test part of my gemfile:

group :test do
  gem 'cucumber-rails', :require => false
  gem 'poltergeist', '~> 1.5.1'
  gem 'phantomjs', :require => 'phantomjs/poltergeist'
  gem 'selenium-webdriver'
  gem 'launchy'
  gem 'database_cleaner'
  gem 'rspec-rails', '~> 3.0.0'
  gem 'factory_girl_rails'
end
0

There are 0 answers