File: has_all_selectors_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 (69 lines) | stat: -rw-r--r-- 2,514 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
# frozen_string_literal: true

Capybara::SpecHelper.spec '#have_all_of_selectors' do
  before do
    @session.visit('/with_html')
  end

  it 'should be true if the given selectors are on the page' do
    expect(@session).to have_all_of_selectors(:css, 'p a#foo', 'h2#h2one', 'h2#h2two')
  end

  it 'should be false if any of the given selectors are not on the page' do
    expect do
      expect(@session).to have_all_of_selectors(:css, 'p a#foo', 'h2#h2three', 'h2#h2one')
    end.to raise_error RSpec::Expectations::ExpectationNotMetError
  end

  it 'should use default selector' do
    Capybara.default_selector = :css
    expect(@session).to have_all_of_selectors('p a#foo', 'h2#h2two', 'h2#h2one')
    expect do
      expect(@session).to have_all_of_selectors('p a#foo', 'h2#h2three', 'h2#h2one')
    end.to raise_error RSpec::Expectations::ExpectationNotMetError
  end

  context 'should respect scopes' do
    it 'when used with `within`' do
      @session.within "//p[@id='first']" do
        expect(@session).to have_all_of_selectors(".//a[@id='foo']")
        expect do
          expect(@session).to have_all_of_selectors(".//a[@id='red']")
        end.to raise_error RSpec::Expectations::ExpectationNotMetError
      end
    end

    it 'when called on elements' do
      el = @session.find "//p[@id='first']"
      expect(el).to have_all_of_selectors(".//a[@id='foo']")
      expect do
        expect(el).to have_all_of_selectors(".//a[@id='red']")
      end.to raise_error RSpec::Expectations::ExpectationNotMetError
    end
  end

  context 'with options' do
    it 'should apply options to all locators' do
      expect(@session).to have_all_of_selectors(:field, 'normal', 'additional_newline', type: :textarea)
      expect do
        expect(@session).to have_all_of_selectors(:field, 'normal', 'test_field', 'additional_newline', type: :textarea)
      end.to raise_error RSpec::Expectations::ExpectationNotMetError
    end
  end

  context 'with wait', requires: [:js] do
    it 'should not raise error if all the elements appear before given wait duration' do
      Capybara.using_wait_time(0.1) do
        @session.visit('/with_js')
        @session.click_link('Click me')
        expect(@session).to have_all_of_selectors(:css, 'a#clickable', 'a#has-been-clicked', '#drag', wait: 5)
      end
    end
  end

  it 'cannot be negated' do
    expect do
      expect(@session).not_to have_all_of_selectors(:css, 'p a#foo', 'h2#h2one', 'h2#h2two')
    end.to raise_error ArgumentError
  end
end