Back to Repositories

Testing Edge Browser Integration in Capybara Framework

This test suite implements Selenium WebDriver integration with Microsoft Edge browser for Capybara testing framework. It establishes browser configuration, handles driver registration, and validates core Capybara functionality with Edge-specific considerations.

Test Coverage Overview

The test suite provides comprehensive coverage of Capybara’s integration with Microsoft Edge browser through Selenium WebDriver. It validates core functionality including:

  • Browser initialization and configuration management
  • Platform-specific path handling for macOS
  • Headless mode support
  • Download path configuration
  • Session management and driver registration

Implementation Analysis

The testing approach utilizes RSpec framework with shared examples to ensure consistent behavior across different Capybara components. The implementation leverages Selenium WebDriver’s Edge-specific options and configurations, with particular attention to platform compatibility and headless operation modes.

  • Modular test organization using shared examples
  • Dynamic driver configuration based on environment
  • Platform-specific browser path handling

Technical Details

  • Primary framework: RSpec
  • Browser automation: Selenium WebDriver
  • Browser target: Microsoft Edge
  • Key dependencies: selenium-webdriver, capybara
  • Configuration: Environment-aware setup with CI integration
  • Test organization: Shared examples and spec helpers

Best Practices Demonstrated

The test suite exemplifies several testing best practices in browser automation and integration testing. It demonstrates proper separation of concerns, environment-aware configuration, and robust error handling.

  • Environment-specific configuration management
  • Selective test skipping for unsupported features
  • Proper driver cleanup and resource management
  • Structured shared examples for consistent testing

teamcapybara/capybara

spec/selenium_spec_edge.rb

            
# frozen_string_literal: true

require 'spec_helper'
require 'selenium-webdriver'
require 'shared_selenium_session'
require 'shared_selenium_node'
require 'rspec/shared_spec_matchers'

# unless ENV['CI']
#   Selenium::WebDriver::Edge::Service.driver_path = '/usr/local/bin/msedgedriver'
# end

Selenium::WebDriver.logger.ignore(:selenium_manager)

if Selenium::WebDriver::Platform.mac?
  Selenium::WebDriver::Edge.path = '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge'
end

Capybara.register_driver :selenium_edge do |app|
  # ::Selenium::WebDriver.logger.level = "debug"
  # If we don't create an options object the path set above won't be used

  # browser_options = Selenium::WebDriver::Edge::Options.new
  # browser_options.add_argument('--headless') if ENV['HEADLESS']

  browser_options = if ENV['HEADLESS']
    Selenium::WebDriver::Options.edge(args: ['--headless=new'])
  else
    Selenium::WebDriver::Options.edge
  end

  Capybara::Selenium::Driver.new(app, browser: :edge, options: browser_options).tap do |driver|
    driver.browser
    driver.download_path = Capybara.save_path
  end
end

module TestSessions
  SeleniumEdge = Capybara::Session.new(:selenium_edge, TestApp)
end

skipped_tests = %i[response_headers status_code trigger]

Capybara::SpecHelper.log_selenium_driver_version(Selenium::WebDriver::Edge) if ENV['CI']

Capybara::SpecHelper.run_specs TestSessions::SeleniumEdge, 'selenium', capybara_skip: skipped_tests do |example|
  case example.metadata[:full_description]
  when 'Capybara::Session selenium #attach_file with a block can upload by clicking the file input'
    pending "Edge doesn't allow clicking on file inputs"
  when /Capybara::Session selenium node #shadow_root should get visible text/
    pending "Selenium doesn't currently support getting visible text for shadow root elements"
  end
end

RSpec.describe 'Capybara::Session with Edge', capybara_skip: skipped_tests do
  include Capybara::SpecHelper
  ['Capybara::Session', 'Capybara::Node', Capybara::RSpecMatchers].each do |examples|
    include_examples examples, TestSessions::SeleniumEdge, :selenium_edge
  end
end