File: matchers.rb

package info (click to toggle)
ruby-capybara 3.40.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: ruby: 23,988; javascript: 752; makefile: 11
file content (207 lines) | stat: -rw-r--r-- 7,426 bytes parent folder | download
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
# frozen_string_literal: true

require 'capybara/rspec/matchers/have_selector'
require 'capybara/rspec/matchers/have_ancestor'
require 'capybara/rspec/matchers/have_sibling'
require 'capybara/rspec/matchers/match_selector'
require 'capybara/rspec/matchers/have_current_path'
require 'capybara/rspec/matchers/match_style'
require 'capybara/rspec/matchers/have_text'
require 'capybara/rspec/matchers/have_title'
require 'capybara/rspec/matchers/become_closed'

module Capybara
  module RSpecMatchers
    # RSpec matcher for whether the element(s) matching a given selector exist.
    #
    # @see Capybara::Node::Matchers#assert_selector
    def have_selector(...)
      Matchers::HaveSelector.new(...)
    end

    # RSpec matcher for whether the element(s) matching a group of selectors exist.
    #
    # @see Capybara::Node::Matchers#assert_all_of_selectors
    def have_all_of_selectors(...)
      Matchers::HaveAllSelectors.new(...)
    end

    # RSpec matcher for whether no element(s) matching a group of selectors exist.
    #
    # @see Capybara::Node::Matchers#assert_none_of_selectors
    def have_none_of_selectors(...)
      Matchers::HaveNoSelectors.new(...)
    end

    # RSpec matcher for whether the element(s) matching any of a group of selectors exist.
    #
    # @see Capybara::Node::Matchers#assert_any_of_selectors
    def have_any_of_selectors(...)
      Matchers::HaveAnySelectors.new(...)
    end

    # RSpec matcher for whether the current element matches a given selector.
    #
    # @see Capybara::Node::Matchers#assert_matches_selector
    def match_selector(...)
      Matchers::MatchSelector.new(...)
    end

    %i[css xpath].each do |selector|
      define_method "have_#{selector}" do |expr, **options, &optional_filter_block|
        Matchers::HaveSelector.new(selector, expr, **options, &optional_filter_block)
      end

      define_method "match_#{selector}" do |expr, **options, &optional_filter_block|
        Matchers::MatchSelector.new(selector, expr, **options, &optional_filter_block)
      end
    end

    # @!method have_xpath(xpath, **options, &optional_filter_block)
    #   RSpec matcher for whether elements(s) matching a given xpath selector exist.
    #
    #   @see Capybara::Node::Matchers#has_xpath?

    # @!method have_css(css, **options, &optional_filter_block)
    #   RSpec matcher for whether elements(s) matching a given css selector exist
    #
    #   @see Capybara::Node::Matchers#has_css?

    # @!method match_xpath(xpath, **options, &optional_filter_block)
    #   RSpec matcher for whether the current element matches a given xpath selector.
    #
    #   @see Capybara::Node::Matchers#matches_xpath?

    # @!method match_css(css, **options, &optional_filter_block)
    #   RSpec matcher for whether the current element matches a given css selector.
    #
    #   @see Capybara::Node::Matchers#matches_css?

    %i[link button field select table element].each do |selector|
      define_method "have_#{selector}" do |locator = nil, **options, &optional_filter_block|
        Matchers::HaveSelector.new(selector, locator, **options, &optional_filter_block)
      end
    end

    # @!method have_element(locator = nil, **options, &optional_filter_block)
    #   RSpec matcher for elements.
    #
    #   @see Capybara::Node::Matchers#has_element?

    # @!method have_link(locator = nil, **options, &optional_filter_block)
    #   RSpec matcher for links.
    #
    #   @see Capybara::Node::Matchers#has_link?

    # @!method have_button(locator = nil, **options, &optional_filter_block)
    #   RSpec matcher for buttons.
    #
    #   @see Capybara::Node::Matchers#has_button?

    # @!method have_field(locator = nil, **options, &optional_filter_block)
    #   RSpec matcher for form fields.
    #
    #   @see Capybara::Node::Matchers#has_field?

    # @!method have_select(locator = nil, **options, &optional_filter_block)
    #   RSpec matcher for select elements.
    #
    #   @see Capybara::Node::Matchers#has_select?

    # @!method have_table(locator = nil, **options, &optional_filter_block)
    #   RSpec matcher for table elements.
    #
    #   @see Capybara::Node::Matchers#has_table?

    %i[checked unchecked].each do |state|
      define_method "have_#{state}_field" do |locator = nil, **options, &optional_filter_block|
        Matchers::HaveSelector.new(:field, locator, **options.merge(state => true), &optional_filter_block)
      end
    end

    # @!method have_checked_field(locator = nil, **options, &optional_filter_block)
    #   RSpec matcher for checked fields.
    #
    #   @see Capybara::Node::Matchers#has_checked_field?

    # @!method have_unchecked_field(locator = nil, **options, &optional_filter_block)
    #   RSpec matcher for unchecked fields.
    #
    #   @see Capybara::Node::Matchers#has_unchecked_field?

    # RSpec matcher for text content.
    #
    # @see Capybara::Node::Matchers#assert_text
    def have_text(text_or_type, *args, **options)
      Matchers::HaveText.new(text_or_type, *args, **options)
    end
    alias_method :have_content, :have_text

    def have_title(title, **options)
      Matchers::HaveTitle.new(title, **options)
    end

    # RSpec matcher for the current path.
    #
    # @see Capybara::SessionMatchers#assert_current_path
    def have_current_path(path, **options, &optional_filter_block)
      Matchers::HaveCurrentPath.new(path, **options, &optional_filter_block)
    end

    # RSpec matcher for element style.
    #
    # @see Capybara::Node::Matchers#matches_style?
    def match_style(styles = nil, **options)
      styles, options = options, {} if styles.nil?
      Matchers::MatchStyle.new(styles, **options)
    end

    ##
    # @deprecated
    #
    def have_style(styles = nil, **options)
      Capybara::Helpers.warn "DEPRECATED: have_style is deprecated, please use match_style : #{Capybara::Helpers.filter_backtrace(caller)}"
      match_style(styles, **options)
    end

    %w[selector css xpath text title current_path link button
       field checked_field unchecked_field select table
       sibling ancestor element].each do |matcher_type|
      define_method "have_no_#{matcher_type}" do |*args, **kw_args, &optional_filter_block|
        Matchers::NegatedMatcher.new(send("have_#{matcher_type}", *args, **kw_args, &optional_filter_block))
      end
    end
    alias_method :have_no_content, :have_no_text

    %w[selector css xpath].each do |matcher_type|
      define_method "not_match_#{matcher_type}" do |*args, **kw_args, &optional_filter_block|
        Matchers::NegatedMatcher.new(send("match_#{matcher_type}", *args, **kw_args, &optional_filter_block))
      end
    end

    # RSpec matcher for whether sibling element(s) matching a given selector exist.
    #
    # @see Capybara::Node::Matchers#assert_sibling
    def have_sibling(...)
      Matchers::HaveSibling.new(...)
    end

    # RSpec matcher for whether ancestor element(s) matching a given selector exist.
    #
    # @see Capybara::Node::Matchers#assert_ancestor
    def have_ancestor(...)
      Matchers::HaveAncestor.new(...)
    end

    ##
    # Wait for window to become closed.
    #
    # @example
    #   expect(window).to become_closed(wait: 0.8)
    #
    # @option options [Numeric] :wait   Maximum wait time. Defaults to {Capybara.configure default_max_wait_time}
    def become_closed(**options)
      Matchers::BecomeClosed.new(options)
    end
  end
end