Back to Repositories

Testing Element Detection Methods in Capybara

This test suite validates Capybara’s element detection capabilities through the #has_element? and #has_no_element? methods. It ensures proper handling of HTML elements with various attributes and locator strategies.

Test Coverage Overview

The test suite provides comprehensive coverage of element presence verification in web pages using Capybara.

Key areas tested include:
  • Positive element detection with multiple attributes
  • Negative element detection scenarios
  • Regular expression matching for href attributes
  • Invalid locator handling and warning systems

Implementation Analysis

The testing approach uses RSpec-style expectations with Capybara’s session object to verify element presence. The implementation demonstrates both positive and negative testing patterns, utilizing Capybara’s DSL for web element interaction.

Notable patterns include:
  • Before hooks for page setup
  • Multiple attribute matching
  • Regular expression support
  • Warning message verification

Technical Details

Testing tools and configuration:
  • Capybara::SpecHelper for test organization
  • RSpec expectation syntax
  • Mock objects for warning verification
  • HTML test fixtures (‘/with_html’ page)

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through organized, comprehensive test cases. Notable practices include:
  • Isolated test scenarios
  • Clear test case descriptions
  • Both positive and negative assertions
  • Edge case handling
  • Error condition testing

teamcapybara/capybara

lib/capybara/spec/session/has_element_spec.rb

            
# frozen_string_literal: true

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

  it 'should be true if the given element is on the page' do
    expect(@session).to have_element('a', id: 'foo')
    expect(@session).to have_element('a', text: 'A link', href: '/with_simple_html')
    # Should `text` actually accept a symbol?
    # expect(@session).to have_element('a', text: :'A link', href: :'/with_simple_html')
    expect(@session).to have_element('a', text: 'A link', href: %r{/with_simple_html})
    expect(@session).to have_element('a', text: 'labore', target: '_self')
  end

  it 'should be false if the given element is not on the page' do
    expect(@session).not_to have_element('a', text: 'monkey')
    expect(@session).not_to have_element('a', text: 'A link', href: '/nonexistent-href')
    expect(@session).not_to have_element('a', text: 'A link', href: /nonexistent/)
    expect(@session).not_to have_element('a', text: 'labore', target: '_blank')
  end

  it 'should notify if an invalid locator is specified' do
    allow(Capybara::Helpers).to receive(:warn).and_return(nil)
    @session.has_element?(@session)
    expect(Capybara::Helpers).to have_received(:warn).with(/Called from: .+/)
  end
end

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

  it 'should be false if the given element is on the page' do
    expect(@session).not_to have_no_element('a', id: 'foo')
    expect(@session).not_to have_no_element('a', text: 'A link', href: '/with_simple_html')
    expect(@session).not_to have_no_element('a', text: 'labore', target: '_self')
  end

  it 'should be true if the given element is not on the page' do
    expect(@session).to have_no_element('a', text: 'monkey')
    expect(@session).to have_no_element('a', text: 'A link', href: '/nonexistent-href')
    expect(@session).to have_no_element('a', text: 'A link', href: %r{/nonexistent-href})
    expect(@session).to have_no_element('a', text: 'labore', target: '_blank')
  end
end