Back to Repositories

Testing Sibling Element Detection Implementation in Capybara

This test suite validates Capybara’s sibling element detection capabilities in web applications. It covers both positive and negative sibling assertions, with comprehensive checks for element relationships and count validation.

Test Coverage Overview

The test suite thoroughly examines sibling element relationships using Capybara’s DOM traversal features.

Key areas tested include:
  • Prior sibling element detection
  • Following sibling element detection
  • Multiple sibling matching scenarios
  • Sibling count validation
  • Negative sibling assertions

Implementation Analysis

The implementation uses RSpec’s expectation syntax with Capybara’s element finding and matching capabilities. The tests demonstrate both positive (#have_sibling) and negative (#have_no_sibling) matching patterns, utilizing CSS selectors for element identification and precise count specifications.

The approach leverages Capybara’s session management and element finding methods, combined with RSpec’s assertion framework.

Technical Details

Testing components include:
  • Capybara::SpecHelper for test organization
  • RSpec matchers and expectations
  • CSS selectors for element targeting
  • Session management with @session
  • Before hooks for test setup
  • Explicit error checking with raise_error matcher

Best Practices Demonstrated

The test suite exhibits several testing best practices:

  • Isolated test cases with clear assertions
  • Proper setup using before blocks
  • Comprehensive error scenario coverage
  • Explicit element count validation
  • Both positive and negative assertion testing
  • Clean and maintainable test structure

teamcapybara/capybara

lib/capybara/spec/session/has_sibling_spec.rb

            
# frozen_string_literal: true

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

  it 'should assert a prior sibling element using the given locator' do
    el = @session.find(:css, '#mid_sibling')
    expect(el).to have_sibling(:css, '#pre_sibling')
  end

  it 'should assert a following sibling element using the given locator' do
    el = @session.find(:css, '#mid_sibling')
    expect(el).to have_sibling(:css, '#post_sibling')
  end

  it 'should not raise an error if there are multiple matches' do
    el = @session.find(:css, '#mid_sibling')
    expect(el).to have_sibling(:css, 'div')
  end

  it 'should allow counts to be specified' do
    el = @session.find(:css, '#mid_sibling')

    expect(el).to have_sibling(:css, 'div').exactly(2).times
    expect do
      expect(el).to have_sibling(:css, 'div').once
    end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
  end
end

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

  it 'should assert no matching sibling' do
    el = @session.find(:css, '#mid_sibling')
    expect(el).to have_no_sibling(:css, '#not_a_sibling')
    expect(el).not_to have_sibling(:css, '#not_a_sibling')
  end

  it 'should raise if there are matching siblings' do
    el = @session.find(:css, '#mid_sibling')
    expect do
      expect(el).to have_no_sibling(:css, '#pre_sibling')
    end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
  end
end