-
-
Save zdennis/54c50221589965e223258ded8b7a2e8c to your computer and use it in GitHub Desktop.
Mutually Human Blog Post: Rails 5 API + Angular 5/6 + Capybara Code Snippets Part 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'capybara/rails' | |
require 'capybara_spa' | |
require 'selenium/webdriver' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.register_driver :headless_chrome do |app| | |
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( | |
chromeOptions: chrome_options.merge(args: %w(headless disable-gpu)), | |
loggingPrefs: logging_preferences | |
) | |
Capybara::Selenium::Driver.new( | |
app, | |
browser: :chrome, | |
desired_capabilities: capabilities | |
) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.app_host = "http://localhost:#{FrontendServer.port}" | |
Capybara.always_include_port = true | |
Capybara.default_max_wait_time = 10 | |
Capybara.javascript_driver = :chrome | |
Capybara.server = :puma | |
Capybara.server_port = 3001 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.app_host = "http://localhost:#{FrontendServer.port}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.always_include_port = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.default_max_wait_time = 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.javascript_driver = :chrome |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.server = :puma |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.server_port = 3001 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# capybara.rb is responsible for configuring Capybara. | |
# | |
# It sets up two different JS drivers: | |
# | |
# * :chrome | |
# * :headless_chrome | |
# | |
# It hard codes an assumption that the Rails app runs on port 3001. | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FrontendServer = CapybaraSpa::Server::NgStaticServer.new( | |
build_path: File.dirname(__FILE__) + '/../../frontend/dist/frontend', | |
http_server_bin_path: File.dirname(__FILE__) + '/../../frontend/node_modules/.bin/angular-http-server', | |
log_file: File.dirname(__FILE__) + '/../../log/angular-process.log', | |
pid_file: File.dirname(__FILE__) + '/../../tmp/angular-process.pid' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.configure do |config| | |
config.before(:each) do |example| | |
if self.class.metadata[:js] | |
begin | |
FrontendServer.start unless FrontendServer.started? | |
rescue CapybaraSpa::Server::Error => ex | |
# When an exception is raised it is being swallowed | |
# so print it out and forcefully fail so the developer | |
# see its. | |
STDERR.puts ex.message, ex.backtrace.join("\n") | |
exit! | |
end | |
end | |
end | |
config.after(:suite) do | |
FrontendServer.stop if FrontendServer.started? | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.before(:each) do |example| | |
if self.class.metadata[:js] | |
begin | |
FrontendServer.start unless FrontendServer.started? | |
rescue CapybaraSpa::Server::Error => ex | |
# When an exception is raised it is being swallowed | |
# so print it out and forcefully fail so the developer | |
# see its. | |
STDERR.puts ex.message, ex.backtrace.join("\n") | |
exit! | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.after(:suite) do | |
FrontendServer.stop if FrontendServer.started? | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This env var comes from the heroku-buildpack-google-chrome | |
chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil) | |
# This env var comes from chromedriver_linux, e.g. TravisCI | |
chrome_bin ||= ENV.fetch('CHROME_BIN', nil) | |
chrome_options = {} | |
chrome_options[:binary] = chrome_bin if chrome_bin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Give us access to browser console logs, see spec/support/browser_logging.rb | |
logging_preferences = { browser: 'ALL' } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.register_driver :chrome do |app| | |
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( | |
chromeOptions: chrome_options, | |
loggingPrefs: logging_preferences | |
) | |
Capybara::Selenium::Driver.new( | |
app, | |
browser: :chrome, | |
desired_capabilities: capabilities | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment