Heroku rake task with poltergeist - ruby-on-rails

I'm writing a rake task which tests an application. To test it I need Capybara and Poltergeist.
Rake task looks like this:
require "capybara"
require "capybara/poltergeist"
require "capybara/dsl"
namespace :almet do
desc "Checking if something changed on tatar.ru"
task check_news: :environment do
Capybara.current_driver = :poltergeist
Browser = Class.new { include Capybara::DSL }
page = Browser.new.page
page.visit("http://almetyevsk.tatar.ru")
unless page.has_selector?(".home-events ul li") &
page.has_selector?(".home-events__item-lead") &
page.has_selector?(".home-events__item-date") &
page.has_selector?("h2 a")
then
alert
end
news_link = page.find(:css, "h2 a", match: :first)[:href]
page.visit(news_link)
unless page.has_selector?(".page__content-i h2") &
page.has_selector?(".page__content-i .news") &
page.has_selector?("i")
then
alert
end
end
def alert
AlertMailer.alert.deliver_now
end
end
This code checks if the main site from parsing data is still the same (no changes in css selectors). This rake task works good locally, but when I pushed it to heroku I've got an error:
rake aborted!
Cliver::Dependency::NotFound: Could not find an executable ["phantomjs"] on your path.
I googled this, installed phantomJS but still got the same error.(Poltergeist using phantomJS). Is it possible to solve my problem?

You need to install PhantomJS onto your Heroku instance. Usually that involves adding a build pack which will install it for you, like https://github.com/stomita/heroku-buildpack-phantomjs

Related

Disable rest-client output on minitest

I've searched for this, but without success.
I have some tests (minitest) that use RestClient and Webmock. When passing for those tests I always have the request logged, polluting the test ouput:
[$] rake
Run options: --seed 60435
Running:
.........................................................RestClient.get "http://example.com/some_controller/some_action?userLocale=fr_FR", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client/2.0.0 (darwin14.1.0 x86_64) ruby/2.2.1p85" # => 200 OK | 4 bytes
Is there a way to disable this ?
[EDIT]
Just to add, if I call the same address using ruby URL I have nothing logged (even using webmock) so it really is something related with Rest-client.
I already tried to set the ENV['RESTCLIENT_LOG'] variable, but without success.
What about:
RestClient.stub :log, '' do
# Your test code here
end
http://www.rubydoc.info/gems/minitest/4.2.0/Object:stub
You have many other options to redirect the log output:
In your test_helper.rb:
RestClient.log = 'tmp/test.log'
http://www.rubydoc.info/gems/rest-client/1.8.0/RestClient.log=
From the command line:
RESTCLIENT_LOG=tmp/restclient.log bundle exec rails test
In last resort you could monkey patch:
# test_helper.rb
RestClient.class_eval do
def self.log
''
end
end

rake aborted! Don't know how to build task 'sandbox'

I'm trying to add the sandbox to my rails spree application and have run into this error
(using windows 8/powershell with Rails 4.1.6). I'm going by this manual: https://github.com/spree/spree/issues/411
This link Use older version of Rake
seems to have a similar issue but I am not sure how to take the necessary steps to achieve it.
When I try:
C:\Ruby193\openUpShop> bundle exec rake sandbox
I get:
rake aborted!
Don't know how to build task 'sandbox'
I'm am new to rails and am still not sure how everything works so a throughout explanation
with step by step instructions would be greatly appreciated! Thanks.
you can use a file sandbox.rb
# use example: rake task:sub_task -- --sandbox
if ARGV.any? {|arg| arg == '--sandbox' }
puts "** << USING SANDBOX!! >> **"
# beginning
$sandbox = -> do
ActiveRecord::Base.connection.begin_transaction
end
# end
at_exit do
ActiveRecord::Base.connection.rollback_transaction
end
end
then only you need add at the beginning of your task.rake file
require_relative 'path_to_your/sandbox.rb'
..and add at the beggining of your task code
desc "description task"
task example_task: :environment do
$sandbox.call if $sandbox.present?
...

