2017-07-10 17:42:15 -04:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2017-07-10 17:42:15 -04: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
|
2017-07-10 17:42:15 -04:00
|
|
|
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
|
2017-07-10 17:42:15 -04:00
|
|
|
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
|
2017-07-10 17:42:15 -04:00
|
|
|
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
|
2017-07-10 17:42:15 -04:00
|
|
|
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
|
2017-07-10 17:42:15 -04:00
|
|
|
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
|
2017-07-10 17:42:15 -04:00
|
|
|
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"')
|
2017-07-10 17:42:15 -04:00
|
|
|
end
|
|
|
|
end
|