File: has_selector_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 (217 lines) | stat: -rw-r--r-- 9,080 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
208
209
210
211
212
213
214
215
216
217
# frozen_string_literal: true

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

  it 'should be true if the given selector is on the page' do
    expect(@session).to have_selector(:xpath, '//p')
    expect(@session).to have_selector(:css, 'p a#foo')
    expect(@session).to have_selector("//p[contains(.,'est')]")
  end

  it 'should be false if the given selector is not on the page' do
    expect(@session).not_to have_selector(:xpath, '//abbr')
    expect(@session).not_to have_selector(:css, 'p a#doesnotexist')
    expect(@session).not_to have_selector("//p[contains(.,'thisstringisnotonpage')]")
  end

  it 'should use default selector' do
    Capybara.default_selector = :css
    expect(@session).not_to have_selector('p a#doesnotexist')
    expect(@session).to have_selector('p a#foo')
  end

  it 'should respect scopes' do
    @session.within "//p[@id='first']" do
      expect(@session).to have_selector(".//a[@id='foo']")
      expect(@session).not_to have_selector(".//a[@id='red']")
    end
  end

  it 'should accept a filter block' do
    expect(@session).to have_selector(:css, 'a', count: 1) { |el| el[:id] == 'foo' }
  end

  context 'with count' do
    it 'should be true if the content is on the page the given number of times' do
      expect(@session).to have_selector('//p', count: 3)
      expect(@session).to have_selector("//p//a[@id='foo']", count: 1)
      expect(@session).to have_selector("//p[contains(.,'est')]", count: 1)
    end

    it 'should be false if the content is on the page the given number of times' do
      expect(@session).not_to have_selector('//p', count: 6)
      expect(@session).not_to have_selector("//p//a[@id='foo']", count: 2)
      expect(@session).not_to have_selector("//p[contains(.,'est')]", count: 5)
    end

    it "should be false if the content isn't on the page at all" do
      expect(@session).not_to have_selector('//abbr', count: 2)
      expect(@session).not_to have_selector("//p//a[@id='doesnotexist']", count: 1)
    end
  end

  context 'with text' do
    it 'should discard all matches where the given string is not contained' do
      expect(@session).to have_selector('//p//a', text: 'Redirect', count: 1)
      expect(@session).to have_selector(:css, 'p a', text: 'Redirect', count: 1)
      expect(@session).not_to have_selector('//p', text: 'Doesnotexist')
    end

    it 'should respect visibility setting' do
      expect(@session).to have_selector(:id, 'hidden-text', text: 'Some of this text is hidden!', visible: :all)
      expect(@session).not_to have_selector(:id, 'hidden-text', text: 'Some of this text is hidden!', visible: :visible)
      Capybara.ignore_hidden_elements = false
      expect(@session).to have_selector(:id, 'hidden-text', text: 'Some of this text is hidden!', visible: :all)
      Capybara.visible_text_only = true
      expect(@session).not_to have_selector(:id, 'hidden-text', text: 'Some of this text is hidden!', visible: :visible)
    end

    it 'should discard all matches where the given regexp is not matched' do
      expect(@session).to have_selector('//p//a', text: /re[dab]i/i, count: 1)
      expect(@session).not_to have_selector('//p//a', text: /Red$/)
    end

    it 'should raise when extra parameters passed' do
      expect do
        expect(@session).to have_selector(:css, 'p a#foo', 'extra')
      end.to raise_error ArgumentError, /extra/
    end

    context 'with whitespace normalization' do
      context 'Capybara.default_normalize_ws = false' do
        it 'should support normalize_ws option' do
          Capybara.default_normalize_ws = false
          expect(@session).not_to have_selector(:id, 'second', text: 'text with whitespace')
          expect(@session).to have_selector(:id, 'second', text: 'text with whitespace', normalize_ws: true)
        end
      end

      context 'Capybara.default_normalize_ws = true' do
        it 'should support normalize_ws option' do
          Capybara.default_normalize_ws = true
          expect(@session).to have_selector(:id, 'second', text: 'text with whitespace')
          expect(@session).not_to have_selector(:id, 'second', text: 'text with whitespace', normalize_ws: false)
        end
      end
    end
  end

  context 'with exact_text' do
    context 'string' do
      it 'should only match elements that match exactly' do
        expect(@session).to have_selector(:id, 'h2one', exact_text: 'Header Class Test One')
        expect(@session).to have_no_selector(:id, 'h2one', exact_text: 'Header Class Test')
      end
    end

    context 'boolean' do
      it 'should only match elements that match exactly when true' do
        expect(@session).to have_selector(:id, 'h2one', text: 'Header Class Test One', exact_text: true)
        expect(@session).to have_no_selector(:id, 'h2one', text: 'Header Class Test', exact_text: true)
      end

      it 'should match substrings when false' do
        expect(@session).to have_selector(:id, 'h2one', text: 'Header Class Test One', exact_text: false)
        expect(@session).to have_selector(:id, 'h2one', text: 'Header Class Test', exact_text: false)
      end

      it 'should warn if text option is a regexp that it is ignoring exact_text' do
        allow(Capybara::Helpers).to receive(:warn)
        expect(@session).to have_selector(:id, 'h2one', text: /Class Test/, exact_text: true)
        expect(Capybara::Helpers).to have_received(:warn).with(/'exact_text' option is not supported/)
      end
    end

    context 'regexp' do
      it 'should only match when it fully matches' do
        expect(@session).to have_selector(:id, 'h2one', exact_text: /Header Class Test One/)
        expect(@session).to have_no_selector(:id, 'h2one', exact_text: /Header Class Test/)
        expect(@session).to have_no_selector(:id, 'h2one', exact_text: /Class Test One/)
        expect(@session).to have_no_selector(:id, 'h2one', exact_text: /Class Test/)
      end
    end
  end

  context 'datalist' do
    it 'should match options' do
      @session.visit('/form')
      expect(@session).to have_selector(:datalist_input, with_options: %w[Jaguar Audi Mercedes])
      expect(@session).not_to have_selector(:datalist_input, with_options: %w[Ford Chevy])
    end
  end
