├── VERSION ├── .gitignore ├── lib ├── minitest-capybara.rb ├── minitest │ ├── capybara │ │ ├── version.rb │ │ └── helpers.rb │ └── capybara.rb └── capybara │ ├── expectations.rb │ └── assertions.rb ├── test ├── test_helper.rb ├── capybara │ ├── expectations_test.rb │ └── assertions_test.rb └── minitest │ ├── base_node_test.rb │ └── capybara_test.rb ├── Rakefile ├── Gemfile ├── CHANGELOG.md ├── .travis.yml ├── minitest-capybara.gemspec ├── LICENSE.txt └── README.md /VERSION: -------------------------------------------------------------------------------- 1 | 0.9.0 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | pkg/* 4 | /Gemfile.lock 5 | -------------------------------------------------------------------------------- /lib/minitest-capybara.rb: -------------------------------------------------------------------------------- 1 | require 'minitest' 2 | require 'minitest/capybara' 3 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require "minitest/autorun" 2 | require "minitest/capybara" 3 | -------------------------------------------------------------------------------- /lib/minitest/capybara/version.rb: -------------------------------------------------------------------------------- 1 | module MiniTest 2 | module Capybara 3 | VERSION = File.read(File.expand_path("../../../../VERSION", __FILE__)).strip 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rake/testtask" 3 | 4 | Rake::TestTask.new(:test) do |t| 5 | t.libs << 'lib' 6 | t.libs << 'test' 7 | t.pattern = 'test/**/*_test.rb' 8 | t.verbose = false 9 | end 10 | 11 | task :default => :test 12 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Specify your gem's dependencies in minitest-matchers-capybara.gemspec 4 | gemspec 5 | 6 | if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.2.2") 7 | gem "rack", "< 2.0" 8 | end 9 | 10 | # DO NOT RELEASE WITH THIS LINE STILL HERE. 11 | gem "capybara", "~> 2.8.0" 12 | -------------------------------------------------------------------------------- /test/capybara/expectations_test.rb: -------------------------------------------------------------------------------- 1 | require "test_helper" 2 | 3 | describe "Expectations" do 4 | include Capybara::DSL 5 | include Capybara::Assertions 6 | 7 | it "defines all the must expectations that Capybara does" do 8 | Minitest::Capybara.assertions.each do |assertion| 9 | assert page.respond_to?("must_have_#{assertion}"), 10 | "The expectation must_have_#{assertion} is not defined." 11 | end 12 | end 13 | 14 | it "defines all the wont expectations that Capybara does" do 15 | Minitest::Capybara.refutations.each do |refutation| 16 | assert page.respond_to?("wont_have_#{refutation}"), 17 | "The expectation wont_have_#{refutation} is not defined." 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## HEAD 2 | 3 | ## v0.9.0 4 | 5 | * Loosen hard capybara dependency 6 | * Fix missing failure text in Capybara 2.9+ 7 | 8 | ## v0.8.2 9 | 10 | * Explicitly require capybara/dsl 11 | * Introduce Minitest::Capybara::Behaviour that can be mixed in to test/spec classes 12 | 13 | ## v0.8.1 14 | 15 | * Do an assertion with `assert_selector`/`refute_selector` call - @joseramonc, @wojtekmach 16 | 17 | ## v0.8.0 18 | 19 | * Remove deprecation for Minitest::Capybara::Assertions. 20 | * Better error messages for `assert_content/refute_content`. 21 | * Remove feature DSL. 22 | * Add Minitest::Capybara::Test and Minitest::Capybara::Spec classes. 23 | 24 | ## v0.7.2 25 | 26 | * Update exception message to Capybara::Assertions. - @apotonick 27 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | cache: bundler 3 | rvm: 4 | - 2.1.8 5 | - 2.2.6 6 | - 2.3.3 7 | - 2.4.0 8 | 9 | sudo: false 10 | script: RUBYOPT="-w $RUBYOPT" bundle exec rake 11 | 12 | before_install: 13 | - gem update bundler 14 | 15 | deploy: 16 | provider: rubygems 17 | api_key: 18 | secure: beHIrfhi+kpvA67zbiMXx5n2qAUaNUz+y3RO3yB19jIiTm4Zol8DRJH9jKO6YpIwLlolTrGVQsmZHqmmXRghv3qZXeYwLdp0mvyMIYc5w10OiRwDzlS1DFogevDszRDpypP7J48oFn2llRt1mw9HyLE83GFQcTEvxUJ+ENLA3tY= 19 | gem: minitest-capybara 20 | on: 21 | tags: true 22 | 23 | before_deploy: 24 | - echo Deploying... 25 | 26 | env: 27 | global: 28 | secure: hhFhgyLXEf3CAQdiHhSXi7sS6E0Jo2t7xMz3rExdM8oUH+caKd+V1NOY23JItuljVwXTorOAZyNnvPdAVwwWEGX5haNk3s++YrBaaXb6YAv8isUNgNkthxRwc9h2ILy0R53I3OHrkA9r9DJTPo5hjrmzLtYOQScdxJPt03nKUyo= 29 | -------------------------------------------------------------------------------- /minitest-capybara.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "minitest/capybara/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "minitest-capybara" 7 | s.version = MiniTest::Capybara::VERSION 8 | s.authors = ["Wojciech Mach"] 9 | s.email = ["wojtek@wojtekmach.pl"] 10 | s.homepage = "" 11 | s.summary = %q{Capybara matchers support for minitest unit and spec} 12 | s.description = s.summary 13 | 14 | s.rubyforge_project = "minitest-capybara" 15 | 16 | s.files = `git ls-files`.split("\n") 17 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 18 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 19 | s.require_paths = ["lib"] 20 | 21 | s.add_dependency "capybara" 22 | 23 | s.add_runtime_dependency "rake" 24 | s.add_runtime_dependency "minitest", "~> 5.0" 25 | end 26 | -------------------------------------------------------------------------------- /test/capybara/assertions_test.rb: -------------------------------------------------------------------------------- 1 | require "test_helper" 2 | 3 | describe "Assertions" do 4 | include Capybara::DSL 5 | include Capybara::Assertions 6 | 7 | it "defines all the assertions that Capybara does" do 8 | Minitest::Capybara.assertions.each do |assertion| 9 | assert self.respond_to?("assert_#{assertion}"), 10 | "The assertion assert_#{assertion} is not defined." 11 | end 12 | end 13 | 14 | it "defines all the refutations that Capybara does" do 15 | Minitest::Capybara.refutations.each do |refutation| 16 | assert self.respond_to?("refute_#{refutation}"), 17 | "The assertion refute_#{refutation} is not defined." 18 | end 19 | end 20 | 21 | it "defines all the negative assertions that Capybara does" do 22 | Minitest::Capybara.refutations.each do |refutation| 23 | assert self.respond_to?("assert_no_#{refutation}"), 24 | "The assertion assert_no_#{refutation} is not defined." 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2016 Wojciech Mach 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /lib/minitest/capybara/helpers.rb: -------------------------------------------------------------------------------- 1 | module Minitest 2 | module Capybara 3 | module Helpers 4 | def self.failure_message(description, options={}) 5 | "expected to find #{description}" + count_message(options) 6 | end 7 | 8 | def self.negative_failure_message(description, options={}) 9 | "expected not to find #{description}" + count_message(options) 10 | end 11 | 12 | def self.declension(singular, plural, count) 13 | if count == 1 14 | singular 15 | else 16 | plural 17 | end 18 | end 19 | 20 | def self.count_message(options) 21 | if options[:count] 22 | " #{options[:count]} #{declension('time', 'times', options[:count])}" 23 | elsif options[:between] 24 | " between #{options[:between].first} and #{options[:between].last} times" 25 | elsif options[:maximum] 26 | " at most #{options[:maximum]} #{declension('time', 'times', options[:maximum])}" 27 | elsif options[:minimum] 28 | " at least #{options[:minimum]} #{declension('time', 'times', options[:minimum])}" 29 | else 30 | "" 31 | end 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/minitest/capybara.rb: -------------------------------------------------------------------------------- 1 | require "capybara" 2 | require "capybara/dsl" 3 | require "minitest/capybara/helpers" 4 | require "minitest/capybara/version" 5 | 6 | module Minitest 7 | module Capybara 8 | @@assertions = ::Capybara::Session::NODE_METHODS.grep(/^has_/).map { |s| s.to_s.match(/^has_(.*?)\?/)[1] } 9 | @@refutations = @@assertions.grep(/^no_/) 10 | @@assertions = (@@assertions - @@refutations).sort 11 | @@refutations = @@refutations.map { |s| s.match(/^no_(.*)/)[1] }.sort 12 | 13 | def self.assertions 14 | @@assertions 15 | end 16 | 17 | def self.refutations 18 | @@refutations 19 | end 20 | end 21 | end 22 | 23 | # Need to be required after Minitest::Capybara is defined 24 | require "capybara/assertions" 25 | require "capybara/expectations" 26 | 27 | module Minitest 28 | module Capybara 29 | module Behaviour 30 | include ::Capybara::DSL 31 | include ::Capybara::Assertions 32 | 33 | def teardown 34 | ::Capybara.reset_session! 35 | ::Capybara.use_default_driver 36 | end 37 | end 38 | 39 | class Test < Minitest::Test 40 | include Behaviour 41 | end 42 | 43 | class Spec < Minitest::Spec 44 | include Behaviour 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /test/minitest/base_node_test.rb: -------------------------------------------------------------------------------- 1 | require File.join(File.dirname(__FILE__), '..', 'test_helper') 2 | 3 | describe Capybara do 4 | describe '.string' do 5 | include Capybara::DSL 6 | include Capybara::Assertions 7 | 8 | before do 9 | @test_node= Capybara.string <<-STRING 10 | Hi everybody 11 | 12 | I am a simple e-mail and just want to be tested 13 | 14 | Greetings 15 | 16 | Your friendly tester 17 | STRING 18 | end 19 | 20 | describe "assertions" do 21 | it "can assert_content String" do 22 | assert_content @test_node, "everybody" 23 | end 24 | 25 | it "can assert_content Regexp" do 26 | assert_content @test_node, /simple e-mail/ 27 | assert_content @test_node, /just.*tested/ 28 | end 29 | 30 | it "wont match Regexp in a String" do 31 | proc { assert_content(@test_node, "just.*tested") }.must_raise Minitest::Assertion 32 | end 33 | 34 | it "can refute_content" do 35 | refute_content @test_node, "Everybody" 36 | end 37 | end 38 | 39 | describe "expectations" do 40 | it "supports must_have_content" do 41 | @test_node.must_have_content "everybody" 42 | end 43 | 44 | it "supports must_have_content with regexp" do 45 | @test_node.must_have_content(/simple e-mail/) 46 | @test_node.must_have_content(/just.*tested/) 47 | end 48 | 49 | it "wont have Regexp content in a String" do 50 | proc { @test_node.must_have_content "just.*tested" }.must_raise Minitest::Assertion 51 | end 52 | 53 | it "supports wont_have_content" do 54 | @test_node.wont_have_content "Everybody" 55 | end 56 | end 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /test/minitest/capybara_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | Capybara.app = lambda { |env| [200, {}, "

