Capybara::Poltergeist Status error, failed to reach server - ruby-on-rails-4

I am trying to make some tests with Rspec/Capybara/Poltergeist on my Rails 4 application, Phantomjs is installed (version 2.2.1), but I always get this error :
Failure/Error: visit(perfect_landing_page_path)
Capybara::Poltergeist::StatusFailError:
Request to 'http://127.0.0.1:49623/path' failed to reach server, check DNS
and/or server status
The test i'm working on :
require 'rails_helper'
RSpec.feature 'Subscription', :type => :feature do
let!(:plan) { create(:plan) }
let!(:landing_page) { create(:landing_page) }
before(:each) { landing_page.default_plan = plan }
describe 'landing_page#perfect_show' do
scenario 'form display', js: true do
plan_2 = create(:plan)
plan_3 = create(:plan)
landing_page.plans << plan_2
landing_page.plans << plan_3
visit(perfect_landing_page_path)
expect(page).to have_css(".start-now", count: 3)
first(".start-now").click
expect(page).to have_css("#new_user")
end
end
end
My Gemfile looks like this :
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
gem 'guard-rspec'
group :test do
gem 'database_cleaner'
gem 'capybara'
gem 'capybara-screenshot'
gem 'poltergeist'
gem 'selenium-webdriver'
gem 'shoulda-matchers', require: false
gem 'show_me_the_cookies'
end
My spec/support/capybara.rb file :
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require "capybara/poltergeist" # Add this line to require poltergeist
require 'selenium-webdriver'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {:js_errors => false, :default_max_wait_time => 30, :timeout => 30, phantomjs_options: [
'--load-images=no',
'--ignore-ssl-errors=true',
'--ssl-protocol=any']})
end
Capybara.register_driver :poltergeist_debug do |app|
Capybara::Poltergeist::Driver.new(app, :inspector => true)
end
Capybara.configure do |config|
config.javascript_driver = :poltergeist
config.ignore_hidden_elements = true
config.default_max_wait_time = 30
end
My spec_helper.rb file :
require 'capybara/rspec'
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.filter_run :focus
config.run_all_when_everything_filtered = true
Did someone encounter this problem before ? Does anyone have a solution for this ? I have been banging my head for days looking on the web...
Thank you very much.
PS : it works fine on my friend's mac (Yosemite or El Capitan), just not on mine.

I was having this problem in CI (CodeShip).
Adding a call to bundle exec rake assets:precompile at the end of the setup commands seemed to fix it:
CodeShip CI Setup Commands:
rvm use 2.3.4 --install
bundle install
export RAILS_ENV=test
bundle exec rake db:schema:load
bundle exec rake assets:precompile
Also had a whitelist per #agbodike but in the rails_helper.rb
config.before(:each, js: true) do
page.driver.browser.url_whitelist = ["127.0.0.1"]
end

I had the same issue and it was due to a 3rd party script timing out. You can prevent loading of 3rd party scripts with a blacklist. For example:
config.before(:each, js: true) do
page.driver.browser.url_blacklist = ["http://use.typekit.net"]
end
would prevent any URL starting with http://use.typekit.net from being called for each test that uses the js driver. More information can be found at:
https://robots.thoughtbot.com/speed-up-javascript-capybara-specs-by-blacklisting-urls
Alternatively you can use a whitelist:
config.before(:each, js: true) do
page.driver.browser.url_whitelist = ["127.0.0.1"]
end
which will block all requests not to 127.0.0.1
I placed the configuration in spec/feature_helper.rb to ensure it was only set for feature specs.

You can avoid these by precompile the assets before tests.
And the code is:
RSpec.configure do |config|
config.before :all do
ENV['PRECOMPILE_ASSETS'] ||= begin
case self.class.metadata[:type]
when :feature, :view
STDOUT.write "Precompiling assets..."
require 'rake'
Rails.application.load_tasks
Rake::Task['assets:precompile'].invoke
STDOUT.puts " done."
Time.now.to_s
end
end
end
end
more info

Related

PG::ConnectionBad: connection is closed after upgrading rails from 4.2 to 5.2

