1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/spec/session/sibling_spec.rb

53 lines
1.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-02-28 19:11:41 -05:00
Capybara::SpecHelper.spec '#sibling' do
before do
@session.visit('/with_html')
end
after do
Capybara::Selector.remove(:monkey)
end
2018-07-10 17:18:39 -04:00
it 'should find a prior sibling element using the given locator' do
el = @session.find(:css, '#mid_sibling')
expect(el.sibling('//div[@data-pre]')[:id]).to eq('pre_sibling')
end
2018-07-10 17:18:39 -04:00
it 'should find a following sibling element using the given locator' do
el = @session.find(:css, '#mid_sibling')
expect(el.sibling('//div[@data-post]')[:id]).to eq('post_sibling')
end
2018-07-10 17:18:39 -04:00
it 'should raise an error if there are multiple matches' do
el = @session.find(:css, '#mid_sibling')
expect { el.sibling('//div') }.to raise_error(Capybara::Ambiguous)
end
2018-07-10 17:18:39 -04:00
context 'with css selectors' do
it 'should find the first element using the given locator' do
el = @session.find(:css, '#mid_sibling')
expect(el.sibling(:css, '#pre_sibling')).to have_text('Pre Sibling')
expect(el.sibling(:css, '#post_sibling')).to have_text('Post Sibling')
end
end
2018-07-10 17:18:39 -04:00
context 'with custom selector' do
it 'should use the custom selector' do
Capybara.add_selector(:data_attribute) do
xpath { |attr| ".//*[@data-#{attr}]" }
end
el = @session.find(:css, '#mid_sibling')
expect(el.sibling(:data_attribute, 'pre').text).to eq('Pre Sibling')
expect(el.sibling(:data_attribute, 'post').text).to eq('Post Sibling')
end
end
2018-07-10 17:18:39 -04:00
it 'should raise ElementNotFound with a useful default message if nothing was found' do
el = @session.find(:css, '#child')
expect do
el.sibling(:xpath, '//div[@id="nosuchthing"]')
2018-07-10 17:18:39 -04:00
end.to raise_error(Capybara::ElementNotFound, 'Unable to find xpath "//div[@id=\\"nosuchthing\\"]" that is a sibling of visible css "#child"')
end
end