Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save senid231/2919118fd33d57fff574de40b7c0a5a8 to your computer and use it in GitHub Desktop.
Save senid231/2919118fd33d57fff574de40b7c0a5a8 to your computer and use it in GitHub Desktop.
Bug report template for active admin and capybara with CSS and JS
# frozen_string_literal: true
# see https://github.com/activeadmin/activeadmin/blob/master/tasks/bug_report_template.rb
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
# Use local changes or ActiveAdmin master.
if ENV['ACTIVE_ADMIN_PATH']
gem 'activeadmin', path: ENV['ACTIVE_ADMIN_PATH'], require: false
elsif ENV['ACTIVE_ADMIN_VERSION']
gem 'activeadmin', ENV['ACTIVE_ADMIN_VERSION'], require: false
else
gem 'activeadmin', github: 'activeadmin/activeadmin', require: false
end
# Change Rails version if necessary.
gem 'rails', '6.1.4'
gem 'sprockets', '3.7.2'
gem 'sassc-rails', '2.1.2'
gem 'sqlite3', '1.4.2'
gem 'capybara'
gem 'selenium-webdriver', require: false
gem 'cuprite'
gem 'puma'
end
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Schema.define do
create_table :active_admin_comments, force: true do |_t|
end
create_table :organizations, force: true do |t|
t.string :name, null: false
end
create_table :users, force: true do |t|
t.string :name, null: false
t.boolean :active, null: false
t.references :organization, null: false
end
end
require 'action_controller/railtie'
require 'action_view/railtie'
require 'active_admin'
class TestApp < Rails::Application
config.root = __dir__
config.session_store :cookie_store, key: 'cookie_store_key'
secrets.secret_token = 'secret_token'
secrets.secret_key_base = 'secret_key_base'
config.eager_load = false
config.logger = Logger.new($stdout)
Rails.logger = config.logger
config.hosts = '127.0.0.1'
active_admin_root = File.expand_path '../../', ActiveAdmin.method(:setup).source_location[0]
assets_path = File.join(active_admin_root, 'lib/generators/active_admin/assets/templates')
config.assets.paths = [assets_path]
end
TestApp.routes.default_url_options = {
host: '127.0.0.1'
}
class ApplicationController < ActionController::Base
include Rails.application.routes.url_helpers
end
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class Organization < ApplicationRecord
end
class User < ApplicationRecord
belongs_to :organization
end
ActiveAdmin.setup do |config|
# Authentication disabled by default. Override if necessary.
config.authentication_method = false
config.current_user_method = false
end
Rails.application.initialize!
ActiveAdmin.register_page 'Dashboard' do
menu priority: 1, label: proc { I18n.t('active_admin.dashboard') }
content do
'Test Me'
end
end
ActiveAdmin.register User do
form do |f|
f.inputs do
f.input :name
f.input :active
f.input :organization_id, as: :select, collection: Organization.all
end
f.submit
end
end
Rails.application.routes.draw do
ActiveAdmin.routes(self)
end
require 'minitest/autorun'
require 'rack/test'
require 'rails/test_help'
require 'capybara/rails'
require 'capybara/minitest'
require 'capybara/cuprite'
Capybara.server = :puma
Capybara.server_host = '127.0.0.1'
Capybara.javascript_driver = :cuprite
Capybara.register_driver(:cuprite) do |app|
Capybara::Cuprite::Driver.new(
app,
window_size: [1920, 1080],
js_errors: true
)
end
class BugTest < ActionDispatch::SystemTestCase
include Rails.application.routes.url_helpers
driven_by Capybara.javascript_driver
test 'checks layout' do
Organization.create!(
[
{ name: 'Some Org' },
{ name: 'Some Other Org' },
{ name: 'Different Org' }
]
)
visit new_admin_user_url
assert_text 'Users'
save_screenshot 'test'
end
def save_screenshot(name, folder = './')
name = "#{name}.png" unless name.end_with?('.png')
full_path = File.expand_path(name, folder)
Capybara.page.save_screenshot(full_path)
puts "[Screenshot]: #{full_path}" # rubocop:disable Rails/Output
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment