Back to Repositories

Testing HTML Content Retrieval Methods in teamcapybara/capybara

This test suite validates Capybara’s HTML content retrieval methods including html(), source(), and body(). It ensures proper page content extraction and dynamic content handling in both static and JavaScript-enabled environments.

Test Coverage Overview

The test suite comprehensively covers three core Capybara methods for accessing page content: html(), source(), and body().

  • Tests both static and JavaScript-modified page content
  • Verifies content presence and absence
  • Handles dynamic page state changes
  • Validates unmodified page source retrieval

Implementation Analysis

The implementation uses RSpec-style testing patterns with Capybara’s SpecHelper framework. Tests employ sleep synchronization for JavaScript scenarios, though the code notes this is not the recommended approach for typical Capybara testing.

  • Utilizes Capybara::SpecHelper.spec blocks
  • Implements requires: [:js] for JavaScript-dependent tests
  • Uses expect() assertions for content verification

Technical Details

  • Testing Framework: RSpec with Capybara
  • Test Environment: Supports both JavaScript and non-JavaScript scenarios
  • Key Methods: html(), source(), body()
  • Session Management: Uses @session for page interaction
  • Synchronization: Uses sleep for JavaScript tests

Best Practices Demonstrated

The test suite demonstrates thorough method validation while highlighting important testing considerations.

  • Clear test case separation for different methods
  • Explicit testing of both positive and negative cases
  • Documentation of non-recommended practices (sleep usage)
  • Consistent testing pattern across similar methods

teamcapybara/capybara

lib/capybara/spec/session/html_spec.rb

            
# frozen_string_literal: true

# NOTE: This file uses `sleep` to sync up parts of the tests. This is only implemented like this
# because of the methods being tested. In tests using Capybara this type of behavior should be implemented
# using Capybara provided assertions with builtin waiting behavior.

Capybara::SpecHelper.spec '#html' do
  it 'should return the unmodified page body' do
    @session.visit('/')
    expect(@session.html).to include('Hello world!')
  end

  it 'should return the current state of the page', requires: [:js] do
    @session.visit('/with_js')
    sleep 1
    expect(@session.html).to include('I changed it')
    expect(@session.html).not_to include('This is text')
  end
end

Capybara::SpecHelper.spec '#source' do
  it 'should return the unmodified page source' do
    @session.visit('/')
    expect(@session.source).to include('Hello world!')
  end

  it 'should return the current state of the page', requires: [:js] do
    @session.visit('/with_js')
    sleep 1
    expect(@session.source).to include('I changed it')
    expect(@session.source).not_to include('This is text')
  end
end

Capybara::SpecHelper.spec '#body' do
  it 'should return the unmodified page source' do
    @session.visit('/')
    expect(@session.body).to include('Hello world!')
  end

  it 'should return the current state of the page', requires: [:js] do
    @session.visit('/with_js')
    sleep 1
    expect(@session.body).to include('I changed it')
    expect(@session.body).not_to include('This is text')
  end
end