Sha256: 057d91bc02fcd108a4b617678a8942b71761e80a2d740eac3a33cb15e8df6892

Contents?: true

Size: 1.78 KB

Versions: 97

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

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

  after do
    Capybara::Selector.remove(:monkey)
  end

  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

  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

  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

  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

  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

  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"]')
    end.to raise_error(Capybara::ElementNotFound, 'Unable to find xpath "//div[@id=\\"nosuchthing\\"]" that is a sibling of visible css "#child"')
  end
end

Version data entries

97 entries across 83 versions & 5 rubygems

Version Path
capybara-3.20.1 lib/capybara/spec/session/sibling_spec.rb
capybara-3.20.0 lib/capybara/spec/session/sibling_spec.rb
capybara-3.19.1 lib/capybara/spec/session/sibling_spec.rb
capybara-3.19.0 lib/capybara/spec/session/sibling_spec.rb
capybara-3.18.0 lib/capybara/spec/session/sibling_spec.rb
capybara-3.15.1 lib/capybara/spec/session/sibling_spec.rb
capybara-3.17.0 lib/capybara/spec/session/sibling_spec.rb
capybara-3.16.2 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.0.13 vendor/bundle/gems/capybara-3.15.0/lib/capybara/spec/session/sibling_spec.rb
capybara-3.16.1 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.0.12.1 vendor/bundle/gems/capybara-3.15.0/lib/capybara/spec/session/sibling_spec.rb
capybara-3.16.0 lib/capybara/spec/session/sibling_spec.rb
capybara-3.15.0 lib/capybara/spec/session/sibling_spec.rb
capybara-3.14.0 lib/capybara/spec/session/sibling_spec.rb
capybara-3.13.2 lib/capybara/spec/session/sibling_spec.rb
capybara-3.13.1 lib/capybara/spec/session/sibling_spec.rb
capybara-3.13.0 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.0.11 vendor/bundle/gems/capybara-3.6.0/lib/capybara/spec/session/sibling_spec.rb
tdiary-5.0.11 vendor/bundle/gems/capybara-3.12.0/lib/capybara/spec/session/sibling_spec.rb
capybara-3.12.0 lib/capybara/spec/session/sibling_spec.rb