File: match_css_spec.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 (31 lines) | stat: -rw-r--r-- 1,099 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

Capybara::SpecHelper.spec '#match_css?' do
  before do
    @session.visit('/with_html')
    @element = @session.find(:css, 'span', text: '42')
  end

  it 'should be true if the given selector matches the element' do
    expect(@element).to match_css('span')
    expect(@element).to match_css('span.number')
  end

  it 'should be false if the given selector does not match' do
    expect(@element).not_to match_css('div')
    expect(@element).not_to match_css('p a#doesnotexist')
    expect(@element).not_to match_css('p.nosuchclass')
  end

  it 'should accept an optional filter block' do
    # This would be better done with
    expect(@element).to match_css('span') { |el| el[:class] == 'number' }
    expect(@element).not_to match_css('span') { |el| el[:class] == 'not_number' }
  end

  it 'should work with root element found via ancestor' do
    el = @session.find(:css, 'body').find(:xpath, '..')
    expect(el).to match_css('html')
    expect { expect(el).to not_match_css('html') }.to raise_exception(RSpec::Expectations::ExpectationNotMetError)
  end
end