Top Level Namespace

Defined Under Namespace

Modules: Capybara, XPath Classes: TestApp

Instance Method Summary collapse

Instance Method Details

#buttonObject

Find buttons ( input [of type submit, reset, image, button] or button elements )



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
# File 'lib/capybara/selector.rb', line 187

Capybara.add_selector(:button) do
  xpath(:value, :title) do |locator, **options|
    input_btn_xpath = XPath.descendant(:input)[XPath.attr(:type).one_of('submit', 'reset', 'image', 'button')]
    btn_xpath = XPath.descendant(:button)
    image_btn_xpath = XPath.descendant(:input)[XPath.attr(:type) == 'image']

    unless locator.nil?
      locator = locator.to_s
      locator_matches = XPath.attr(:id).equals(locator) | XPath.attr(:value).is(locator) | XPath.attr(:title).is(locator)
      locator_matches |= XPath.attr(:aria-label').is(locator) if options[:enable_aria_label]

      input_btn_xpath = input_btn_xpath[locator_matches]

      btn_xpath = btn_xpath[locator_matches | XPath.string.n.is(locator) | XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]

      alt_matches = XPath.attr(:alt).is(locator)
      alt_matches |= XPath.attr(:aria-label').is(locator) if options[:enable_aria_label]
      image_btn_xpath = image_btn_xpath[alt_matches]
    end

    res_xpath = input_btn_xpath.union(btn_xpath).union(image_btn_xpath)

    res_xpath = expression_filters.keys.inject(res_xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }

    res_xpath
  end

  filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }

  describe do |disabled: nil, **options|
    desc = "".dup
    desc << " that is disabled" if disabled == true
    desc << describe_all_expression_filters(options)
    desc
  end
end

#checkboxObject

Find checkboxes



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/capybara/selector.rb', line 330

Capybara.add_selector(:checkbox) do
  xpath do |locator, **options|
    xpath = XPath.descendant(:input)[XPath.attr(:type) == 'checkbox']
    locate_field(xpath, locator, options)
  end

  filter_set(:_field, %i[checked unchecked disabled name])

  filter(:option) { |node, value| node.value == value.to_s }

  describe do |option: nil, **options|
    desc = "".dup
    desc << " with value #{option.inspect}" if option
    desc << describe_all_expression_filters(options)
    desc
  end
end

#cssObject

Select elements by CSS selector



45
46
47
# File 'lib/capybara/selector.rb', line 45

Capybara.add_selector(:css) do
  css { |css| css }
end

#fieldObject

Select field elements (input [not of type submit, image, or hidden], textarea, select)



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
# File 'lib/capybara/selector.rb', line 75

Capybara.add_selector(:field) do
  xpath do |locator, **options|
    xpath = XPath.descendant(:input, :textarea, :select)[!XPath.attr(:type).one_of('submit', 'image', 'hidden')]
    locate_field(xpath, locator, options)
  end

  expression_filter(:type) do |expr, type|
    type = type.to_s
    if %w[textarea select].include?(type)
      expr.self(type.to_sym)
    else
      expr[XPath.attr(:type) == type]
    end
  end

  filter_set(:_field) # checked/unchecked/disabled/multiple/name/placeholder

  filter(:readonly, :boolean) { |node, value| !(value ^ node.readonly?) }
  filter(:with) do |node, with|
    with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s
  end
  describe do |type: nil, **options|
    desc = "".dup
    (expression_filters.keys - [:type]).each { |ef| desc << " with #{ef} #{options[ef]}" if options.key?(ef) }
    desc << " of type #{type.inspect}" if type
    desc << " with value #{options[:with].to_s.inspect}" if options.key?(:with)
    desc
  end
end

#fieldsetObject

Select fieldset elements



115
116
117
118
119
120
121
122
# File 'lib/capybara/selector.rb', line 115

Capybara.add_selector(:fieldset) do
  xpath(:legend) do |locator, legend: nil, **_options|
    xpath = XPath.descendant(:fieldset)
    xpath = xpath[(XPath.attr(:id) == locator.to_s) | XPath.child(:legend)[XPath.string.n.is(locator.to_s)]] unless locator.nil?
    xpath = xpath[XPath.child(:legend)[XPath.string.n.is(legend)]] if legend
    xpath
  end
end

#file_fieldObject

Find file input elements



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/capybara/selector.rb', line 447

Capybara.add_selector(:file_field) do
  label "file field"
  xpath do |locator, options|
    xpath = XPath.descendant(:input)[XPath.attr(:type) == 'file']
    locate_field(xpath, locator, options)
  end

  filter_set(:_field, %i[disabled multiple name])

  describe do |**options|
    desc = "".dup
    desc << describe_all_expression_filters(options)
    desc
  end
end

#fillable_fieldObject

Find text fillable fields ( textarea, input [not of type submit, image, radio, checkbox, hidden, file] )



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
# File 'lib/capybara/selector.rb', line 253

Capybara.add_selector(:fillable_field) do
  label "field"

  xpath do |locator, **options|
    xpath = XPath.descendant(:input, :textarea)[!XPath.attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')]
    locate_field(xpath, locator, options)
  end

  expression_filter(:type) do |expr, type|
    type = type.to_s
    if ['textarea'].include?(type)
      expr.self(type.to_sym)
    else
      expr[XPath.attr(:type) == type]
    end
  end

  filter_set(:_field, %i[disabled multiple name placeholder])

  filter(:with) do |node, with|
    with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s
  end

  describe do |options|
    desc = "".dup
    desc << describe_all_expression_filters(options)
    desc << " with value #{options[:with].to_s.inspect}" if options.key?(:with)
    desc
  end
end

#frameObject

Find frame/iframe elements



538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/capybara/selector.rb', line 538

Capybara.add_selector(:frame) do
  xpath(:name) do |locator, **options|
    xpath = XPath.descendant(:iframe).union(XPath.descendant(:frame))
    xpath = xpath[(XPath.attr(:id) == locator.to_s) | (XPath.attr(:name) == locator.to_s)] unless locator.nil?
    xpath = expression_filters.keys.inject(xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }
    xpath
  end

  describe do |name: nil, **_options|
    desc = "".dup
    desc << " with name #{name}" if name
    desc
  end
end

#idObject

Select element by id



55
56
57
# File 'lib/capybara/selector.rb', line 55

Capybara.add_selector(:id) do
  xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
end

#labelObject

Find label elements



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/capybara/selector.rb', line 470

Capybara.add_selector(:label) do
  label "label"
  xpath(:for) do |locator, options|
    xpath = XPath.descendant(:label)
    xpath = xpath[XPath.string.n.is(locator.to_s) | (XPath.attr(:id) == locator.to_s)] unless locator.nil?
    if options.key?(:for) && !options[:for].is_a?(Capybara::Node::Element)
      with_attr = XPath.attr(:for) == options[:for].to_s
      labelable_elements = %i[button input keygen meter output progress select textarea]
      wrapped = !XPath.attr(:for) &
                XPath.descendant(*labelable_elements)[XPath.attr(:id) == options[:for].to_s]
      xpath = xpath[with_attr | wrapped]
    end
    xpath
  end

  filter(:for) do |node, field_or_value|
    if field_or_value.is_a? Capybara::Node::Element
      if node[:for]
        field_or_value[:id] == node[:for]
      else
        field_or_value.find_xpath('./ancestor::label[1]').include? node.base
      end
    else
      # Non element values were handled through the expression filter
      true
    end
  end

  describe do |**options|
    desc = "".dup
    desc << " for #{options[:for]}" if options[:for]
    desc
  end
end

Find links ( <a> elements with an href attribute )



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
# File 'lib/capybara/selector.rb', line 136

Capybara.add_selector(:link) do
  xpath(:title, :alt) do |locator, href: true, enable_aria_label: false, alt: nil, title: nil, **_options|
    xpath = XPath.descendant(:a)
    xpath = if href.nil?
      xpath[!XPath.attr(:href)]
    else
      xpath[XPath.attr(:href)]
    end
    unless locator.nil?
      locator = locator.to_s
      matchers = [XPath.attr(:id) == locator,
                  XPath.string.n.is(locator),
                  XPath.attr(:title).is(locator),
                  XPath.descendant(:img)[XPath.attr(:alt).is(locator)]].reduce(:|)
      matchers |= XPath.attr(:aria-label').is(locator) if enable_aria_label
      xpath = xpath[matchers]
    end
    xpath = xpath[find_by_attr(:title, title)]
    xpath = xpath[XPath.descendant(:img)[XPath.attr(:alt) == alt]] if alt
    xpath
  end

  filter(:href) do |node, href|
    case href
    when nil
      true
    when Regexp
      node[:href].match href
    else
      node.first(:xpath, XPath.self[XPath.attr(:href) == href.to_s], minimum: 0)
    end
  end

  describe do |**options|
    desc = "".dup
    desc << " with href #{options[:href].inspect}" if options[:href]
    desc << " with no href attribute" if options.fetch(:href, true).nil?
  end
end

Find links or buttons



228
229
230
231
232
233
234
235
236
237
# File 'lib/capybara/selector.rb', line 228

Capybara.add_selector(:link_or_button) do
  label "link or button"
  xpath do |locator, **options|
    self.class.all.values_at(:link, :button).map { |selector| selector.xpath.call(locator, options) }.reduce(:union)
  end

  filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| node.tag_name == "a" or !(value ^ node.disabled?) }

  describe { |disabled: nil, **_options| " that is disabled" if disabled == true }
end

#must_have_buttonObject



# File 'lib/capybara/minitest/spec.rb', line 71

#must_have_checked_fieldObject



# File 'lib/capybara/minitest/spec.rb', line 95

#must_have_cssObject

see Capybara::Node::Matchers#has_css?



# File 'lib/capybara/minitest/spec.rb', line 47

#must_have_current_pathObject



# File 'lib/capybara/minitest/spec.rb', line 153

#must_have_fieldObject



# File 'lib/capybara/minitest/spec.rb', line 83



# File 'lib/capybara/minitest/spec.rb', line 59

#must_have_selectObject



# File 'lib/capybara/minitest/spec.rb', line 117

#must_have_tableObject



# File 'lib/capybara/minitest/spec.rb', line 129

#must_have_titleObject



# File 'lib/capybara/minitest/spec.rb', line 141

#must_have_unchecked_fieldObject



# File 'lib/capybara/minitest/spec.rb', line 106

#must_have_xpathObject

see Capybara::Node::Matchers#has_xpath?



# File 'lib/capybara/minitest/spec.rb', line 35

#optionObject

Find option elements



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/capybara/selector.rb', line 418

Capybara.add_selector(:option) do
  xpath do |locator|
    xpath = XPath.descendant(:option)
    xpath = xpath[XPath.string.n.is(locator.to_s)] unless locator.nil?
    xpath
  end

  filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }
  filter(:selected, :boolean) { |node, value| !(value ^ node.selected?) }

  describe do |**options|
    desc = "".dup
    desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)
    desc << " that is#{' not' unless options[:selected]} selected" if options.key?(:selected)
    desc
  end
