1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Make RSpec matchers work with session and nodes

Also split RSpec matchers into separate file, so
they can be used with cucumber.
This commit is contained in:
Jonas Nicklas 2011-02-04 14:47:15 +01:00
parent 66dcafc3c7
commit 6fea34cff8
4 changed files with 224 additions and 116 deletions

View file

@ -1,8 +1,10 @@
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec_matchers'
RSpec.configure do |config|
config.include Capybara, :type => :acceptance
config.include Capybara::RSpecMatchers, :type => :acceptance
config.after do
if example.metadata[:type] == :acceptance
Capybara.reset_sessions!
@ -16,36 +18,3 @@ RSpec.configure do |config|
end
end
end
module Capybara
module RSpec
module StringMatchers
extend ::RSpec::Matchers::DSL
%w[css xpath selector].each do |type|
matcher "have_#{type}" do |*args|
match_for_should do |string|
Capybara::string(string).send("has_#{type}?", *args)
end
match_for_should_not do |string|
Capybara::string(string).send("has_no_#{type}?", *args)
end
failure_message_for_should do |string|
"expected #{type} #{formatted(args)} to return something from:\n#{string}"
end
failure_message_for_should_not do |string|
"expected #{type} #{formatted(args)} not to return anything from:\n#{string}"
end
def formatted(args)
args.length == 1 ? args.first.inspect : args.inspect
end
end
end
end
end
end

View file

@ -0,0 +1,37 @@
module Capybara
module RSpecMatchers
extend ::RSpec::Matchers::DSL
%w[css xpath selector].each do |type|
matcher "have_#{type}" do |*args|
match_for_should do |actual|
wrap(actual).send("has_#{type}?", *args)
end
match_for_should_not do |actual|
wrap(actual).send("has_no_#{type}?", *args)
end
failure_message_for_should do |actual|
"expected #{type} #{formatted} to return something from:\n#{actual.inspect}"
end
failure_message_for_should_not do |actual|
"expected #{type} #{formatted} not to return anything from:\n#{actual.inspect}"
end
define_method :wrap do |actual|
if actual.respond_to?("has_#{type}?")
actual
else
Capybara.string(actual.to_s)
end
end
define_method :formatted do
args.length == 1 ? args.first.inspect : args.inspect
end
end
end
end
end

185
spec/rspec_matchers_spec.rb Normal file
View file

