Back to Repositories

Testing Text Visibility and Extraction Workflows in Capybara

This test suite evaluates Capybara’s text handling capabilities, focusing on visible and hidden text elements in web pages. The tests verify text extraction, visibility handling, and normalization behavior across different configurations and scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of Capybara’s text handling functionality:

  • Basic text extraction from web pages
  • Visibility handling for hidden elements
  • Text normalization and whitespace handling
  • Configuration-based text visibility controls

Implementation Analysis

The testing approach employs Capybara’s session-based testing pattern, utilizing various configuration options and selectors. Tests demonstrate both default behavior and explicit visibility settings, with particular attention to the interaction between global configuration and local text extraction options.

Technical Details

  • Uses RSpec testing framework
  • Leverages Capybara’s SpecHelper
  • Tests both CSS and XPath selectors
  • Implements visibility control through Capybara.ignore_hidden_elements
  • Utilizes text normalization and whitespace handling features

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolation of test cases
  • Configuration state management
  • Edge case coverage
  • Clear test case organization
  • Comprehensive visibility handling scenarios

teamcapybara/capybara

lib/capybara/spec/session/text_spec.rb

            
# frozen_string_literal: true

Capybara::SpecHelper.spec '#text' do
  it 'should print the text of the page' do
    @session.visit('/with_simple_html')
    expect(@session.text).to eq('Bar')
  end

  it 'ignores invisible text by default' do
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is')
  end

  it 'shows invisible text if `:all` given' do
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden-text').text(:all)).to eq('Some of this text is hidden!')
  end

  it 'ignores invisible text if `:visible` given' do
    Capybara.ignore_hidden_elements = false
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden-text').text(:visible)).to eq('Some of this text is')
  end

  it 'ignores invisible text if `Capybara.ignore_hidden_elements = true`' do
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is')
    Capybara.ignore_hidden_elements = false
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is hidden!')
  end

  it 'ignores invisible text if `Capybara.visible_text_only = true`' do
    @session.visit('/with_html')
    Capybara.visible_text_only = true
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is')
    Capybara.ignore_hidden_elements = false
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is')
  end

  it 'ignores invisible text if ancestor is invisible' do
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden_via_ancestor', visible: false).text).to eq('')
  end

  context 'with css as default selector' do
    before { Capybara.default_selector = :css }

    after { Capybara.default_selector = :xpath }

    it 'should print the text of the page' do
      @session.visit('/with_simple_html')
      expect(@session.text).to eq('Bar')
    end
  end

  it 'should be correctly normalized when visible' do
    @session.visit('/with_html')
    el = @session.find(:css, '#normalized')
    expect(el.text).to eq "Some text\nMore text\nAnd more text\nEven more    text on multiple lines"
  end

  it 'should be a textContent with irrelevant whitespace collapsed when non-visible' do
    @session.visit('/with_html')
    el = @session.find(:css, '#non_visible_normalized', visible: false)
    expect(el.text(:all)).to eq 'Some textMore text And more text Even more    text on multiple lines'
  end

  it 'should strip correctly' do
    @session.visit('/with_html')
    el = @session.find(:css, '#ws')
    expect(el.text).to eq ' '
    expect(el.text(:all)).to eq ' '
  end
end