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
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/capybara-3.40.0/lib/capybara/spec/session/sibling_spec.rb
capybara-3.40.0 lib/capybara/spec/session/sibling_spec.rb
capybara-3.39.2 lib/capybara/spec/session/sibling_spec.rb
capybara-3.39.1 lib/capybara/spec/session/sibling_spec.rb
capybara-3.39.0 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/capybara-3.36.0/lib/capybara/spec/session/sibling_spec.rb
capybara-3.38.0 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/capybara-3.37.1/lib/capybara/spec/session/sibling_spec.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/capybara-3.37.1/lib/capybara/spec/session/sibling_spec.rb
capybara-3.37.1 lib/capybara/spec/session/sibling_spec.rb
capybara-3.37.0 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/capybara-3.36.0/lib/capybara/spec/session/sibling_spec.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/capybara-3.36.0/lib/capybara/spec/session/sibling_spec.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/capybara-3.35.3/lib/capybara/spec/session/sibling_spec.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/capybara-3.36.0/lib/capybara/spec/session/sibling_spec.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/capybara-3.36.0/lib/capybara/spec/session/sibling_spec.rb
capybara-3.36.0 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/capybara-3.35.3/lib/capybara/spec/session/sibling_spec.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/capybara-3.35.3/lib/capybara/spec/session/sibling_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/capybara-3.35.3/lib/capybara/spec/session/sibling_spec.rb