@ -0,0 +1,185 @@
require 'spec_helper'
require 'capybara/dsl'
require 'capybara/rspec_matchers'
Capybara.app = TestApp
describe Capybara::RSpecMatchers do
include Capybara
include Capybara::RSpecMatchers
describe "have_css matcher" do
context "on a string" do
context "with should" do
it "passes if has_css? returns true" do
"<h1>Text</h1>".should have_css('h1')
end
it "fails if has_css? returns false" do
expect do
"<h1>Text</h1>".should have_css('h2')
end.to raise_error(/expected css "h2" to return something/)
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
"<h1>Text</h1>".should_not have_css('h2')
end
it "fails if has_no_css? returns false" do
expect do
"<h1>Text</h1>".should_not have_css('h1')
end.to raise_error(/expected css "h1" not to return anything/)
end
end
end
context "on a page or node" do
before do
visit('/with_html')
end
context "with should" do
it "passes if has_css? returns true" do
page.should have_css('h1')
end
it "fails if has_css? returns false" do
expect do
page.should have_css('h1#doesnotexist')
end.to raise_error(/expected css .* to return something/)
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
page.should_not have_css('h1#doesnotexist')
end
it "fails if has_no_css? returns false" do
expect do
page.should_not have_css('h1')
end.to raise_error(/expected css .* not to return anything/)
end
end
end
end
describe "have_xpath matcher" do
context "on a string" do
context "with should" do
it "passes if has_css? returns true" do
"<h1>Text</h1>".should have_xpath('//h1')
end
it "fails if has_css? returns false" do
expect do
"<h1>Text</h1>".should have_xpath('//h2')
end.to raise_error(%r(expected xpath "//h2" to return something))
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
"<h1>Text</h1>".should_not have_xpath('//h2')
end
it "fails if has_no_css? returns false" do
expect do
"<h1>Text</h1>".should_not have_xpath('//h1')
end.to raise_error(%r(expected xpath "//h1" not to return anything))
end
end
end
context "on a page or node" do
before do
visit('/with_html')
end
context "with should" do
it "passes if has_css? returns true" do
page.should have_xpath('//h1')
end
it "fails if has_css? returns false" do
expect do
page.should have_xpath("//h1[@id='doesnotexist']")
end.to raise_error(%r(expected xpath "//h1\[@id='doesnotexist'\]" to return something))
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
page.should_not have_xpath('//h1[@id="doesnotexist"]')
end
it "fails if has_no_css? returns false" do
expect do
page.should_not have_xpath('//h1')
end.to raise_error(%r(expected xpath "//h1" not to return anything))
end
end
end
end
describe "have_selector matcher" do
context "on a string" do
context "with should" do
it "passes if has_css? returns true" do
"<h1>Text</h1>".should have_selector('//h1')
end
it "fails if has_css? returns false" do
expect do
"<h1>Text</h1>".should have_selector('//h2')
end.to raise_error(%r(expected selector "//h2" to return something))
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
"<h1>Text</h1>".should_not have_selector('//h2')
end
it "fails if has_no_css? returns false" do
expect do
"<h1>Text</h1>".should_not have_selector('//h1')
end.to raise_error(%r(expected selector "//h1" not to return anything))
end
end
end
context "on a page or node" do
before do
visit('/with_html')
end
context "with should" do
it "passes if has_css? returns true" do
page.should have_selector('//h1')
end
it "fails if has_css? returns false" do
expect do
page.should have_selector("//h1[@id='doesnotexist']")
end.to raise_error(%r(expected selector "//h1\[@id='doesnotexist'\]" to return something))
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
page.should_not have_selector('//h1[@id="doesnotexist"]')
end
it "fails if has_no_css? returns false" do
expect do
page.should_not have_selector('//h1')
end.to raise_error(%r(expected selector "//h1" not to return anything))
end
end
end
end
end

View file

@ -45,86 +45,3 @@ describe 'capybara/rspec', :type => :other do
expect { visit('/') }.to raise_error(NoMethodError)
end
end
describe Capybara::RSpec::StringMatchers do
include Capybara::RSpec::StringMatchers
before { Capybara.default_selector = :css }
after { Capybara.default_selector = :xpath }
describe "have_css matcher" do
context "with should" do
it "passes if has_css? returns true" do
"<h1>Text</h1>".should have_css('h1')
end
it "fails if has_css? returns false" do
expect do
"<h1>Text</h1>".should have_css('h2')
end.to raise_error(/expected css .* to return something/)
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
"<h1>Text</h1>".should_not have_css('h2')
end
it "fails if has_no_css? returns false" do
expect do
"<h1>Text</h1>".should_not have_css('h1')
end.to raise_error(/expected css .* not to return anything/)
end
end
end
describe "have_xpath matcher" do
context "with should" do
it "passes if has_xpath? returns true" do
"<h1>Text</h1>".should have_xpath('//h1')
end
it "fails if has_xpath? returns false" do
expect do
"<h1>Text</h1>".should have_xpath('//h2')
end.to raise_error(/expected xpath .* to return something/)
end
end
context "with should_not" do
it "passes if has_no_xpath? returns true" do
"<h1>Text</h1>".should_not have_xpath('//h2')
end
it "fails if has_no_xpath? returns false" do
expect do
"<h1>Text</h1>".should_not have_xpath('//h1')
end.to raise_error(/expected xpath .* not to return anything/)
end
end
end
describe "have_selector matcher" do
context "with should" do
it "passes if has_selector? returns true" do
"<h1>Text</h1>".should have_selector('h1')
end
it "fails if has_selector? returns false" do
expect do
"<h1>Text</h1>".should have_selector('h2')
end.to raise_error(/expected selector .* to return something/)
end
end
context "with should_not" do
it "passes if has_no_selector? returns true" do
"<h1>Text</h1>".should_not have_selector('h2')
end
it "fails if has_no_selector? returns false" do
expect do
"<h1>Text</h1>".should_not have_selector('h1')
end.to raise_error(/expected selector .* not to return anything/)
end
end
end
end