Skip to content

Instantly share code, notes, and snippets.

@bibendi
Created August 21, 2018 10:43
Show Gist options
  • Save bibendi/047e27a9fb7ed5d720e61f8c1e2b2714 to your computer and use it in GitHub Desktop.
Save bibendi/047e27a9fb7ed5d720e61f8c1e2b2714 to your computer and use it in GitHub Desktop.
Example Capybara tests
require 'rails_helper'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'site_prism'
Capybara.server = :puma, { Silent: true }
Capybara.server_host = `hostname`.strip.downcase
Capybara.server_port = 3002
Capybara.default_max_wait_time = 5
Capybara.save_path = "./tmp/capybara_output"
Capybara.always_include_port = true
Capybara.raise_server_errors = true
# Save capybara screenshots to circleci artifacts dir if present
if ENV['CIRCLE_ARTIFACTS']
Capybara.save_path = ENV['CIRCLE_ARTIFACTS']
end
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
# See https://github.com/GoogleChrome/puppeteer/issues/1645#issuecomment-356060348
CHROME_OPTIONS = %w(
--no-sandbox
--disable-background-networking
--disable-default-apps
--disable-extensions
--disable-sync
--disable-gpu
--disable-translate
--headless
--hide-scrollbars
--metrics-recording-only
--mute-audio
--no-first-run
--safebrowsing-disable-auto-update
--ignore-certificate-errors
--ignore-ssl-errors
--ignore-certificate-errors-spki-list
--user-data-dir=/tmp
).freeze
Capybara.register_driver :headless_chrome do |app|
driver =
if ENV['SELENIUM_DRIVER_URL'].present? && TcpCheck.running?(url: ENV['SELENIUM_DRIVER_URL'])
puts "\n\n🤖 Remote Chrome detected: #{ENV['SELENIUM_DRIVER_URL']}!\n\n"
Capybara::Selenium::Driver.new(
app,
browser: :remote,
url: ENV.fetch('SELENIUM_DRIVER_URL'),
desired_capabilities: :chrome
)
else
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: Selenium::WebDriver::Chrome::Options.new(
args: CHROME_OPTIONS
)
)
end
driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(1024, 740)
driver
end
Capybara.javascript_driver = Capybara.default_driver = :chrome
RSpec.configure do |config|
# Skip features configuration if we exclude feature specs
next if config.filter.opposite.rules[:type] == "feature"
if ENV['CAPYBARA_HEADLESS']
puts "\n\n🤖 Using headless Chrome!\n\n"
Capybara.javascript_driver = Capybara.default_driver = :headless_chrome
end
# Requires supporting files, such as context, patches, whatever
Dir[Rails.root.join('spec/features/support/**/*.rb')].each { |f| require f }
config.before(:suite) do
logger = Logger.new(STDOUT)
# Sprockets doesn't have a way to configure task logger(
require "rake/sprocketstask"
Rake::SprocketsTask.prepend(Module.new do
def initialize(*)
super
@logger.level = Logger::ERROR
end
end)
Rails.application.load_tasks
force_assets = ENV['FORCE_ASSETS'].present?
if Rails.application.config.assets.compile
logger.info "Use lazy assets compilation. ⚠️"
assets_path = File.join(Rails.root, "public", Rails.application.config.assets.prefix)
FileUtils.rm_rf(assets_path) if File.directory?(assets_path)
webpacker_path = Webpacker.config.public_output_path
FileUtils.rm_rf(webpacker_path) if File.directory?(webpacker_path)
next
end
if force_assets || !File.directory?(File.join(Rails.root, "public", Rails.application.config.assets.prefix))
logger.info "Starting tests assets precompilation..."
Rake::Task["assets:precompile"].invoke
# Reset assets manifest (it's loaded before we compile assets)
Rails.application.assets_manifest = ActionView::Base.assets_manifest =
Sprockets::Railtie.build_manifest(Rails.application)
logger.info "Finished tests assets precompilation. ✅"
else
logger.info "Assets precompiled. Skip. ⚠️ Use FORCE_ASSETS=1 to re-compile"
end
end
config.before(:each, type: :feature) do
WebMock.allow_net_connect!(net_http_connect_on_start: true)
end
config.after(:each, type: :feature) do
Capybara.reset_session!
allowed = []
if ENV["SELENIUM_DRIVER_URL"].present?
allowed << URI.parse(ENV.fetch("SELENIUM_DRIVER_URL")).host
end
WebMock.disable_net_connect!(allow_localhost: true, allow: allowed)
end
end
# frozen_string_literal: true
require "acceptance_helper"
feature %Q{
Admin on Event page
} do
let_it_be(:draft_event) { create(:admin_event, draft: true) }
let_it_be(:public_event) { create(:admin_event, draft: false) }
background do
login_as admin
end
scenario "can publish an event" do
visit "/admin/events/#{draft_event.slug}"
click_on "Publish"
expect(page).to have_text "Event has been published"
end
scenario "can unpublish an event" do
visit "/admin/events/#{public_event.slug}"
click_on "Unpublish"
expect(page).to have_text "Event has been unpublished"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment