Back to Repositories

Testing JavaScript Evaluation Methods in Capybara Framework

This test suite evaluates Capybara’s JavaScript evaluation capabilities through the #evaluate_script method. It verifies script execution, argument passing, and element manipulation in web applications using Capybara’s testing framework.

Test Coverage Overview

The test suite comprehensively covers the #evaluate_script functionality in Capybara, focusing on JavaScript execution scenarios.

  • Basic script evaluation and return value verification
  • Whitespace handling in scripts
  • Argument passing to JavaScript functions
  • DOM element manipulation and interaction
  • Support for complex multi-statement scripts

Implementation Analysis

The testing approach employs RSpec-style syntax with Capybara’s session object to validate JavaScript functionality. Tests utilize a combination of direct script evaluation and DOM manipulation patterns, leveraging Capybara’s ability to interact with JavaScript-enabled pages.

  • Uses RSpec expect syntax for assertions
  • Implements before-visit patterns for page setup
  • Employs element finding and manipulation techniques

Technical Details

  • Requires JavaScript-capable driver (:js requirement)
  • Uses ES arguments feature for advanced testing
  • Implements IIFE pattern for complex JavaScript execution
  • Leverages Capybara::Node::Element for DOM interaction
  • Utilizes CSS selectors for element identification

Best Practices Demonstrated

The test suite exemplifies robust testing practices by covering various edge cases and usage patterns.

  • Isolated test cases for specific functionality
  • Clear test descriptions and expectations
  • Progressive complexity in test scenarios
  • Proper setup and teardown patterns
  • Comprehensive feature validation

teamcapybara/capybara

lib/capybara/spec/session/evaluate_script_spec.rb

            
# frozen_string_literal: true

Capybara::SpecHelper.spec '#evaluate_script', requires: [:js] do
  it 'should evaluate the given script and return whatever it produces' do
    @session.visit('/with_js')
    expect(@session.evaluate_script('1+3')).to eq(4)
  end

  it 'should ignore leading whitespace' do
    @session.visit('/with_js')
    expect(@session.evaluate_script('
      1 + 3
    ')).to eq(4)
  end

  it 'should pass arguments to the script', requires: %i[js es_args] do
    @session.visit('/with_js')
    expect(@session).to have_css('#change')
    @session.evaluate_script("document.getElementById('change').textContent = arguments[0]", 'Doodle Funk')
    expect(@session).to have_css('#change', text: 'Doodle Funk')
  end

  it 'should support passing elements as arguments to the script', requires: %i[js es_args] do
    @session.visit('/with_js')
    el = @session.find(:css, '#change')
    @session.evaluate_script('arguments[0].textContent = arguments[1]', el, 'Doodle Funk')
    expect(@session).to have_css('#change', text: 'Doodle Funk')
  end

  it 'should support returning elements', requires: %i[js es_args] do
    @session.visit('/with_js')
    @session.find(:css, '#change') # ensure page has loaded and element is available
    el = @session.evaluate_script("document.getElementById('change')")
    expect(el).to be_instance_of(Capybara::Node::Element)
    expect(el).to eq(@session.find(:css, '#change'))
  end

  it 'should support multi statement via IIFE' do
    @session.visit('/with_js')
    @session.find(:css, '#change')
    el = @session.evaluate_script(<<~JS)
      (function(){
        var el = document.getElementById('change');
        return el;
      })()
    JS
    expect(el).to eq(@session.find(:css, '#change'))
  end
end