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 RailsSystemFixture < 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)
      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:|
      rails_server.base_url
    end

    suite_fixture :playwright do
      runtime = Playwright.create(
        playwright_cli_executable_path: "./node_modules/.bin/playwright",
      )
      on_teardown { runtime.stop }
      runtime.playwright
    end

    suite_fixture :browser do |playwright:|
      browser_type = case ENV["BROWSER"]
      when "firefox"
        :firefox
      when "webkit"
        :webkit
      else
        :chromium
      end

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

      browser = playwright.send(browser_type).launch(**launch_options)
      on_teardown { browser.close }
      browser
    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
  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.



103
104
105
106
107
# File 'lib/smartest/init_rails_generator.rb', line 103

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/smartest/init_rails_generator.rb', line 109

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