capybara snippets

How to Test Authentication With Devise+Capybara+Minitest

Tagged devise, capybara, authentication, warden, transaction, gotcha, minitest  Languages ruby

Testing authentication functionality with Capybara and Devise? See the following checklist:

* Use shared connections or disable transactional fixtures. * Set Capybara.default_host to match config.session_store.domain or you'll get "401 Unauthorized" * Name of test should end with "integration", e.g. describe "Dashboard Business integration" do * Add the following to your integration tests:

include Warden::Test::Helpers
  Warden.test_mode!

  after do
    Warden.test_reset!
  end

Full example of integration test with Devise, Capybara, and minitest:

class IntegrationSpec < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL
  include Warden::Test::Helpers
  Warden.test_mode!

  before do
    @routes = Rails.application.routes
  end

  after do
    Warden.test_reset!
  end

  def sign_in(user)
    login_as(user, scope: :user)
  end

  def sign_out
    logout(:user)
  end

  def default_url_options
    Rails.configuration.action_mailer.default_url_options
  end
end

MiniTest::Spec.register_spec_type( /integration$/, IntegrationSpec )

Integration Tests With Capybara, Devise, Minitest::Spec, and Capybara-Webkit

Tagged capybara, devise, minitest, spec, webkit, headless  Languages ruby

Does your boss want you to run integration tests with Capybara, Devise, Minitest::Spec, Capybara-Webkit, and the Headless gem?

Then use this code, which features: * Automatic test failure on JavaScript errors * Check for XSS attack vectors with assert_no_alerts. * Sign in/out with Devise

require 'headless'
require 'awesome_print'

headless = Headless.new(reuse: true, destroy_at_exit: false)
puts "Headless mode. Display :#{headless.display}. PID #{Process.pid}"
headless.start

class IntegrationSpec < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL
  include Warden::Test::Helpers
  Warden.test_mode!

  before do
    assert_no_js_errors
  end

  after do
    Warden.test_reset!
  end

  def sign_in(user, scope = :user)
    login_as(user, scope: scope)
  end

  def sign_out(scope = :user)
    logout(scope)
  end

  def assert_no_js_errors
    errors = page.driver.error_messages
    assert_equal 0, errors.size, errors.ai
  end

  def assert_no_alerts(types = [:alert, :confirm, :prompt])
    types.each do |type|
      alerts = page.driver.send(:"#{type}_messages")
      assert_equal 0, alerts.size, alerts.ai
    end
  end

  def default_url_options
    Rails.configuration.action_mailer.default_url_options
  end
end

MiniTest::Spec.register_spec_type( /integration$/, IntegrationSpec )

Remember to name your tests like this:

require 'test_helper'

describe "Dashboard integration" do
...
end

Otherwise the tests won't inherit from the IntegrationSpec class.

Checking For JavaScript Errors or e.g. XSS Attacks With Capybara

Tagged capybara, xss, javascript  Languages ruby

Use the following code to check for JavaScript errors, or XSS vulnerabilities:

class IntegrationSpec < MiniTest::Spec
  def assert_no_js_errors
    assert_equal 0, page.driver.error_messages.size, page.driver.error_messages.ai
  end

  def assert_no_alerts(types = [:alert, :confirm, :prompt])
    types.each do |type|
      alerts = page.driver.send(:"#{type}_messages")
      assert_equal 0, alerts.size, alerts.ai
    end
  end
end

Test plan * Insert XSS hacks into database * Write integration tests that visit all pages

Requires * capybara * capybara-webkit

How to pass a Hash as a parameter to a Cucumber/Capybara step/test

Tagged cucumber, capybara  Languages ruby

Helper:

Given(/^I receive a message from "(.*?)" with data$/) do |from, table|
  # Convert AST table into hash
  data = table.raw.transpose.to_h # ".with_indifferent_access" if needed
  # Do something with the data
end

Test:

And I receive a message from "jebus@hell.com" with data
  | first_name | last_name | status | price |
  | Jebus | Hell | broken | 4€ |

undefined method `+' for nil:NilClass (NoMethodError)

Tagged capybara, cucumber, minitest  Languages ruby

When Capybara/Cucumber tests fail:

undefined method `+' for nil:NilClass (NoMethodError)
.../.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/minitest-5.10.1/lib/minitest/assertions.rb:135:in `assert'

Do this:

  1. Install test-unit
gem 'test-unit'
  1. Include assertions in env.rb
World Test::Unit::Assertions