end

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

  it 'should be false if the given selector is on the page' do
    expect(@session).not_to have_no_selector(:xpath, '//p')
    expect(@session).not_to have_no_selector(:css, 'p a#foo')
    expect(@session).not_to have_no_selector("//p[contains(.,'est')]")
  end

  it 'should be true if the given selector is not on the page' do
    expect(@session).to have_no_selector(:xpath, '//abbr')
    expect(@session).to have_no_selector(:css, 'p a#doesnotexist')
    expect(@session).to have_no_selector("//p[contains(.,'thisstringisnotonpage')]")
  end

  it 'should use default selector' do
    Capybara.default_selector = :css
    expect(@session).to have_no_selector('p a#doesnotexist')
    expect(@session).not_to have_no_selector('p a#foo')
  end

  it 'should respect scopes' do
    @session.within "//p[@id='first']" do
      expect(@session).not_to have_no_selector(".//a[@id='foo']")
      expect(@session).to have_no_selector(".//a[@id='red']")
    end
  end

  it 'should accept a filter block' do
    expect(@session).to have_no_selector(:css, 'a#foo') { |el| el[:id] != 'foo' }
  end

  context 'with count' do
    it 'should be false if the content is on the page the given number of times' do
      expect(@session).not_to have_no_selector('//p', count: 3)
      expect(@session).not_to have_no_selector("//p//a[@id='foo']", count: 1)
      expect(@session).not_to have_no_selector("//p[contains(.,'est')]", count: 1)
    end

    it 'should be true if the content is on the page the wrong number of times' do
      expect(@session).to have_no_selector('//p', count: 6)
      expect(@session).to have_no_selector("//p//a[@id='foo']", count: 2)
      expect(@session).to have_no_selector("//p[contains(.,'est')]", count: 5)
    end

    it "should be true if the content isn't on the page at all" do
      expect(@session).to have_no_selector('//abbr', count: 2)
      expect(@session).to have_no_selector("//p//a[@id='doesnotexist']", count: 1)
    end
  end

  context 'with text' do
    it 'should discard all matches where the given string is contained' do
      expect(@session).not_to have_no_selector('//p//a', text: 'Redirect', count: 1)
      expect(@session).to have_no_selector('//p', text: 'Doesnotexist')
    end

    it 'should discard all matches where the given regexp is matched' do
      expect(@session).not_to have_no_selector('//p//a', text: /re[dab]i/i, count: 1)
      expect(@session).to have_no_selector('//p//a', text: /Red$/)
    end

    it 'should error when matching element exists' do
      expect do
        expect(@session).to have_no_selector('//h2', text: 'Header Class Test Five')
      end.to raise_error RSpec::Expectations::ExpectationNotMetError
    end
  end
end