开发者

Testing jquery dialog with capybara-webkit

开发者 https://www.devze.com 2023-04-10 10:00 出处:网络
I want to test an interaction where the user clicks a li开发者_如何学编程nk and a jquery dialog (http://jqueryui.com/demos/dialog/) pops up.

I want to test an interaction where the user clicks a li开发者_如何学编程nk and a jquery dialog (http://jqueryui.com/demos/dialog/) pops up.

I would like to test:

  1. That the dialog shows up
  2. That text in the dialog box is correct
  3. That the user can exit by clicking the 'x'

I tested the dialog manually and it works but I am interested in how I would write the actual spec. I would like to do this using Capybara-webkit, but I haven't been able to find code samples for actually using Capybara-webkit.

I am using Capybara-webkit and specs but not cucumber.


Here's how to do this:

Resources

  • Railscasts Episode 257: Request Specs and Capybara
  • Capybara-webkit on Github

Installation

Make sure your Gemfile includes capybara-webkit, rspec-rails, and database_cleaner:

group :development, :test do
  gem 'rspec-rails', '~> 2.7.0'
  gem 'capybara-webkit', '~> 0.7.2'
  gem 'database_cleaner', '~> 0.6.7'
end

You need database_cleaner because database transactions aren’t compatible with rspec drivers besides Rack::Test:

RSpec.configure do |config|

#...

  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

You must still use the :js => true flag in your tests. Turns out, it's not a selenium-only feature:

describe "Something" do
  it "uses javascript", :js => true do
     pending "write a test"
  end
end

And, of course, require 'capybara/rspec' and set Capybara.javascript_driver = :webkit in your spec_helper:

#...

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
Capybara.javascript_driver = :webkit

#...

That should be all it takes to get capybara-webkit and rspec up and running.

0

精彩评论

暂无评论...
验证码 换一张
取 消