/usr/lib/ruby/vendor_ruby/capybara/rspec/matchers.rb is in ruby-capybara 2.10.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | # frozen_string_literal: true
module Capybara
module RSpecMatchers
class Matcher
include ::RSpec::Matchers::Composable if defined?(::RSpec::Expectations::Version) && (Gem::Version.new(RSpec::Expectations::Version::STRING) >= Gem::Version.new('3.0'))
def wrap(actual)
if actual.respond_to?("has_selector?")
actual
else
Capybara.string(actual.to_s)
end
end
end
class HaveSelector < Matcher
attr_reader :failure_message, :failure_message_when_negated
def initialize(*args, &filter_block)
@args = args
@filter_block = filter_block
end
def matches?(actual)
wrap(actual).assert_selector(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
end
def does_not_match?(actual)
wrap(actual).assert_no_selector(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
end
def description
"have #{query.description}"
end
def query
@query ||= Capybara::Queries::SelectorQuery.new(*@args, &@filter_block)
end
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
end
class HaveText < Matcher
attr_reader :type, :content, :options
attr_reader :failure_message, :failure_message_when_negated
def initialize(*args)
@args = args.dup
# are set just for backwards compatability
@type = args.shift if args.first.is_a?(Symbol)
@content = args.shift
@options = (args.first.is_a?(Hash))? args.first : {}
end
def matches?(actual)
wrap(actual).assert_text(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
end
def does_not_match?(actual)
wrap(actual).assert_no_text(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
end
def description
"text #{format(content)}"
end
def format(content)
content = Capybara::Helpers.normalize_whitespace(content) unless content.is_a? Regexp
content.inspect
end
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
end
class HaveTitle < Matcher
attr_reader :title
attr_reader :failure_message, :failure_message_when_negated
def initialize(*args)
@args = args
# are set just for backwards compatability
@title = args.first
end
def matches?(actual)
wrap(actual).assert_title(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
end
def does_not_match?(actual)
wrap(actual).assert_no_title(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
end
def description
"have title #{title.inspect}"
end
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
end
class HaveCurrentPath < Matcher
attr_reader :current_path
attr_reader :failure_message, :failure_message_when_negated
def initialize(*args)
@args = args
# are set just for backwards compatability
@current_path = args.first
end
def matches?(actual)
wrap(actual).assert_current_path(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
end
def does_not_match?(actual)
wrap(actual).assert_no_current_path(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
end
def description
"have current path #{current_path.inspect}"
end
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
end
class BecomeClosed
def initialize(options)
@wait_time = Capybara::Queries::BaseQuery.wait(options)
end
def matches?(window)
@window = window
start_time = Capybara::Helpers.monotonic_time
while window.exists?
return false if (Capybara::Helpers.monotonic_time - start_time) > @wait_time
sleep 0.05
end
true
end
def failure_message
"expected #{@window.inspect} to become closed after #{@wait_time} seconds"
end
def failure_message_when_negated
"expected #{@window.inspect} not to become closed after #{@wait_time} seconds"
end
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
end
class MatchSelector < Matcher
attr_reader :failure_message, :failure_message_when_negated
def initialize(*args)
@args = args
end
def matches?(actual)
actual.assert_matches_selector(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
end
def does_not_match?(actual)
actual.assert_not_matches_selector(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
end
def description
"match #{query.description}"
end
def query
@query ||= Capybara::Queries::MatchQuery.new(*@args)
end
# RSpec 2 compatibility:
alias_method :failure_message_for_should, :failure_message
alias_method :failure_message_for_should_not, :failure_message_when_negated
end
def have_selector(*args, &optional_filter_block)
HaveSelector.new(*args, &optional_filter_block)
end
def match_selector(*args)
MatchSelector.new(*args)
end
# defined_negated_matcher was added in RSpec 3.1 - it's syntactic sugar only since a user can do
# expect(page).not_to match_selector, so not sure we really need to support not_match_selector for prior to RSpec 3.1
::RSpec::Matchers.define_negated_matcher :not_match_selector, :match_selector if defined?(::RSpec::Expectations::Version) && (Gem::Version.new(RSpec::Expectations::Version::STRING) >= Gem::Version.new('3.1'))
def have_xpath(xpath, options={}, &optional_filter_block)
HaveSelector.new(:xpath, xpath, options, &optional_filter_block)
end
def match_xpath(xpath, options={})
MatchSelector.new(:xpath, xpath, options)
end
def have_css(css, options={}, &optional_filter_block)
HaveSelector.new(:css, css, options, &optional_filter_block)
end
def match_css(css, options={})
MatchSelector.new(:css, css, options)
end
def have_text(*args)
HaveText.new(*args)
end
alias_method :have_content, :have_text
def have_title(title, options = {})
HaveTitle.new(title, options)
end
def have_current_path(path, options = {})
HaveCurrentPath.new(path, options)
end
def have_link(locator=nil, options={}, &optional_filter_block)
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:link, locator, options, &optional_filter_block)
end
def have_button(locator=nil, options={}, &optional_filter_block)
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:button, locator, options, &optional_filter_block)
end
def have_field(locator=nil, options={}, &optional_filter_block)
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:field, locator, options, &optional_filter_block)
end
def have_checked_field(locator=nil, options={}, &optional_filter_block)
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:field, locator, options.merge(checked: true), &optional_filter_block)
end
def have_unchecked_field(locator=nil, options={}, &optional_filter_block)
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:field, locator, options.merge(unchecked: true), &optional_filter_block)
end
def have_select(locator=nil, options={}, &optional_filter_block)
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:select, locator, options, &optional_filter_block)
end
def have_table(locator=nil, options={}, &optional_filter_block)
locator, options = nil, locator if locator.is_a? Hash
HaveSelector.new(:table, locator, options, &optional_filter_block)
end
##
# Wait for window to become closed.
# @example
# expect(window).to become_closed(wait: 0.8)
# @param options [Hash] optional param
# @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum wait time
def become_closed(options = {})
BecomeClosed.new(options)
end
end
end
|