RSpec is Freezing

I have rspec configured installed in my rails app. It was working fine (We are just experimenting with rspec so there's only 2 tests).
They were working fine. Now rspec is freezing when it's going to perform a test using database.
I just freezes. I don't even know were to start looking because there's no error in the output.
Is there a verbose or debugging mode for rspec or someone ever faced this?
I've tried -b but it freezes before can give an error.
Output: (Rspec is configured with --format documentation)
[leandro#machine:~] rspec
User
Than, that's it. It hangs. I has to reset manually the computer twice.
This is the user_spec.rb
require 'spec_helper'
describe User do
let(:user) { User.create({
username: "teste",
email: "teste#teste.com",
name: "Teste",
phone: "123456789",
password: "123456",
notes: "Teste"
}) }
subject { user }
it "is invalid without a username" do
user.username = nil
is_expected.not_to be_valid
end
end
And my spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.color = true
config.tty = true
config.formatter = :documentation #:progress, :html, :textmate
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
SOLUTION
It turns out that delayed_job_active_record gem was causing the hanging.
I don't know why, but as #Greg Malcolm I looked into the log/teste.log and rspec was feeezing right after createing the delayed jobs database and setting up a new user.
I've restricted the gem just for development and production enviroment, and it worked!
I haven't heard of a rake like verbose feature for rspec. But it's probably more useful to look through log/test.log and see what shows up there. It shows database activity which is part of a freeze effect.
You might also want to rails console into test and make sure your database connection is working right.
You have to check the ./log/test.log file. As this file tends to grow big, use the tail command to read the last n lines.
For example, to read the last 5 lines of the log file, execute the following command:
tail -5 ./log/test.log
In my case, my Google Pub/Sub emulator was hanging.
[ActiveJob] Failing with status #<struct Struct::Status code=4, details="Deadline Exceeded", metadata={}>
[ActiveJob] calling pubsub.googleapis.com:443:/google.pubsub.v1.Publisher/GetTopic
It might also be helpful to use tail -f ./log/test.log, to keep track of the logs while RSpec is running. You can read more about it on "Tail Your Test Logs", by thoughtbot.
It's pretty old question but in case anybody else will get this. I've got the same strange Rspec behaviour today because of left --drb option in project .rspec file after removing DRb server (spork or zeus). Just make sure Rspec have DRb disabled if you don't have spork or zeus installed.

run rake task inside rails application

I want to run asset precompile task inside the rails application,As I had many dependencies who will change the code,in that case all the time whenever they change i need to run the script as I cant give server access to them so I am providing the GUI for them from that they alone can run the script,so,I have built UI to run the task with some parameter like
system("Template='#{params[:template]}' Theme='#{params[:theme]}' rake assets:precompile)
I am getting two values from UI(params[:template],params[:theme]).Another thing i want to run this task in another path(site path) means Admin side UI is there that task should execute in Site directory,
if(params[:theme_script] == "true")
template=Template.where(:name => params[:template]).first
if template
theme = template.themes.where(:name => params[:theme]).first
if theme
# Dir.chdir "#{THEMEPATH}"do
# `Template="#{template.name}" Theme="#{theme.name}" rake assets:precompile`
# end
# sleep 10
# system("#{Rails.root.to_s}/lib/shell_script.sh")
# RunRake.run_rake(template.name,theme.name)
# Dir.chdir "#{THEMEPATH}"do
# Rake::Task['assets:precompile'].invoke
# end
ENV["Template"] = template.name
ENV["Theme"] = theme.name
precompile_task = "bundle exec rake assets:precompile --trace 2>&1"
output = Dir.chdir(THEMEPATH) { %x[ #{precompile_task} ] }
flash[:notice] = "Asset created successfully"
else
flash[:notice] = "U have enter invalid data"
end
else
flash[:notice] = "U have enter invalid data"
end
end
This is my code am checking multiple condition and am allowing to execute the task.
I have tried this code by putting in controller and lib, but this is not working.
I have tried with shell script also.
Could please anyone can help me.
You can just setup an environment variable for rails, and then issue #invoke method from a controller. So, prepare the files:
gemfile
gem 'rake'
config/initializers/rake.rb:
Rake.load_rakefile Rails.root.join( 'Rakefile' )
app/controllers/your_controller:
ENV["Template"] = template.name
ENV["Theme"] = theme.name
Rake::Task[ 'assets:precompile' ].invoke
Issue bundle install, then run console rails c, and type:
Rake::Task.tasks.map(&:name).grep 'assets:precompile'
# => ["assets:precompile"]
As you can see, the task assets:precompile is loaded successfully. Then just issue the action for the controller.
To run the task for an other rails app you shell run also the other ruby instance, similar to as you had done:
system( "other_app_run.sh '#{template.name}' '${theme.name}'" )
other_app_run.sh:
#!/bin/bash
source "$HOME/.rvm/scripts/rvm"
cd /other/app/path
export Template="$1"
export Theme="$2"
rake assets:precompile

Getting an error when running headless jasmine tests in rails

I've got a rake task set up to run headless jasmine tests on a build server and output the results in junit format. Here's the task:
namespace :jasmine do
desc "Runs Jasmine tests headlessly and writes out junit xml."
task :headless_junit do |t, args|
run_jasmine_tests(Dir.pwd)
end
end
def run_jasmine_tests(output_dir)
require 'headless'
require 'jasmine'
require 'rspec'
require 'rspec/core/rake_task'
output_file = "#{output_dir}/jasmine_results.xml"
Headless.ly do
RSpec::Core::RakeTask.new(:jasmine_continuous_integration_runner) do |t|
t.rspec_opts = ['--format', 'RspecJunitFormatter', '--out', output_file ]
t.verbose = true
t.rspec_opts += ["-r #{File.expand_path(File.join(::Rails.root, 'config', 'environment'))}"]
t.pattern = [Jasmine.runner_filepath]
end
Rake::Task['jasmine_continuous_integration_runner'].invoke
end
end
When I run the this I get this error:
TypeError: jasmine.getEnv(...).currentSpec is null in http://localhost:34002/assets/jquery.js?body=true (line 1129)
expect#http://localhost:34002/assets/jquery.js?body=true:1129
#http://localhost:34002/__spec__/activity_catalog_search_filters_spec.js:15
jasmine.Block.prototype.execute#http://localhost:34002/__jasmine__/jasmine.js:1064
jasmine.Queue.prototype.next_#http://localhost:34002/__jasmine__/jasmine.js:2096
jasmine.Queue.prototype.next_/onComplete/<#http://localhost:34002/__jasmine__/jasmine.js:2086
... LOTS MORE ...
I'm using rails 3.2.13, jasmine 1.3.2, headless 1.0.1, rspec 2.14.1 and Jasmine-jQuery 1.5.8
I think it could be similar to the problem this guy is having:
TypeError: jasmine.getEnv().currentSpec is null
Turns out the issue was with a test that was using jQuery.get to load a url into the dom. An empty string was being passed down as the url (since the test writer didn't really care what was loaded I guess) but that caused jQuery to fetch the current page (the jasmine tests themselves) and load that into the dom. Massive chaos ensued.
The more interesting thing (and perhaps more helpful) was how we figured that out. Turns out the fancy rake task was not the issue. It's just that the headless tests use Firefox and I usually load them manually in chrome, where this error didn't seem to happen. One I had the error reproduced in Firefox it was easy enough to track down the cause with the debugger.
So the bottom line is, if you're ci tests are failing and you can't reproduce it, try loading them manually in Firefox.

Resources