I have upgraded my rails version of a project from 4.2 to 5.2.1 some of my tests are failed because of pg connection closed error on the full trace is is as following.
Failure/Error: DatabaseCleaner[:active_record].clean_with(:truncation)
ActiveRecord::StatementInvalid:
PG::ConnectionBad: connection is closed: TRUNCATE TABLE "public"."alerts", "public"."article_attachments", "public"."article_check_specification_tolerances", "public"."article_machine_part_settings", "public"."articles", "public"."attachments", "public"."check_batches", "public"."check_groups", "public"."check_specification_machine_types", "public"."check_specification_priorities", "public"."check_specification_responsibility_areas", "public"."check_specifications", "public"."checks", "public"."comments", "public"."counters", "public"."customers", "public"."defect_groups", "public"."defect_translations", "public"."defects", "public"."delayed_jobs", "public"."furnaces", "public"."gv_area_equipments", "public"."gv_areas", "public"."gv_components", "public"."gv_entries", "public"."gv_equipment_families", "public"."gv_equipments", "public"."gv_squads", "public"."gv_stop_reasons", "public"."gv_sub_equipment_components", "public"."gv_sub_equipments", "public"."job_specifications", "public"."jobs", "public"."lab_recipe_versions", "public"."lab_recipes", "public"."lines", "public"."machine_downtimes", "public"."machine_groups", "public"."machine_part_change_reasons", "public"."machine_part_changes", "public"."machine_part_translations", "public"."machine_parts", "public"."machine_type_group_machine_types", "public"."machine_type_groups", "public"."machine_types", "public"."messages", "public"."mold_sets", "public"."packing_schemes", "public"."rails_admin_settings", "public"."reasons", "public"."rejects", "public"."responsibility_areas", "public"."roles", "public"."settings", "public"."shift_definitions", "public"."shifts", "public"."system_log_entries", "public"."task_status_changes", "public"."tasks", "public"."tresholds", "public"."user_responsibility_areas", "public"."users", "public"."users_roles", "public"."workstations", "public"."machines", "public"."systematic_rejects", "public"."systematic_reject_machines" RESTART IDENTITY CASCADE;
and my rspec configurations are
RSpec.configure do |config|
config.around(:each) do |example|
DatabaseCleaner[:active_record].clean_with(:truncation)
# DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.cleaning do
example.run
end
end
end
strange thing is I can run tests one by one directory like
rspec spec/controllers/
rspec spec/contexts/
rspec spec/models
but it does not work when I try to run all in one with just
rspec spec/featues/
my feature_helper.rb is
require 'spec_helper'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'capybara/rails'
require 'rack/handler/puma'
require 'support/shared_activerecord_connection'
require 'support/feature_macros'
Capybara.register_server :puma do |app, port, host|
require 'rack/handler/puma'
Capybara.asset_host = "http://#{host}:#{port}"
Rack::Handler::Puma.run(app, Host: host, Port: port, Threads: '0:4', Silent: true, config_files: ['-'])
end
Capybara.configure do |config|
config.server = :puma
end
RSpec.configure do |config|
config.before(:suite) do
Rails.application.load_tasks
Rake::Task['assets:precompile'].invoke
end
end
require 'spec_helper'
require 'counter_column_fix'
RedisStore.class_eval do
def self.new_instance
$redis = Redis.new(Rails.configuration.redis_config)
end
end
what should I do to remove this error?
DatabaseCleaner isn’t generally needed with Rails 5.1+ . Remove all references to it from your project and enable transactional testing in your RSpec config

No such file or directory - connect(2) for "/tmp/puma-status-1439451994589-14316"

