Class: Smartest::InitBrowserGenerator

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

Constant Summary collapse

PLAYWRIGHT_FIXTURE =
<<~RUBY
  # frozen_string_literal: true

  require "playwright"

  class PlaywrightFixture < Smartest::Fixture
    suite_fixture :playwright do
      runtime = Playwright.create(
        playwright_cli_executable_path: "./node_modules/.bin/playwright",
      )
      cleanup { 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)
      cleanup { browser.close }
      browser
    end

    fixture :page do |browser:|
      context = browser.new_context
      cleanup { context.close }
      context.new_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_BROWSER_TEST =
<<~RUBY
  # frozen_string_literal: true

  require "test_helper"

  test("finds the smartest gem on RubyGems") do |page:|
    page.goto("https://rubygems.org/")
    page.locator("input[name='query']").fill("smartest")
    page.keyboard.press("Enter")

    page.locator("a[href='/gems/smartest']").click
    expect(page).to have_url("https://rubygems.org/gems/smartest")
    expect(page.locator(".versions")).to have_text("0.3.0.alpha1")
  end
RUBY

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of InitBrowserGenerator.



77
78
79
80
81
# File 'lib/smartest/init_browser_generator.rb', line 77

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/smartest/init_browser_generator.rb', line 83

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

  0
end