File: within_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 (212 lines) | stat: -rw-r--r-- 6,515 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
# frozen_string_literal: true

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

  context 'with CSS selector' do
    it 'should click links in the given scope' do
      @session.within(:css, '#for_bar li:first-child') do
        @session.click_link('Go')
      end
      expect(@session).to have_content('Bar')
    end

    it 'should assert content in the given scope' do
      @session.within(:css, '#for_foo') do
        expect(@session).not_to have_content('First Name')
      end
      expect(@session).to have_content('First Name')
    end

    it 'should accept additional options' do
      @session.within(:css, '#for_bar li', text: 'With Simple HTML') do
        @session.click_link('Go')
      end
      expect(@session).to have_content('Bar')
    end

    it 'should reload the node if the page is changed' do
      @session.within(:css, '#for_foo') do
        @session.visit('/with_scope_other')
        expect(@session).to have_content('Different text')
      end
    end

    it 'should reload multiple nodes if the page is changed' do
      @session.within(:css, '#for_bar') do
        @session.within(:css, 'form[action="/redirect"]') do
          @session.refresh
          expect(@session).to have_content('First Name')
        end
      end
    end

    it 'should error if the page is changed and a matching node no longer exists' do
      @session.within(:css, '#for_foo') do
        @session.visit('/')
        expect { @session.text }.to raise_error(StandardError)
      end
    end

    it 'should pass scope element to the block' do
      @session.within(:css, '#another_foo') do |scope|
        expect(scope).to match_css('#another_foo')
      end
    end

    it 'should scope click', requires: [:js] do
      @session.within(:css, '#another_foo') do |scope|
        @session.click
        expect(scope).to have_text('I was clicked')
      end
    end
  end

  context 'with XPath selector' do
    it 'should click links in the given scope' do
      @session.within(:xpath, "//div[@id='for_bar']//li[contains(.,'With Simple HTML')]") do
        @session.click_link('Go')
      end
      expect(@session).to have_content('Bar')
    end
  end

  context 'with the default selector' do
    it 'should use XPath' do
      @session.within("//div[@id='for_bar']//li[contains(.,'With Simple HTML')]") do
        @session.click_link('Go')
      end
      expect(@session).to have_content('Bar')
    end
  end

  context 'with Node rather than selector' do
    it 'should click links in the given scope' do
      node_of_interest = @session.find(:css, '#for_bar li', text: 'With Simple HTML')

      @session.within(node_of_interest) do
        @session.click_link('Go')
      end
      expect(@session).to have_content('Bar')
    end
  end

  context 'with the default selector set to CSS' do
    before { Capybara.default_selector = :css }

    after { Capybara.default_selector = :xpath }

    it 'should use CSS' do
      @session.within('#for_bar li', text: 'With Simple HTML') do
        @session.click_link('Go')
      end
      expect(@session).to have_content('Bar')
    end
  end

  context 'with nested scopes' do
    it 'should respect the inner scope' do
      @session.within("//div[@id='for_bar']") do
        @session.within(".//li[contains(.,'Bar')]") do
          @session.click_link('Go')
        end
      end
      expect(@session).to have_content('Another World')
    end

    it 'should respect the outer scope' do
      @session.within("//div[@id='another_foo']") do
        @session.within(".//li[contains(.,'With Simple HTML')]") do
          @session.click_link('Go')
        end
      end
      expect(@session).to have_content('Hello world')
    end
  end

  it 'should raise an error if the scope is not found on the page' do
    expect do
      @session.within("//div[@id='doesnotexist']") do
      end
    end.to raise_error(Capybara::ElementNotFound)
  end

  it 'should restore the scope when an error is raised' do
    expect do
      @session.within("//div[@id='for_bar']") do
        expect do
          expect do
            @session.within(".//div[@id='doesnotexist']") do
            end
          end.to raise_error(Capybara::ElementNotFound)
        end.not_to change { @session.has_xpath?(".//div[@id='another_foo']") }.from(false)
      end
    end.not_to change { @session.has_xpath?(".//div[@id='another_foo']") }.from(true)
  end

  it 'should fill in a field and click a button' do
    @session.within("//li[contains(.,'Bar')]") do
      @session.click_button('Go')
    end
    expect(extract_results(@session)['first_name']).to eq('Peter')
    @session.visit('/with_scope')
    @session.within("//li[contains(.,'Bar')]") do
      @session.fill_in('First Name', with: 'Dagobert')
      @session.click_button('Go')
    end
    expect(extract_results(@session)['first_name']).to eq('Dagobert')
  end

  it 'should have #within_element as an alias' do
    expect(Capybara::Session.instance_method(:within)).to eq Capybara::Session.instance_method(:within_element)
    @session.within_element(:css, '#for_foo') do
      expect(@session).not_to have_content('First Name')
    end
  end
end

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

  it 'should restrict scope to a fieldset given by id' do
    @session.within_fieldset('villain_fieldset') do
      @session.fill_in('Name', with: 'Goldfinger')
      @session.click_button('Create')
    end
    expect(extract_results(@session)['villain_name']).to eq('Goldfinger')
  end

  it 'should restrict scope to a fieldset given by legend' do
    @session.within_fieldset('Villain') do
      @session.fill_in('Name', with: 'Goldfinger')
      @session.click_button('Create')
    end
    expect(extract_results(@session)['villain_name']).to eq('Goldfinger')
  end
end

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

  it 'should restrict scope to a fieldset given by id' do
    @session.within_table('girl_table') do
      @session.fill_in('Name', with: 'Christmas')
      @session.click_button('Create')
    end
    expect(extract_results(@session)['girl_name']).to eq('Christmas')
  end

  it 'should restrict scope to a fieldset given by legend' do
    @session.within_table('Villain') do
      @session.fill_in('Name', with: 'Quantum')
      @session.click_button('Create')
    end
    expect(extract_results(@session)['villain_name']).to eq('Quantum')
  end
end