Back to Repositories

Testing Page Body Content and Encoding Handling in teamcapybara/capybara

This test suite validates the body content and encoding functionality in Capybara sessions. It ensures proper page content retrieval and handles both ASCII and UTF-8 character encoding scenarios across different page responses.

Test Coverage Overview

The test suite covers core body content retrieval and encoding handling in Capybara sessions.

Key areas tested include:
  • Unmodified page body content verification
  • HTML entity encoding compatibility
  • UTF-8 encoding validation
  • ASCII to UTF-8 conversion handling

Implementation Analysis

The testing approach utilizes Capybara’s session management capabilities to validate content rendering and encoding behaviors. It implements systematic checks for both standard content retrieval and character encoding edge cases, using RSpec’s expectation syntax for assertions.

The tests employ visit and have_content methods to ensure proper page loading and content availability before performing body validations.

Technical Details

Testing tools and configuration:
  • RSpec testing framework
  • Capybara session management
  • UTF-8 encoding validation
  • HTML entity handling
  • Async content loading checks

Best Practices Demonstrated

The test suite exemplifies robust testing practices through careful handling of asynchronous content loading and encoding scenarios. It demonstrates proper test isolation, clear context separation, and thorough error handling for encoding operations.

Notable practices include:
  • Waiting for content before assertions
  • Explicit encoding error checking
  • Clear test case organization
  • Comprehensive encoding scenario coverage

teamcapybara/capybara

lib/capybara/spec/session/body_spec.rb

            
# frozen_string_literal: true

Capybara::SpecHelper.spec '#body' do
  it 'should return the unmodified page body' do
    @session.visit('/')
    expect(@session).to have_content('Hello world!') # wait for content to appear if visit is async
    expect(@session.body).to include('Hello world!')
  end

  context 'encoding of response between ascii and utf8' do
    it 'should be valid with html entities' do
      @session.visit('/with_html_entities')
      expect(@session).to have_content('Encoding') # wait for content to appear if visit is async
      expect { @session.body.encode!('UTF-8') }.not_to raise_error
    end

    it 'should be valid without html entities' do
      @session.visit('/with_html')
      expect(@session).to have_content('This is a test') # wait for content to appear if visit is async
      expect { @session.body.encode!('UTF-8') }.not_to raise_error
    end
  end
end