I want to deploy my rails through Capistrano with Puma and Nginx. I have configured deploy.rb for puma and added required gems in gem files.
I am able to run initial deploy command as 'cap production deploy:initial' and able to access my rails app as described below.
But when I want to deploy some new changes or restart puma it fails and gave this error.
Gemfile:
gem 'capistrano', '~> 3.4.0'
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
# gem 'capistrano-passenger', require: false
gem 'capistrano-ext', require: false
gem 'capistrano-faster-assets', '~> 1.0.2'
Capfile:
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/faster_assets'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/puma/workers'
require 'capistrano/puma/nginx'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
deploy.rb
# Puma Server Configuration
set :puma_threads, [4, 16]
set :puma_workers, 1
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"
# set :puma_conf, "#{shared_path}/puma.rb"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/capistrano`
puts "WARNING: HEAD is not the same as origin/capistrano"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
I have also used the below command to generate template for Puma and Nginx as below.
rails g capistrano:nginx_puma:config
I have run below commands to deploy my rails to EC2 instance (with Ubuntu)
cap production deploy:check
cap production puma:config
cap production puma:nginx_config
cap production deploy:initial
Now, I want to deploy some changes with below code.
cap production deploy
But I am getting the error as below.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as ubuntu#54.175.134.149: bundle exit status: 1
bundle stdout: No such file or directory - connect(2) for "/tmp/puma-status-1439451994589-14316"
bundle stderr: Nothing written
SSHKit::Command::Failed: bundle exit status: 1
bundle stdout: No such file or directory - connect(2) for "/tmp/puma-status-1439451994589-14316"
bundle stderr: Nothing written
Tasks: TOP => deploy:restart
(See full trace by running task with --trace)
Please help!
Thanks
I found one workaround to fix this issue.
Just add the snippet below to your deploy.rb file.
It will override puma restart task.
Rake::Task["puma:restart"].clear_actions
namespace :puma do
task :restart do
on roles(:all) do
execute "RACK_ENV=#{fetch(:rails_env)} #{fetch(:rvm_binary)} #{fetch(:rvm_ruby_version)} do pumactl -S #{shared_path}/tmp/pids/puma.state restart"
end
end
end

Rails, ember-cli-rails gem

