Back to Repositories

Validating Page Title Assertions in Capybara Framework

This test suite validates title assertion functionality in Capybara, focusing on both positive and negative title verification scenarios. The tests cover various aspects of title matching, including exact matches, regexp patterns, and asynchronous title changes.

Test Coverage Overview

The test suite provides comprehensive coverage of Capybara’s title assertion methods.

Key functionality includes:
  • Basic title matching with exact strings
  • Regular expression pattern matching
  • Empty string handling
  • Asynchronous title changes
  • Title normalization checks
  • Negative title assertions

Implementation Analysis

The testing approach employs RSpec’s expect syntax with both positive and negative assertion patterns.

Technical implementation features:
  • Before hooks for test setup
  • JavaScript-dependent scenarios marked with requires: [:js]
  • Wait timing configurations for async operations
  • URI manipulation for complex title scenarios

Technical Details

Testing tools and configuration:
  • Capybara test framework
  • RSpec expectation syntax
  • Addressable::URI for URL manipulation
  • JavaScript-enabled test scenarios
  • Custom wait timers for async operations
  • SpecHelper for test organization

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable practices include:
  • Isolation of test scenarios
  • Edge case coverage
  • Async operation handling
  • Clear error message validation
  • Consistent test structure
  • Proper setup and teardown patterns

teamcapybara/capybara

lib/capybara/spec/session/assert_title_spec.rb

            
# frozen_string_literal: true

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

  it "should not raise if the page's title contains the given string" do
    expect do
      @session.assert_title('js')
    end.not_to raise_error
  end

  it 'should not raise when given an empty string' do
    expect do
      @session.assert_title('')
    end.not_to raise_error
  end

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

    expect do
      @session.assert_title(/w[a-z]{10}_js/)
    end.to raise_error(Capybara::ExpectationNotMet, 'expected "with_js" to match /w[a-z]{10}_js/')
  end

  it 'should wait for title', requires: [:js] do
    @session.click_link('Change title')
    expect do
      @session.assert_title('changed title', wait: 3)
    end.not_to raise_error
  end

  it "should raise error if the title doesn't contain the given string" do
    expect do
      @session.assert_title('monkey')
    end.to raise_error(Capybara::ExpectationNotMet, 'expected "with_js" to include "monkey"')
  end

  it 'should not normalize given title' do
    @session.visit('/with_js')
    expect { @session.assert_title('  with_js  ') }.to raise_error(Capybara::ExpectationNotMet)
  end

  it 'should match correctly normalized title' do
    uri = Addressable::URI.parse('/with_title')
    uri.query_values = { title: '   with space  title   ' }
    @session.visit(uri.to_s)
    @session.assert_title('  with space  title')
    expect { @session.assert_title('with space title') }.to raise_error(Capybara::ExpectationNotMet)
  end

  it 'should not normalize given title in error message' do
    expect do
      @session.assert_title(2)
    end.to raise_error(Capybara::ExpectationNotMet, 'expected "with_js" to include "2"')
  end
end

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

  it 'should raise error if the title contains the given string' do
    expect do
      @session.assert_no_title('with_j')
    end.to raise_error(Capybara::ExpectationNotMet, 'expected "with_js" not to include "with_j"')
  end

  it 'should allow regexp matches' do
    expect do
      @session.assert_no_title(/w[a-z]{3}_js/)
    end.to raise_error(Capybara::ExpectationNotMet, 'expected "with_js" not to match /w[a-z]{3}_js/')
    @session.assert_no_title(/monkey/)
  end

  it 'should wait for title to disappear', requires: [:js] do
    @session.click_link('Change title')
    expect do
      @session.assert_no_title('with_js', wait: 3)
    end.not_to raise_error
  end

  it "should not raise if the title doesn't contain the given string" do
    expect do
      @session.assert_no_title('monkey')
    end.not_to raise_error
  end
end