Class: Smartest::InitRailsGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/smartest/init_rails_generator.rb

Constant Summary collapse

RAILS_SYSTEM_FIXTURE =
<<~RUBY
  # frozen_string_literal: true

  require 'smartest/rails'
  require "playwright"

  class RailsSystemTestFixture < Smartest::Fixture
    suite_fixture :rails_server do
      # Set the environment before loading config/environment so the test
      # server cannot boot against the development database by default.
      ENV["RAILS_ENV"] ||= "test"
      ENV["RACK_ENV"] ||= ENV["RAILS_ENV"]
      require_relative "../../config/environment"

      server = Smartest::Rails::TestServer.new(
        app: Rails.application,
        host: ENV["SMARTEST_RAILS_TEST_SERVER_HOST"],
        port: ENV["SMARTEST_RAILS_TEST_SERVER_PORT"],
      )
      server.start
      server.wait_for_ready

      on_teardown do
        server.stop
        server.wait_for_stopped
      end

      server
    end

    suite_fixture :base_url do |rails_server:|
      ENV.fetch("SMARTEST_RAILS_BASE_URL", rails_server.base_url)
    end

    suite_fixture :browser do
      ws_endpoint = ENV["PLAYWRIGHT_WS_ENDPOINT"]

      if ws_endpoint && !ws_endpoint.empty?
        playwright_execution = Playwright.connect_to_browser_server(
          ws_endpoint,
          browser_type: selected_browser_type.to_s,
        )
        on_teardown { playwright_execution.stop }

        playwright_execution.browser
      else
        playwright_execution = Playwright.create(
          playwright_cli_executable_path: ENV.fetch(
            "PLAYWRIGHT_CLI_EXECUTABLE_PATH",
            "./node_modules/.bin/playwright",
          )
        )
        on_teardown { playwright_execution.stop }

        playwright = playwright_execution.playwright
        browser = playwright.public_send(selected_browser_type).launch(**browser_launch_options)
        on_teardown { browser.close }
        browser
      end
    end

    fixture :browser_context do |base_url:, browser:|
      context = browser.new_context(baseURL: base_url)
      on_teardown { context.close }
      context
    end

    fixture :page do |browser_context:|
      page = browser_context.new_page
      on_teardown { page.close }
      page
    end

    private

    def selected_browser_type
      case ENV.fetch("BROWSER", "chromium")
      when "firefox"
        :firefox
      when "webkit"
        :webkit
      else
        :chromium
      end
    end

    def browser_launch_options
      launch_options = {}
      launch_options[:headless] = !%w[0 false].include?(ENV.fetch("HEADLESS", "true"))
      if (slow_mo = ENV.fetch("SLOW_MO", "0").to_i) > 0
        launch_options[:slowMo] = slow_mo
      end

      launch_options
    end
  end
RUBY
PLAYWRIGHT_MATCHER =
<<~RUBY
  # frozen_string_literal: true

  require "playwright"
  require "playwright/test"

  module PlaywrightMatcher
    include Playwright::Test::Matchers
  end
RUBY
EXAMPLE_RAILS_SYSTEM_TEST =
<<~RUBY
  # frozen_string_literal: true

  require "test_helper"

  test("loads the Rails application") do |page:|
    response = page.goto("/")

    expect(response.status).to be_between(200, 599)
  end
RUBY

Instance Method Summary collapse

Constructor Details

#initialize(root: Dir.pwd, output: $stdout, command_runner: nil) ⇒ InitRailsGenerator

Returns a new instance of InitRailsGenerator.



128
129
130
131
132
# File 'lib/smartest/init_rails_generator.rb', line 128

def initialize(root: Dir.pwd, output: $stdout, command_runner: nil)
  @root = root
  @output = output
  @command_runner = command_runner || method(:run_system_command)
end

Instance Method Details

#runObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/smartest/init_rails_generator.rb', line 134

def run
  Smartest::InitGenerator.new(
    root: @root,
    output: @output,
    files: smartest_files,
    final_message: nil
  ).run
  create_file("smartest/fixtures/rails_system_fixture.rb", RAILS_SYSTEM_FIXTURE)
  create_file("smartest/matchers/playwright_matcher.rb", PLAYWRIGHT_MATCHER)
  update_test_helper
  update_gemfile
  install_dependencies
  @output.puts
  @output.puts "Run your Rails browser test suite with: bundle exec smartest smartest/example_rails_system_test.rb"

  0
end