foo

bar
"] } 4 | 5 | class AppTest < Minitest::Capybara::Spec 6 | before do 7 | visit "/" 8 | end 9 | 10 | # assertions 11 | 12 | it "supports assert_content" do 13 | assert_content("foo").must_equal true 14 | e = proc { assert_content("BAD") }.must_raise Minitest::Assertion 15 | e.message.must_equal "Expected to find text \"BAD\" in \"foobar\"." 16 | end 17 | 18 | it "supports refute_content" do 19 | refute_content("BAD").must_equal true 20 | e = proc { refute_content("foo") }.must_raise Minitest::Assertion 21 | e.message.must_equal "Expected not to find text \"foo\" in \"foobar\"." 22 | end 23 | 24 | it "supports assert_selector" do 25 | assert_selector "h1", text: "foo" 26 | assert_selector :css, "h1", text: "foo" 27 | proc { assert_selector "h1", text: "BAD" }.must_raise Minitest::Assertion 28 | proc { assert_selector :css, "h1", text: "BAD" }.must_raise Minitest::Assertion 29 | end 30 | 31 | it "supports refute_selector" do 32 | refute_selector "h1", text: "BAD" 33 | proc { refute_selector "h1", text: "foo" }.must_raise Minitest::Assertion 34 | end 35 | 36 | it "supports assert_link" do 37 | assert_link 'bar' 38 | e = proc { assert_link("BAD") }.must_raise Minitest::Assertion 39 | e.message.must_equal "expected to find BAD." 40 | end 41 | 42 | it "supports refute_link" do 43 | refute_link 'BAD' 44 | e = proc { refute_link("bar") }.must_raise Minitest::Assertion 45 | e.message.must_equal "expected not to find bar." 46 | end 47 | 48 | # expectations 49 | 50 | it "supports must_have_content" do 51 | page.must_have_content "foo" 52 | proc { page.must_have_content "BAD" }.must_raise Minitest::Assertion 53 | end 54 | 55 | it "supports wont_have_content" do 56 | page.wont_have_content "BAD" 57 | proc { page.wont_have_content "foo" }.must_raise Minitest::Assertion 58 | end 59 | 60 | it "supports must_have_selector" do 61 | page.must_have_selector "h1", text: "foo" 62 | page.must_have_selector :css, "h1", text: "foo" 63 | proc { page.must_have_selector "h1", text: "BAD" }.must_raise Minitest::Assertion 64 | proc { page.must_have_selector :css, "h1", text: "BAD" }.must_raise Minitest::Assertion 65 | end 66 | 67 | it "supports wont_have_selector" do 68 | page.wont_have_selector "h1", text: "BAD" 69 | proc { page.wont_have_selector "h1", text: "foo" }.must_raise Minitest::Assertion 70 | end 71 | 72 | it "supports must_have_link" do 73 | page.must_have_link 'bar' 74 | proc { page.must_have_link("BAD") }.must_raise Minitest::Assertion 75 | end 76 | 77 | it "supports wont_have_link" do 78 | page.wont_have_link 'BAD' 79 | proc { page.wont_have_link("bar") }.must_raise Minitest::Assertion 80 | end 81 | end 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # minitest-capybara 2 | 3 | [![Build Status](https://secure.travis-ci.org/wojtekmach/minitest-capybara.png?branch=master)](http://travis-ci.org/wojtekmach/minitest-capybara) 4 | 5 | ## :warning: This gem is no longer maintained as Capybara has added Minitest support upstream, use that instead. 6 | 7 | Capybara matchers support for Minitest unit & spec. 8 | 9 | ## Why? 10 | 11 | Capybara has good support for RSpec. 12 | 13 | If you want to use it with Minitest, you can of course write: 14 | 15 | ```ruby 16 | assert page.has_content?("Hello") 17 | ``` 18 | 19 | but: 20 | 21 | 1. it's kinda ugly 22 | 2. you don't have meaningfull error messages. 23 | 24 | With this project Minitest gets all the good stuff. 25 | 26 | ## Rails integration 27 | 28 | `minitest-capybara` (and `capybara`) works with Rails out of the box, remember to require `capybara/rails`. 29 | 30 | See example Rails app: . 31 | 32 | For more features, check out: [minitest-rails-capybara](https://github.com/blowmage/minitest-rails-capybara). 33 | 34 | ## Usage 35 | 36 | With minitest/test: 37 | 38 | ```ruby 39 | class HomeTest < Minitest::Capybara::Test 40 | def test_home 41 | visit "/" 42 | 43 | assert_content "Homepage" 44 | 45 | within ".login" do 46 | refute_content "Signed in as" 47 | end 48 | 49 | assert_link "Sign in" 50 | assert_link find(".login"), "Sign in" 51 | 52 | assert_selector 'li:first', text: "Item 1" 53 | end 54 | end 55 | ``` 56 | 57 | With minitest/spec: 58 | 59 | ```ruby 60 | class HomeSpec < Minitest::Capybara::Spec 61 | it "works" do 62 | visit "/" 63 | 64 | page.must_have_content "Homepage" 65 | 66 | within ".login" do 67 | page.wont_have_content "Signed in as" 68 | end 69 | 70 | find(".login").must_have_link("Sign in") 71 | 72 | page.must_have_selector 'li:first', text: "Item 1" 73 | end 74 | end 75 | ``` 76 | 77 | Instead of inheriting directly from Minitest::Capybara::Test (or Spec) it's usually better to create a custom test base class: 78 | 79 | ```ruby 80 | # test/acceptance_test_helper.rb 81 | require "minitest/autorun" 82 | 83 | class AcceptanceTest < Minitest::Capybara::Test 84 | # custom methods, before blocks etc. 85 | end 86 | ``` 87 | 88 | If you need to inherit from a different base class (e.g. `ActiveSupport::TestCase`) you can do this instead: 89 | 90 | ```ruby 91 | # test/acceptance_test_helper.rb 92 | require "test_helper" 93 | 94 | class AcceptanceTest < ActiveSupport::TestCase 95 | include Minitest::Capybara::Behaviour 96 | 97 | # custom methods, before blocks etc. 98 | end 99 | ``` 100 | 101 | ## Capybara drivers 102 | 103 | Switching drivers is easy with [minitest-metadata]: 104 | 105 | [minitest-metadata]: https://github.com/wojtekmach/minitest-metadata 106 | 107 | ```ruby 108 | require 'minitest-metadata' 109 | 110 | class AcceptanceSpec < Minitest::Capybara::Spec 111 | before do 112 | if metadata[:js] 113 | Capybara.current_driver = Capybara.javascript_driver 114 | else 115 | Capybara.current_driver = Capybara.default_driver 116 | end 117 | end 118 | end 119 | 120 | class HomeSpec < AcceptanceSpec 121 | it "home with ajax", js: true do 122 | visit "/" 123 | page.must_have_content "AJAX enabled..." 124 | end 125 | end 126 | ``` 127 | 128 | ## License 129 | 130 | minitest-capybara is released under the [MIT License](LICENSE.txt). 131 | -------------------------------------------------------------------------------- /lib/capybara/expectations.rb: -------------------------------------------------------------------------------- 1 | require "minitest/spec" 2 | 3 | module Capybara 4 | module Expectations 5 | Minitest::Capybara.assertions.each do |assertion| 6 | infect_an_assertion "assert_#{assertion}", "must_have_#{assertion}", :reverse 7 | end 8 | 9 | Minitest::Capybara.refutations.each do |refutation| 10 | infect_an_assertion "refute_#{refutation}", "wont_have_#{refutation}", :reverse 11 | end 12 | 13 | ## 14 | # Expectation that there is button 15 | # 16 | # see Capybara::Expectations#wont_have_button 17 | # see Capybara::Assertions#assert_button 18 | # see Capybara::Assertions#refute_button 19 | # see Capybara::Assertions#assert_no_button 20 | # :method: must_have_button 21 | 22 | ## 23 | # Expectation that there is no button 24 | # 25 | # see Capybara::Expectations#must_have_button 26 | # see Capybara::Assertions#assert_button 27 | # see Capybara::Assertions#refute_button 28 | # see Capybara::Assertions#assert_no_button 29 | # :method: wont_have_button 30 | 31 | 32 | ## 33 | # Expectation that there is checked_field 34 | # 35 | # see Capybara::Expectations#wont_have_checked_field 36 | # see Capybara::Assertions#assert_checked_field 37 | # see Capybara::Assertions#refute_checked_field 38 | # see Capybara::Assertions#assert_no_checked_field 39 | # :method: must_have_checked_field 40 | 41 | ## 42 | # Expectation that there is no checked_field 43 | # 44 | # see Capybara::Expectations#must_have_checked_field 45 | # see Capybara::Assertions#assert_checked_field 46 | # see Capybara::Assertions#refute_checked_field 47 | # see Capybara::Assertions#assert_no_checked_field 48 | # :method: wont_have_checked_field 49 | 50 | 51 | ## 52 | # Expectation that there is content 53 | # 54 | # see Capybara::Expectations#wont_have_content 55 | # see Capybara::Assertions#assert_content 56 | # see Capybara::Assertions#refute_content 57 | # see Capybara::Assertions#assert_no_content 58 | # :method: must_have_content 59 | 60 | ## 61 | # Expectation that there is no content 62 | # 63 | # see Capybara::Expectations#must_have_content 64 | # see Capybara::Assertions#assert_content 65 | # see Capybara::Assertions#refute_content 66 | # see Capybara::Assertions#assert_no_content 67 | # :method: wont_have_content 68 | 69 | 70 | ## 71 | # Expectation that there is css 72 | # 73 | # see Capybara::Expectations#wont_have_css 74 | # see Capybara::Assertions#assert_css 75 | # see Capybara::Assertions#refute_css 76 | # see Capybara::Assertions#assert_no_css 77 | # :method: must_have_css 78 | 79 | ## 80 | # Expectation that there is no css 81 | # 82 | # see Capybara::Expectations#must_have_css 83 | # see Capybara::Assertions#assert_css 84 | # see Capybara::Assertions#refute_css 85 | # see Capybara::Assertions#assert_no_css 86 | # :method: wont_have_css 87 | 88 | 89 | ## 90 | # Expectation that there is field 91 | # 92 | # see Capybara::Expectations#wont_have_field 93 | # see Capybara::Assertions#assert_field 94 | # see Capybara::Assertions#refute_field 95 | # see Capybara::Assertions#assert_no_field 96 | # :method: must_have_field 97 | 98 | ## 99 | # Expectation that there is no field 100 | # 101 | # see Capybara::Expectations#must_have_field 102 | # see Capybara::Assertions#assert_field 103 | # see Capybara::Assertions#refute_field 104 | # see Capybara::Assertions#assert_no_field 105 | # :method: wont_have_field 106 | 107 | 108 | ## 109 | # Expectation that there is link 110 | # 111 | # see Capybara::Expectations#wont_have_link 112 | # see Capybara::Assertions#assert_link 113 | # see Capybara::Assertions#refute_link 114 | # see Capybara::Assertions#assert_no_link 115 | # :method: must_have_link 116 | 117 | ## 118 | # Expectation that there is no link 119 | # 120 | # see Capybara::Expectations#must_have_link 121 | # see Capybara::Assertions#assert_link 122 | # see Capybara::Assertions#refute_link 123 | # see Capybara::Assertions#assert_no_link 124 | # :method: wont_have_link 125 | 126 | 127 | ## 128 | # Expectation that there is select 129 | # 130 | # see Capybara::Expectations#wont_have_select 131 | # see Capybara::Assertions#assert_select 132 | # see Capybara::Assertions#refute_select 133 | # see Capybara::Assertions#assert_no_select 134 | # :method: must_have_select 135 | 136 | ## 137 | # Expectation that there is no select 138 | # 139 | # see Capybara::Expectations#must_have_select 140 | # see Capybara::Assertions#assert_select 141 | # see Capybara::Assertions#refute_select 142 | # see Capybara::Assertions#assert_no_select 143 | # :method: wont_have_select 144 | 145 | 146 | ## 147 | # Expectation that there is selector 148 | # 149 | # see Capybara::Expectations#wont_have_selector 150 | # see Capybara::Assertions#assert_selector 151 | # see Capybara::Assertions#refute_selector 152 | # see Capybara::Assertions#assert_no_selector 153 | # :method: must_have_selector 154 | 155 | ## 156 | # Expectation that there is no selector 157 | # 158 | # see Capybara::Expectations#must_have_selector 159 | # see Capybara::Assertions#assert_selector 160 | # see Capybara::Assertions#refute_selector 161 | # see Capybara::Assertions#assert_no_selector 162 | # :method: wont_have_selector 163 | 164 | 165 | ## 166 | # Expectation that there is table 167 | # 168 | # see Capybara::Expectations#wont_have_table 169 | # see Capybara::Assertions#assert_table 170 | # see Capybara::Assertions#refute_table 171 | # see Capybara::Assertions#assert_no_table 172 | # :method: must_have_table 173 | 174 | ## 175 | # Expectation that there is no table 176 | # 177 | # see Capybara::Expectations#must_have_table 178 | # see Capybara::Assertions#assert_table 179 | # see Capybara::Assertions#refute_table 180 | # see Capybara::Assertions#assert_no_table 181 | # :method: wont_have_table 182 | 183 | 184 | ## 185 | # Expectation that there is text 186 | # 187 | # see Capybara::Expectations#wont_have_text 188 | # see Capybara::Assertions#assert_text 189 | # see Capybara::Assertions#refute_text 190 | # see Capybara::Assertions#assert_no_text 191 | # :method: must_have_text 192 | 193 | ## 194 | # Expectation that there is no text 195 | # 196 | # see Capybara::Expectations#must_have_text 197 | # see Capybara::Assertions#assert_text 198 | # see Capybara::Assertions#refute_text 199 | # see Capybara::Assertions#assert_no_text 200 | # :method: wont_have_text 201 | 202 | 203 | ## 204 | # Expectation that there is unchecked_field 205 | # 206 | # see Capybara::Expectations#wont_have_unchecked_field 207 | # see Capybara::Assertions#assert_unchecked_field 208 | # see Capybara::Assertions#refute_unchecked_field 209 | # see Capybara::Assertions#assert_no_unchecked_field 210 | # :method: must_have_unchecked_field 211 | 212 | ## 213 | # Expectation that there is no unchecked_field 214 | # 215 | # see Capybara::Expectations#must_have_unchecked_field 216 | # see Capybara::Assertions#assert_unchecked_field 217 | # see Capybara::Assertions#refute_unchecked_field 218 | # see Capybara::Assertions#assert_no_unchecked_field 219 | # :method: wont_have_unchecked_field 220 | 221 | 222 | ## 223 | # Expectation that there is xpath 224 | # 225 | # see Capybara::Expectations#wont_have_xpath 226 | # see Capybara::Assertions#assert_xpath 227 | # see Capybara::Assertions#refute_xpath 228 | # see Capybara::Assertions#assert_no_xpath 229 | # :method: must_have_xpath 230 | 231 | ## 232 | # Expectation that there is no xpath 233 | # 234 | # see Capybara::Expectations#must_have_xpath 235 | # see Capybara::Assertions#assert_xpath 236 | # see Capybara::Assertions#refute_xpath 237 | # see Capybara::Assertions#assert_no_xpath 238 | # :method: wont_have_xpath 239 | end 240 | end 241 | 242 | class Capybara::Session 243 | include Capybara::Expectations unless ENV["MT_NO_EXPECTATIONS"] 244 | end 245 | 246 | class Capybara::Node::Base 247 | include Capybara::Expectations unless ENV["MT_NO_EXPECTATIONS"] 248 | end 249 | 250 | class Capybara::Node::Simple 251 | include Capybara::Expectations unless ENV["MT_NO_EXPECTATIONS"] 252 | end 253 | -------------------------------------------------------------------------------- /lib/capybara/assertions.rb: -------------------------------------------------------------------------------- 1 | module Capybara 2 | module Assertions 3 | def self.included(base) 4 | raise "Make sure to include Capybara::Assertions after Capybara::DSL" unless base < Capybara::DSL 5 | end 6 | 7 | def assert_text(*args) 8 | node, *args = prepare_args(args) 9 | assert node.has_text?(*args), message { "Expected to find text #{args.first.inspect} in #{node.text.inspect}" } 10 | end 11 | alias_method :assert_content, :assert_text 12 | 13 | def refute_text(*args) 14 | node, *args = prepare_args(args) 15 | assert node.has_no_text?(*args), message { "Expected not to find text #{args.first.inspect} in #{node.text.inspect}" } 16 | end 17 | alias_method :assert_no_text, :refute_text 18 | alias_method :refute_content, :refute_text 19 | alias_method :assert_no_content, :refute_text 20 | 21 | def assert_selector(*args) 22 | node, *args = prepare_args(args) 23 | assert node.assert_selector(*args) 24 | rescue Capybara::ExpectationNotMet => e 25 | assert false, e.message 26 | end 27 | 28 | def refute_selector(*args) 29 | node, *args = prepare_args(args) 30 | assert node.assert_no_selector(*args) 31 | rescue Capybara::ExpectationNotMet => e 32 | assert false, e.message 33 | end 34 | alias_method :assert_no_selector, :refute_selector 35 | 36 | ruby = "" 37 | (Minitest::Capybara.assertions - %w[text content selector]).each do |assertion| 38 | ruby << <<-RUBY 39 | def assert_#{assertion}(*args) 40 | node, *args = prepare_args(args) 41 | assert node.has_#{assertion}?(*args), message { Minitest::Capybara::Helpers.failure_message(*args) } 42 | end 43 | RUBY 44 | end 45 | (Minitest::Capybara.refutations - %w[text content selector]).each do |refutation| 46 | ruby << <<-RUBY 47 | def refute_#{refutation}(*args) 48 | node, *args = prepare_args(args) 49 | assert node.has_no_#{refutation}?(*args), message { Minitest::Capybara::Helpers.negative_failure_message(*args) } 50 | end 51 | alias_method :assert_no_#{refutation}, :refute_#{refutation} 52 | RUBY 53 | end 54 | class_eval(ruby) 55 | 56 | ## 57 | # Assertion that there is button 58 | # 59 | # see Capybara::Assertions#refute_button 60 | # see Capybara::Assertions#assert_no_button 61 | # see Capybara::expectations#must_have_button 62 | # see Capybara::expectations#wont_have_button 63 | # :method: assert_button 64 | 65 | ## 66 | # Assertion that there is no button 67 | # 68 | # see Capybara::Assertions#assert_button 69 | # see Capybara::expectations#must_have_button 70 | # see Capybara::expectations#wont_have_button 71 | # :method: refute_button 72 | # :alias: assert_no_button 73 | 74 | 75 | ## 76 | # Assertion that there is checked_field 77 | # 78 | # see Capybara::Assertions#refute_checked_field 79 | # see Capybara::Assertions#assert_no_checked_field 80 | # see Capybara::expectations#must_have_checked_field 81 | # see Capybara::expectations#wont_have_checked_field 82 | # :method: assert_checked_field 83 | 84 | ## 85 | # Assertion that there is no checked_field 86 | # 87 | # see Capybara::Assertions#assert_checked_field 88 | # see Capybara::expectations#must_have_checked_field 89 | # see Capybara::expectations#wont_have_checked_field 90 | # :method: refute_checked_field 91 | # :alias: assert_no_checked_field 92 | 93 | 94 | ## 95 | # Assertion that there is content 96 | # 97 | # see Capybara::Assertions#refute_content 98 | # see Capybara::Assertions#assert_no_content 99 | # see Capybara::expectations#must_have_content 100 | # see Capybara::expectations#wont_have_content 101 | # :method: assert_content 102 | 103 | ## 104 | # Assertion that there is no content 105 | # 106 | # see Capybara::Assertions#assert_content 107 | # see Capybara::expectations#must_have_content 108 | # see Capybara::expectations#wont_have_content 109 | # :method: refute_content 110 | # :alias: assert_no_content 111 | 112 | 113 | ## 114 | # Assertion that there is css 115 | # 116 | # see Capybara::Assertions#refute_css 117 | # see Capybara::Assertions#assert_no_css 118 | # see Capybara::expectations#must_have_css 119 | # see Capybara::expectations#wont_have_css 120 | # :method: assert_css 121 | 122 | ## 123 | # Assertion that there is no css 124 | # 125 | # see Capybara::Assertions#assert_css 126 | # see Capybara::expectations#must_have_css 127 | # see Capybara::expectations#wont_have_css 128 | # :method: refute_css 129 | # :alias: assert_no_css 130 | 131 | 132 | ## 133 | # Assertion that there is field 134 | # 135 | # see Capybara::Assertions#refute_field 136 | # see Capybara::Assertions#assert_no_field 137 | # see Capybara::expectations#must_have_field 138 | # see Capybara::expectations#wont_have_field 139 | # :method: assert_field 140 | 141 | ## 142 | # Assertion that there is no field 143 | # 144 | # see Capybara::Assertions#assert_field 145 | # see Capybara::expectations#must_have_field 146 | # see Capybara::expectations#wont_have_field 147 | # :method: refute_field 148 | # :alias: assert_no_field 149 | 150 | 151 | ## 152 | # Assertion that there is link 153 | # 154 | # see Capybara::Assertions#refute_link 155 | # see Capybara::Assertions#assert_no_link 156 | # see Capybara::expectations#must_have_link 157 | # see Capybara::expectations#wont_have_link 158 | # :method: assert_link 159 | 160 | ## 161 | # Assertion that there is no link 162 | # 163 | # see Capybara::Assertions#assert_link 164 | # see Capybara::expectations#must_have_link 165 | # see Capybara::expectations#wont_have_link 166 | # :method: refute_link 167 | # :alias: assert_no_link 168 | 169 | 170 | ## 171 | # Assertion that there is select 172 | # 173 | # see Capybara::Assertions#refute_select 174 | # see Capybara::Assertions#assert_no_select 175 | # see Capybara::expectations#must_have_select 176 | # see Capybara::expectations#wont_have_select 177 | # :method: assert_select 178 | 179 | ## 180 | # Assertion that there is no select 181 | # 182 | # see Capybara::Assertions#assert_select 183 | # see Capybara::expectations#must_have_select 184 | # see Capybara::expectations#wont_have_select 185 | # :method: refute_select 186 | # :alias: assert_no_select 187 | 188 | 189 | ## 190 | # Assertion that there is selector 191 | # 192 | # see Capybara::Assertions#refute_selector 193 | # see Capybara::Assertions#assert_no_selector 194 | # see Capybara::expectations#must_have_selector 195 | # see Capybara::expectations#wont_have_selector 196 | # :method: assert_selector 197 | 198 | ## 199 | # Assertion that there is no selector 200 | # 201 | # see Capybara::Assertions#assert_selector 202 | # see Capybara::expectations#must_have_selector 203 | # see Capybara::expectations#wont_have_selector 204 | # :method: refute_selector 205 | # :alias: assert_no_selector 206 | 207 | 208 | ## 209 | # Assertion that there is table 210 | # 211 | # see Capybara::Assertions#refute_table 212 | # see Capybara::Assertions#assert_no_table 213 | # see Capybara::expectations#must_have_table 214 | # see Capybara::expectations#wont_have_table 215 | # :method: assert_table 216 | 217 | ## 218 | # Assertion that there is no table 219 | # 220 | # see Capybara::Assertions#assert_table 221 | # see Capybara::expectations#must_have_table 222 | # see Capybara::expectations#wont_have_table 223 | # :method: refute_table 224 | # :alias: assert_no_table 225 | 226 | 227 | ## 228 | # Assertion that there is text 229 | # 230 | # see Capybara::Assertions#refute_text 231 | # see Capybara::Assertions#assert_no_text 232 | # see Capybara::expectations#must_have_text 233 | # see Capybara::expectations#wont_have_text 234 | # :method: assert_text 235 | 236 | ## 237 | # Assertion that there is no text 238 | # 239 | # see Capybara::Assertions#assert_text 240 | # see Capybara::expectations#must_have_text 241 | # see Capybara::expectations#wont_have_text 242 | # :method: refute_text 243 | # :alias: assert_no_text 244 | 245 | 246 | ## 247 | # Assertion that there is unchecked_field 248 | # 249 | # see Capybara::Assertions#refute_unchecked_field 250 | # see Capybara::Assertions#assert_no_unchecked_field 251 | # see Capybara::expectations#must_have_unchecked_field 252 | # see Capybara::expectations#wont_have_unchecked_field 253 | # :method: assert_unchecked_field 254 | 255 | ## 256 | # Assertion that there is no unchecked_field 257 | # 258 | # see Capybara::Assertions#assert_unchecked_field 259 | # see Capybara::expectations#must_have_unchecked_field 260 | # see Capybara::expectations#wont_have_unchecked_field 261 | # :method: refute_unchecked_field 262 | # :alias: assert_no_unchecked_field 263 | 264 | 265 | ## 266 | # Assertion that there is xpath 267 | # 268 | # see Capybara::Assertions#refute_xpath 269 | # see Capybara::Assertions#assert_no_xpath 270 | # see Capybara::expectations#must_have_xpath 271 | # see Capybara::expectations#wont_have_xpath 272 | # :method: assert_xpath 273 | 274 | ## 275 | # Assertion that there is no xpath 276 | # 277 | # see Capybara::Assertions#assert_xpath 278 | # see Capybara::expectations#must_have_xpath 279 | # see Capybara::expectations#wont_have_xpath 280 | # :method: refute_xpath 281 | # :alias: assert_no_xpath 282 | 283 | private 284 | 285 | def prepare_args(args) 286 | if args.first.is_a?(Capybara::Session) || args.first.kind_of?(Capybara::Node::Base) || 287 | args.first.is_a?(Capybara::Node::Simple) 288 | args 289 | else 290 | [page, *args] 291 | end 292 | end 293 | end 294 | end 295 | --------------------------------------------------------------------------------