end

#radio_buttonObject

Find radio buttons



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/capybara/selector.rb', line 297

Capybara.add_selector(:radio_button) do
  label "radio button"

  xpath do |locator, **options|
    xpath = XPath.descendant(:input)[XPath.attr(:type) == 'radio']
    locate_field(xpath, locator, options)
  end

  filter_set(:_field, %i[checked unchecked disabled name])

  filter(:option) { |node, value| node.value == value.to_s }

  describe do |option: nil, **options|
    desc = "".dup
    desc << " with value #{option.inspect}" if option
    desc << describe_all_expression_filters(options)
    desc
  end
end

#selectObject

Find select elements



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/capybara/selector.rb', line 364

Capybara.add_selector(:select) do
  label "select box"

  xpath do |locator, **options|
    xpath = XPath.descendant(:select)
    locate_field(xpath, locator, options)
  end

  filter_set(:_field, %i[disabled multiple name placeholder])

  filter(:options) do |node, options|
    actual = if node.visible?
      node.all(:xpath, './/option', wait: false).map(&:text)
    else
      node.all(:xpath, './/option', visible: false, wait: false).map { |option| option.text(:all) }
    end
    options.sort == actual.sort
  end

  filter(:with_options) do |node, options|
    finder_settings = { minimum: 0 }
    finder_settings[:visible] = false unless node.visible?
    options.all? { |option| node.first(:option, option, finder_settings) }
  end

  filter(:selected) do |node, selected|
    actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }
    Array(selected).sort == actual.sort
  end

  filter(:with_selected) do |node, selected|
    actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }
    (Array(selected) - actual).empty?
  end

  describe do |options: nil, with_options: nil, selected: nil, with_selected: nil, **opts|
    desc = "".dup
    desc << " with options #{options.inspect}" if options
    desc << " with at least options #{with_options.inspect}" if with_options
    desc << " with #{selected.inspect} selected" if selected
    desc << " with at least #{with_selected.inspect} selected" if with_selected
    desc << describe_all_expression_filters(opts)
    desc
  end
