Sha256: 8f54d4c0df50fc555fb740bb2595ff7bcc4b19f7535a63e7990e9cbc8dfe1b90

Contents?: true

Size: 1.78 KB

Versions: 14

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

14 entries across 12 versions & 2 rubygems

Version Path
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/capybara-2.18.0/lib/capybara/spec/session/sibling_spec.rb
tdiary-5.0.8 vendor/bundle/gems/tdiary-5.0.7/vendor/bundle/gems/capybara-2.16.1/lib/capybara/spec/session/sibling_spec.rb
tdiary-5.0.8 vendor/bundle/gems/capybara-2.18.0/lib/capybara/spec/session/sibling_spec.rb
capybara-2.18.0 lib/capybara/spec/session/sibling_spec.rb
capybara-2.17.0 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.0.7 vendor/bundle/gems/capybara-2.16.1/lib/capybara/spec/session/sibling_spec.rb
capybara-2.16.1 lib/capybara/spec/session/sibling_spec.rb
capybara-2.16.0 lib/capybara/spec/session/sibling_spec.rb
capybara-2.15.4 lib/capybara/spec/session/sibling_spec.rb
capybara-2.15.3 lib/capybara/spec/session/sibling_spec.rb
capybara-2.15.2 lib/capybara/spec/session/sibling_spec.rb
tdiary-5.0.6 vendor/bundle/gems/capybara-2.15.1/lib/capybara/spec/session/sibling_spec.rb
capybara-2.15.1 lib/capybara/spec/session/sibling_spec.rb
capybara-2.15.0 lib/capybara/spec/session/sibling_spec.rb