Resources

This episode is sponsored by Honeybadger

Episode Source - https://github.com/driftingruby/417-system-tests
Download Source Code

Summary

# Terminal
bin/rails g system_test hello_world
bin/rails test:system

# test/application_system_test_case.rb
require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  using = ENV["CI"] ? :headless_chrome : :chrome
  driven_by :selenium, using: using, screen_size: [1400, 1400]

  Dir[Rails.root.join("test", "system", "support", "*_helper.rb")].each do |file|
    require file
    module_name = File.basename(file, '.rb').camelize
    include ["System", "Support", module_name].join("::").constantize
  end
end

# test/system/hello_worlds_test.rb
require "application_system_test_case"

class HelloWorldsTest < ApplicationSystemTestCase
  # test "visiting the index" do
  #   visit hello_worlds_url
  #
  #   assert_selector "h1", text: "HelloWorld"
  # end

  test "should see the hello world" do
    visit root_url
    take_screenshot
    assert_text "Hello World"
  end
end

# test/system/character_counters_test.rb
require "application_system_test_case"

class CharacterCountersTest < ApplicationSystemTestCase
  test "should see the count down of characters" do
    visit root_url
    assert_text "There are 280 characters remaining."
    find("textarea").fill_in with: "This is a test message!"
    assert_text "There are 257 characters remaining."
  end

  test "authenticated users should see the count down of characters" do
    user = users(:one)
    login_as(user.email)
    visit root_url
    assert_text "There are 280 characters remaining."
    find("textarea").fill_in with: "This is a test message!"
    assert_text "There are 257 characters remaining."
  end
end

# test/system/user_authentications_test.rb
require "application_system_test_case"

class UserAuthenticationsTest < ApplicationSystemTestCase
  test "registers new user successfully" do
    visit new_user_registration_url
    fill_in "Email", with: "john.smith@example.com"
    fill_in "Password", with: "password"
    fill_in "Password confirmation", with: "password"
    click_on "Sign up"
    assert_text "Welcome! You have signed up successfully."
  end

  test "logs in successfully" do
    user = users(:one)
    visit root_url
    click_on "Sign In"
    fill_in "Email", with: user.email
    fill_in "Password", with: "password"
    click_on "Log in"
    assert_text "Signed in successfully."
  end
end

# test/fixtures/users.yml
one:
  email: one@example.com
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>