File: click_link_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 (240 lines) | stat: -rw-r--r-- 8,121 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# frozen_string_literal: true

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

  it 'should wait for asynchronous load', requires: [:js] do
    Capybara.default_max_wait_time = 2
    @session.visit('/with_js')
    @session.click_link('Click me')
    @session.click_link('Has been clicked')
  end

  it 'casts to string' do
    @session.click_link(:foo)
    expect(@session).to have_content('Another World')
  end

  it 'raises any errors caught inside the server', requires: [:server] do
    quietly { @session.visit('/error') }
    expect do
      @session.click_link('foo')
    end.to raise_error(TestApp::TestAppError)
  end

  context 'with id given' do
    it 'should take user to the linked page' do
      @session.click_link('foo')
      expect(@session).to have_content('Another World')
    end
  end

  context 'with text given' do
    it 'should take user to the linked page' do
      @session.click_link('labore')
      expect(@session).to have_content('Bar')
    end

    it 'should accept partial matches', :exact_false do
      @session.click_link('abo')
      expect(@session).to have_content('Bar')
    end
  end

  context 'with title given' do
    it 'should take user to the linked page' do
      @session.click_link('awesome title')
      expect(@session).to have_content('Bar')
    end

    it 'should accept partial matches', :exact_false do
      @session.click_link('some titl')
      expect(@session).to have_content('Bar')
    end
  end

  context 'with alternative text given to a contained image' do
    it 'should take user to the linked page' do
      @session.click_link('awesome image')
      expect(@session).to have_content('Bar')
    end

    it 'should accept partial matches', :exact_false do
      @session.click_link('some imag')
      expect(@session).to have_content('Bar')
    end
  end

  context "with a locator that doesn't exist" do
    it 'should raise an error' do
      msg = 'Unable to find link "does not exist"'
      expect do
        @session.click_link('does not exist')
      end.to raise_error(Capybara::ElementNotFound, msg)
    end
  end

  context 'with :href option given' do
    it 'should find links with valid href' do
      @session.click_link('labore', href: '/with_simple_html')
      expect(@session).to have_content('Bar')
    end

    it "should raise error if link wasn't found" do
      expect { @session.click_link('labore', href: 'invalid_href') }.to raise_error(Capybara::ElementNotFound, /with href "invalid_href/)
    end
  end

  context 'with a regex :href option given' do
    it 'should find a link matching an all-matching regex pattern' do
      @session.click_link('labore', href: /.+/)
      expect(@session).to have_content('Bar')
    end

    it 'should find a link matching an exact regex pattern' do
      @session.click_link('labore', href: %r{/with_simple_html})
      expect(@session).to have_content('Bar')
    end

    it 'should find a link matching a partial regex pattern' do
      @session.click_link('labore', href: %r{/with_simple})
      expect(@session).to have_content('Bar')
    end

    it "should raise an error if no link's href matched the pattern" do
      expect { @session.click_link('labore', href: /invalid_pattern/) }.to raise_error(Capybara::ElementNotFound, %r{with href matching /invalid_pattern/})
      expect { @session.click_link('labore', href: /.+d+/) }.to raise_error(Capybara::ElementNotFound, /#{Regexp.quote 'with href matching /.+d+/'}/)
    end

    context 'href: nil' do
      it 'should not raise an error on links with no href attribute' do
        expect { @session.click_link('No Href', href: nil) }.not_to raise_error
      end

      it 'should raise an error if href attribute exists' do
        expect { @session.click_link('Blank Href', href: nil) }.to raise_error(Capybara::ElementNotFound, /with no href attribute/)
        expect { @session.click_link('Normal Anchor', href: nil) }.to raise_error(Capybara::ElementNotFound, /with no href attribute/)
      end
    end

    context 'href: false' do
      it 'should not raise an error on links with no href attribute' do
        expect { @session.click_link('No Href', href: false) }.not_to raise_error
      end

      it 'should not raise an error if href attribute exists' do
        expect { @session.click_link('Blank Href', href: false) }.not_to raise_error
        expect { @session.click_link('Normal Anchor', href: false) }.not_to raise_error
      end
    end
  end

  context 'with :target option given' do
    it 'should find links with valid target' do
      @session.click_link('labore', target: '_self')
      expect(@session).to have_content('Bar')
    end

    it "should raise error if link wasn't found" do
      expect { @session.click_link('labore', target: '_blank') }.to raise_error(Capybara::ElementNotFound, /Unable to find link "labore"/)
    end
  end

  it 'should follow relative links' do
    @session.visit('/')
    @session.click_link('Relative')
    expect(@session).to have_content('This is a test')
  end

  it 'should follow protocol relative links' do
    @session.click_link('Protocol')
    expect(@session).to have_content('Another World')
  end

  it 'should follow redirects' do
    @session.click_link('Redirect')
    expect(@session).to have_content('You landed')
  end

  it 'should follow redirects back to itself' do
    @session.click_link('BackToMyself')
    expect(@session).to have_css('#referrer', text: %r{/with_html$})
    expect(@session).to have_content('This is a test')
  end

  it 'should add query string to current URL with naked query string' do
    @session.click_link('Naked Query String')
    expect(@session).to have_content('Query String sent')
  end

  it 'should do nothing on anchor links' do
    @session.fill_in('test_field', with: 'blah')
    @session.click_link('Normal Anchor')
    expect(@session.find_field('test_field').value).to eq('blah')
    @session.click_link('Blank Anchor')
    expect(@session.find_field('test_field').value).to eq('blah')
    @session.click_link('Blank JS Anchor')
    expect(@session.find_field('test_field').value).to eq('blah')
  end

  it 'should do nothing on URL+anchor links for the same page' do
    @session.fill_in('test_field', with: 'blah')
    @session.click_link('Anchor on same page')
    expect(@session.find_field('test_field').value).to eq('blah')
  end

  it 'should follow link on URL+anchor links for a different page' do
    @session.click_link('Anchor on different page')
    expect(@session).to have_content('Bar')
  end

  it 'should follow link on anchor if the path has regex special characters' do
    @session.visit('/with.*html')
    @session.click_link('Anchor on different page')
    expect(@session).to have_content('Bar')
  end

  it 'should raise an error with links with no href' do
    expect do
      @session.click_link('No Href')
    end.to raise_error(Capybara::ElementNotFound)
  end

  context 'with :exact option' do
    it 'should accept partial matches when false' do
      @session.click_link('abo', exact: false)
      expect(@session).to have_content('Bar')
    end

    it 'should not accept partial matches when true' do
      expect do
        @session.click_link('abo', exact: true)
      end.to raise_error(Capybara::ElementNotFound)
    end
  end

  context 'without locator' do
    it 'uses options' do
      @session.click_link(href: '/foo')
      expect(@session).to have_content('Another World')
    end
  end

  it 'should return element clicked' do
    el = @session.find(:link, 'Normal Anchor')
    expect(@session.click_link('Normal Anchor')).to eq el
  end

  it 'can download a file', requires: [:download] do
    # This requires the driver used for the test to be configured
    # to download documents with the mime type "text/csv"
    download_file = File.join(Capybara.save_path, 'download.csv')
    expect(File).not_to exist(download_file)
    @session.click_link('Download Me')
    sleep 2 # allow time for file to download
    expect(File).to exist(download_file)
    FileUtils.rm_rf download_file
  end
end