2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
Capybara::SpecHelper.spec '#click_link' do
|
|
|
|
before do
|
|
|
|
@session.visit('/with_html')
|
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should wait for asynchronous load', requires: [:js] do
|
2018-07-05 21:08:59 -04:00
|
|
|
Capybara.default_max_wait_time = 2
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.click_link('Click me')
|
|
|
|
@session.click_link('Has been clicked')
|
|
|
|
end
|
2010-10-22 10:45:33 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'casts to string' do
|
2018-02-28 19:11:41 -05:00
|
|
|
@session.click_link(:foo)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Another World')
|
2012-09-06 03:33:43 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'raises any errors caught inside the server', requires: [:server] do
|
|
|
|
quietly { @session.visit('/error') }
|
2014-03-02 09:49:00 -05:00
|
|
|
expect do
|
|
|
|
@session.click_link('foo')
|
|
|
|
end.to raise_error(TestApp::TestAppError)
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with id given' do
|
|
|
|
it 'should take user to the linked page' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('foo')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Another World')
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-01-15 10:13:48 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with text given' do
|
|
|
|
it 'should take user to the linked page' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('labore')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should accept partial matches', :exact_false do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('abo')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with title given' do
|
|
|
|
it 'should take user to the linked page' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('awesome title')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should accept partial matches', :exact_false do
|
2012-09-09 17:58:09 -04:00
|
|
|
@session.click_link('some titl')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2011-08-30 04:54:20 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2011-08-30 04:54:20 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with alternative text given to a contained image' do
|
|
|
|
it 'should take user to the linked page' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('awesome image')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2010-10-22 10:45:33 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should accept partial matches', :exact_false do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('some imag')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2010-02-12 08:41:41 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-10-22 10:45:33 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with a locator that doesn't exist" do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should raise an error' do
|
|
|
|
msg = 'Unable to find link "does not exist"'
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('does not exist')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound, msg)
|
2010-10-22 10:45:33 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-10-22 10:45:33 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with :href option given' do
|
|
|
|
it 'should find links with valid href' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.click_link('labore', href: '/with_simple_html')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2013-02-19 15:12:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise error if link wasn't found" do
|
2018-11-28 14:39:58 -05:00
|
|
|
expect { @session.click_link('labore', href: 'invalid_href') }.to raise_error(Capybara::ElementNotFound, /with href "invalid_href/)
|
2013-02-19 15:12:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with a regex :href option given' do
|
|
|
|
it 'should find a link matching an all-matching regex pattern' do
|
2016-10-04 14:10:29 -04:00
|
|
|
@session.click_link('labore', href: /.+/)
|
2015-04-27 20:33:39 -04:00
|
|
|
expect(@session).to have_content('Bar')
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should find a link matching an exact regex pattern' do
|
2018-05-14 17:30:34 -04:00
|
|
|
@session.click_link('labore', href: %r{/with_simple_html})
|
2015-07-22 02:41:23 -04:00
|
|
|
expect(@session).to have_content('Bar')
|
2015-04-27 20:33:39 -04:00
|
|
|
end
|
2015-07-22 02:41:23 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should find a link matching a partial regex pattern' do
|
2018-05-14 17:30:34 -04:00
|
|
|
@session.click_link('labore', href: %r{/with_simple})
|
2015-04-27 20:33:39 -04:00
|
|
|
expect(@session).to have_content('Bar')
|
|
|
|
end
|
2015-07-22 02:41:23 -04:00
|
|
|
|
2015-04-27 20:33:39 -04:00
|
|
|
it "should raise an error if no link's href matched the pattern" do
|
2018-11-28 14:39:58 -05:00
|
|
|
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+/"}/)
|
2015-04-27 20:33:39 -04:00
|
|
|
end
|
2016-12-15 13:59:01 -05:00
|
|
|
|
|
|
|
context 'href: nil' do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should not raise an error on links with no href attribute' do
|
2016-12-15 13:59:01 -05:00
|
|
|
expect { @session.click_link('No Href', href: nil) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should raise an error if href attribute exists' do
|
2018-11-28 14:39:58 -05:00
|
|
|
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/)
|
2016-12-15 13:59:01 -05:00
|
|
|
end
|
|
|
|
end
|
2019-05-14 19:00:37 -04:00
|
|
|
|
|
|
|
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
|
2015-04-27 20:33:39 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should follow relative links' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.visit('/')
|
|
|
|
@session.click_link('Relative')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('This is a test')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-10-22 10:45:33 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should follow protocol relative links' do
|
2012-09-06 04:46:54 -04:00
|
|
|
@session.click_link('Protocol')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Another World')
|
2012-09-06 04:46:54 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should follow redirects' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('Redirect')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('You landed')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-10-22 10:45:33 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should follow redirects back to itself' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('BackToMyself')
|
2018-05-14 17:30:34 -04:00
|
|
|
expect(@session).to have_css('#referrer', text: %r{/with_html$})
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('This is a test')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-10-22 10:45:33 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should add query string to current URL with naked query string' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('Naked Query String')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Query String sent')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should do nothing on anchor links' do
|
|
|
|
@session.fill_in('test_field', with: 'blah')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('Normal Anchor')
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(@session.find_field('test_field').value).to eq('blah')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('Blank Anchor')
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(@session.find_field('test_field').value).to eq('blah')
|
2015-03-16 18:57:04 -04:00
|
|
|
@session.click_link('Blank JS Anchor')
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(@session.find_field('test_field').value).to eq('blah')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should do nothing on URL+anchor links for the same page' do
|
|
|
|
@session.fill_in('test_field', with: 'blah')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('Anchor on same page')
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(@session.find_field('test_field').value).to eq('blah')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should follow link on URL+anchor links for a different page' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('Anchor on different page')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should follow link on anchor if the path has regex special characters' do
|
2015-04-14 15:46:00 -04:00
|
|
|
@session.visit('/with.*html')
|
|
|
|
@session.click_link('Anchor on different page')
|
|
|
|
expect(@session).to have_content('Bar')
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should raise an error with links with no href' do
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.click_link('No Href')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
2013-02-24 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'with :exact option' do
|
|
|
|
it 'should accept partial matches when false' do
|
2018-02-28 19:11:41 -05:00
|
|
|
@session.click_link('abo', exact: false)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_content('Bar')
|
2013-02-24 10:33:41 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should not accept partial matches when true' do
|
2013-02-24 10:33:41 -05:00
|
|
|
expect do
|
2018-02-28 19:11:41 -05:00
|
|
|
@session.click_link('abo', exact: true)
|
2013-02-24 10:33:41 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
2016-04-14 14:22:51 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'without locator' do
|
|
|
|
it 'uses options' do
|
2016-04-14 14:22:51 -04:00
|
|
|
@session.click_link(href: '/foo')
|
|
|
|
expect(@session).to have_content('Another World')
|
|
|
|
end
|
|
|
|
end
|
2016-10-10 17:43:40 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should return element clicked' do
|
2016-10-10 17:43:40 -04:00
|
|
|
el = @session.find(:link, 'Normal Anchor')
|
|
|
|
expect(@session.click_link('Normal Anchor')).to eq el
|
|
|
|
end
|
2018-06-17 13:29:46 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'can download a file', requires: [:download] do
|
2018-06-17 13:29:46 -04:00
|
|
|
# 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')
|
2018-10-09 13:29:25 -04:00
|
|
|
sleep 2 # allow time for file to download
|
2018-06-17 13:29:46 -04:00
|
|
|
expect(File).to exist(download_file)
|
|
|
|
FileUtils.rm_rf download_file
|
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|