Back to Repositories

Testing JavaScript Script Execution Workflows in teamcapybara/capybara

This test suite validates the JavaScript execution capabilities of Capybara through the #execute_script method. It ensures proper interaction between Ruby test code and JavaScript operations in web applications, focusing on script execution and argument passing.

Test Coverage Overview

The test suite covers essential JavaScript execution scenarios in Capybara, focusing on DOM manipulation and function calls.

  • Basic script execution with text content modification
  • Interaction with page-defined JavaScript functions
  • Argument passing to executed scripts
  • DOM element handling as script arguments

Implementation Analysis

The testing approach utilizes Capybara’s JavaScript driver capabilities to verify script execution behaviors. The implementation employs RSpec expectations combined with Capybara’s DOM interaction methods, demonstrating both synchronous script execution and element state verification.

Key patterns include direct DOM manipulation, jQuery usage, and element reference passing.

Technical Details

  • RSpec testing framework
  • Capybara JavaScript driver
  • DOM manipulation via native JavaScript
  • jQuery integration testing
  • ES6 argument handling support

Best Practices Demonstrated

The test suite exemplifies robust JavaScript testing practices in Ruby applications. It demonstrates proper isolation of test cases, clear expectations, and comprehensive coverage of different script execution scenarios.

  • Isolated test cases for specific functionality
  • Clear separation of concerns
  • Progressive complexity in test scenarios
  • Proper error handling validation

teamcapybara/capybara

lib/capybara/spec/session/execute_script_spec.rb

            
# frozen_string_literal: true

Capybara::SpecHelper.spec '#execute_script', requires: [:js] do
  it 'should execute the given script and return nothing' do
    @session.visit('/with_js')
    expect(@session.execute_script("document.getElementById('change').textContent = 'Funky Doodle'")).to be_nil
    expect(@session).to have_css('#change', text: 'Funky Doodle')
  end

  it 'should be able to call functions defined in the page' do
    @session.visit('/with_js')
    expect { @session.execute_script("$('#change').text('Funky Doodle')") }.not_to raise_error
  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.execute_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.execute_script('arguments[1].textContent = arguments[0]', 'Doodle Funk', el)
    expect(@session).to have_css('#change', text: 'Doodle Funk')
  end
end