end

#tableObject

Find table elements



514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/capybara/selector.rb', line 514

Capybara.add_selector(:table) do
  xpath(:caption) do |locator, options|
    xpath = XPath.descendant(:table)
    xpath = xpath[(XPath.attr(:id) == locator.to_s) | XPath.descendant(:caption).is(locator.to_s)] unless locator.nil?
    xpath = xpath[XPath.descendant(:caption) == options[:caption]] if options[:caption]
    xpath
  end

  describe do |caption: nil, **_options|
    desc = "".dup
    desc << " with caption #{caption}" if caption
    desc
  end
end

#wont_have_buttonObject



# File 'lib/capybara/minitest/spec.rb', line 77

#wont_have_chceked_fieldObject

Expectation that there is no checked_field



# File 'lib/capybara/minitest/spec.rb', line 101

#wont_have_cssObject

see Capybara::Node::Matchers#has_no_css?



# File 'lib/capybara/minitest/spec.rb', line 53

#wont_have_current_pathObject



# File 'lib/capybara/minitest/spec.rb', line 159

#wont_have_fieldObject



# File 'lib/capybara/minitest/spec.rb', line 89

Expectation that there is no link

see Capybara::Node::Matchers#has_no_link?



# File 'lib/capybara/minitest/spec.rb', line 65

#wont_have_selectObject



# File 'lib/capybara/minitest/spec.rb', line 123

#wont_have_tableObject



# File 'lib/capybara/minitest/spec.rb', line 135

#wont_have_titleObject



# File 'lib/capybara/minitest/spec.rb', line 147

#wont_have_unchceked_fieldObject

Expectation that there is no unchecked_field



# File 'lib/capybara/minitest/spec.rb', line 112

#wont_have_xpathObject

see Capybara::Node::Matchers#has_no_xpath?



# File 'lib/capybara/minitest/spec.rb', line 41

#xpathObject

Select elements by XPath expression



35
36
37
# File 'lib/capybara/selector.rb', line 35

Capybara.add_selector(:xpath) do
  xpath { |xpath| xpath }
end