I tried to deploy a Rails & Ember-cli application to a VPS by Capistrano. I have integrated Ember-cli into Rails by using ember-cli-rails gem. The problem happens when Capistrano wants to do assets:precompile. Throwing an error like EmberCLI Rails requires your Ember app to have an addon. In this case, I am able to run assets:precompile on my local machine and on the server in the released directory without any problem. On server I have installed `Nodejs and NPM.
deploy.rb
lock '3.1.0'
set :application, 'APPLICATION NAME'
set :repo_url, 'GIT ADDRESS'
set :deploy_to, 'SERVER SIDE ADDRESS'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
end
end
end
Capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rvm'
set :rvm_type, :user
set :rvm_ruby_version, '2.2.0'
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Related Gems
gem 'rails', '4.2.0'
gem 'capistrano', '~> 3.1.0'
gem 'capistrano-bundler', '~> 1.1.2'
gem 'capistrano-rails', '~> 1.1.1'
gem 'capistrano-rvm', github: "capistrano/rvm"
any help would be appreciated.
I just fixed this problem by adding some tasks to my deploy.rb file.
desc "Build Ember addon"
task :build_ember_addon do
on roles(:app) do
execute "cd #{release_path}/frontend && /home/$USER/.nvm/versions/node/v0.12.2/bin/npm install --save-dev ember-cli-rails-addon#0.0.11"
end
end
desc "Intall npm"
task :npm_install do
on roles(:app) do
execute "cd #{release_path}/frontend && /home/$USER/.nvm/versions/node/v0.12.2/bin/npm install"
end
end
desc "bower install"
task :bower_install do
on roles(:app) do
execute "cd #{release_path}/frontend && /home/$USER/.nvm/versions/node/v0.12.2/bin/bower install --config.interactive=false"
end
end
before 'deploy:updated', 'deploy:build_ember_addon'
before 'deploy:updated', 'deploy:npm_install'
before 'deploy:updated', 'deploy:bower_install'

Asset_Sync not pushing to S3

I am building a rails app on heroku, and want to deploy the js, css, and image files to a bucket on Amazon. I haven't found many resources for this, but I am using this (2012) tutorial for guidance; https://firmhouse.com/blog/complete-guide-to-serving-your-rails-assets-over-s3-with-asset_sync
The site is mainly css and js at the moment. Here is my code so far;
production.rb
Rails.application.configure do
config.action_controller.asset_host = "http://localize.s3.amazonaws.com"
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
end
initializers/asset_sync.rb
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = ENV['FOG_PROVIDER']
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = ENV['FOG_REGION']
# Don't delete files from the store
config.existing_remote_files = "delete"
# Automatically replace files with their equivalent gzip compressed version
config.gzip_compression = true
# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
config.manifest = true
config.custom_headers = { '.*' => { cache_control: 'max-age=31536000', expires: 1.year.from_now.httpdate } }
end
end
Heroku Vars
AWS_ACCESS_KEY_ID: *****************
AWS_SECRET_ACCESS_KEY: *****************************
FOG_DIRECTORY: localize
FOG_PROVIDER: AWS
FOG_REGION: us-west-2
gemfile
gem 'rails', '4.1.1'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails'
gem 'sdoc', '~> 0.4.0', group: :doc
#aws
gem "fog", "~>1.20"
gem 'asset_sync'
group :development do
gem 'thin'
end
group :production do
gem 'newrelic_rpm'
gem 'rails_12factor'
gem 'pg'
end
I also ran:
heroku config:add FOG_PROVIDER=AWS AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=yyy
Along with
heroku config:add FOG_DIRECTORY=localize
And then when I run
bundle exec rake assets:precompile
Or
RAILS_ENV=production bundle exec rake assets:precompile
I get this output;
rake aborted!
AssetSync::Config::Invalid: Fog directory can't be blank, Aws access key can't be blank, Aws secret access key can't be blank
Anyone who has experience with rails, heroku, and S3 who could guide me in the right direction would be much appreciated. Thanks in advance.
Ok looking at your settings there seems to be a few things wrong, Ill add what i normally use and hopefully it will help you
Production.rb
ExampleApp::Application.configure do
config.action_controller.asset_host = "http://exampleapp.s3.amazonaws.com"
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
end
asset_sync.rb
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = ENV['FOG_PROVIDER']
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = ENV['FOG_REGION']
# Don't delete files from the store
config.existing_remote_files = "delete"
# Automatically replace files with their equivalent gzip compressed version
config.gzip_compression = true
# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
config.manifest = true
config.custom_headers = { '.*' => { cache_control: 'max-age=31536000', expires: 1.year.from_now.httpdate } }
end
end

(Rails) : NoMethodError: undefined method cost' for BCrypt::Engine:Class

When I learn "Ruby on Rails Tutorial", and I want to create a User on console:
irb(main):001:0> User.create(name:"gsky",email:"k#q.com",
irb(main):002:1* password:"aaaaaa",password_confirmation:"aaaaaa")
then, I getting the following error message:
NoMethodError: undefined method cost' for BCrypt::Engine:Class
from D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activemodel-4.
0.2/lib/active_model/secure_password.rb:104:inpassword='
from D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.2/lib/active_record/attribute_assignment.rb:42:in public_send'
This is user model:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end
Add bcrypt-ruby to your Gemfile as specified below:
gem 'bcrypt-ruby', '3.1.2'
then run bundle update from your project root directory and bundle install
When i saw "Ruby On Rails Tutorial" I have met the same problem,
I solved it by set Gemfile from:
gem 'bcrypt-ruby', '3.0.1'
to:
gem 'bcrypt-ruby', '3.1.2'
then run:
bundle install
Also going for the tutorial/book and having the same problems, I used gem 'bcrypt-ruby', '~> 3.0.0' because of problems with bundle install. After going through secure_password.rb, the problem was in BCrypt::Engine.cost, this method actually doesn't exist.
I changed my gem to gem 'bcrypt-ruby', '~> 3.1.0' which installed bcrypt 3.1.7. Saw a warning message about the gem being renamed and changed it to gem 'bcrypt', '~> 3.1.0' (this part shouldn't matter). After doing bundle install, I was able to see the implementation of Bcrypt::Engine.cost through my IDE and I was able to make my user in rails c.
I want to add that adding ActiveModel::SecurePassword.min_cost = true in test.rb was able to let me make new users if I ran rails c in test environment, but when I added the same line in development.rb, it didn't work.
I think you are learning rails from the tutorial. If you just want to continue and not spent much time on doing the right fix, you can just use the cost as say 10, instead of calling the BCrypt::Engine.cost method.
So replace
cost = BCrypt::Engine.cost
with
cost = 10
This value when used will take less than 200ms to compute and that should be okay.
Tested on mac:
Include this in your gemfile:
gem 'bcrypt', '3.1.11'
run:
xcode-select --install
then run: bundle install
That's it.
Best of lucks