Back to Repositories

Validating Current Path Assertions in Capybara

This test suite validates the current path assertion functionality in Capybara, focusing on the assert_current_path and assert_no_current_path methods. The tests ensure proper URL path validation, pattern matching, and asynchronous path changes in web applications.

Test Coverage Overview

The test suite provides comprehensive coverage of path assertion scenarios in Capybara:
  • Basic path matching and validation
  • Regular expression pattern matching
  • Query parameter handling
  • Full URL comparison
  • Asynchronous path changes
  • Nil URL handling

Implementation Analysis

The testing approach employs RSpec-style specifications with before hooks and expectation blocks. The implementation leverages Capybara’s session management and URL validation features, including JavaScript-enabled tests for dynamic path changes and custom matcher options for flexible path comparison.

Technical Details

Key technical components include:
  • Capybara::SpecHelper for test organization
  • RSpec expectation syntax
  • JavaScript-enabled test scenarios
  • URL pattern matching with regular expressions
  • Session management and navigation
  • Custom assertion options (ignore_query, url)

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test cases with clear assertions
  • Comprehensive edge case coverage
  • Consistent error handling patterns
  • Proper setup and teardown management
  • Clear test descriptions and organization

teamcapybara/capybara

lib/capybara/spec/session/assert_current_path_spec.rb

            
# frozen_string_literal: true

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

  it 'should not raise if the page has the given current path' do
    expect { @session.assert_current_path('/with_js') }.not_to raise_error
  end

  it 'should allow regexp matches' do
    expect { @session.assert_current_path(/w[a-z]{3}_js/) }.not_to raise_error
  end

  it 'should wait for current_path', requires: [:js] do
    @session.click_link('Change page')
    expect { @session.assert_current_path('/with_html') }.not_to raise_error
  end

  it 'should raise if the page has not the given current_path' do
    expect { @session.assert_current_path('/with_html') }.to raise_error(Capybara::ExpectationNotMet, 'expected "/with_js" to equal "/with_html"')
  end

  it 'should check query options' do
    @session.visit('/with_js?test=test')
    expect { @session.assert_current_path('/with_js?test=test') }.not_to raise_error
  end

  it 'should compare the full url' do
    expect { @session.assert_current_path(%r{\Ahttp://[^/]*/with_js\Z}, url: true) }.not_to raise_error
  end

  it 'should ignore the query' do
    @session.visit('/with_js?test=test')
    expect { @session.assert_current_path('/with_js', ignore_query: true) }.not_to raise_error
  end

  it 'should not cause an exception when current_url is nil' do
    allow(@session).to receive(:current_url).and_return(nil)
    allow(@session.page).to receive(:current_url).and_return(nil) if @session.respond_to? :page

    expect { @session.assert_current_path(nil) }.not_to raise_error
  end
end

Capybara::SpecHelper.spec '#assert_no_current_path?' do
  before do
    @session.visit('/with_js')
  end

  it 'should raise if the page has the given current_path' do
    expect { @session.assert_no_current_path('/with_js') }.to raise_error(Capybara::ExpectationNotMet)
  end

  it 'should allow regexp matches' do
    expect { @session.assert_no_current_path(/monkey/) }.not_to raise_error
  end

  it 'should wait for current_path to disappear', requires: [:js] do
    @session.click_link('Change page')
    expect { @session.assert_no_current_path('/with_js') }.not_to raise_error
  end

  it 'should not raise if the page has not the given current_path' do
    expect { @session.assert_no_current_path('/with_html') }.not_to raise_error
  end

  it 'should not cause an exception when current_url is nil' do
    allow(@session).to receive(:current_url).and_return(nil)
    allow(@session.page).to receive(:current_url).and_return(nil) if @session.respond_to? :page

    expect { @session.assert_no_current_path('/with_html') }.not_to raise_error
  end
end