Getting an error when running headless jasmine tests in rails - ruby-on-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.

Related

SimpleCov not processing statistics

As the simpleCov gem documentation indicates, the test_helper.rb file was amended in a Rails 7 app to include
require 'simplecov'
SimpleCov.start 'rails'
puts "SimpleCov started successfully!"
# Previous content of test helper now starts here
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
require 'webmock/minitest'
and a model test file
class CategoryTest < ActiveSupport::TestCase
puts "MyCode is being loaded!"
runs as expected
> rails test test/models
SimpleCov started successfully!
MyCode is being loaded!
However the net coverage is blatantly wrong:
93 runs, 192 assertions, 0 failures, 0 errors, 0 skips
51 / 3095 LOC (1.65%) covered.
as this was a copy of another application that had full coverage and was renamed.
I noticed at some point that the coverage percentages were dropping, but that was while running individual tests (and thus ignorable given SimpleCov re-writes all); now the result remains invariably at that figure.
The only thing I can think of is that some lines are covered and thus something might be interrupting SimpleCov's analysis somehwere down the line.
Any thoughts on how to debug this? [now moot question]
update
I had the itch about this some time ago and came across a solution.
Commenting out parallelize(workers: :number_of_processors) allows coverage to be measured.
So why is this default setting clashing with SimpleCov ?

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

Heroku rake task with poltergeist

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

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.

Cucumber + Webrat + Selenium guide

I have been using Cucumber and Webrat for a while. I now need to start writing behaviour that involve AJAX interactions so I was thinking to use the Selenium adapter for Webrat.
Can anyone point out a easy and updated step-by-step guide for installing and configuring selenium+webrat+cucumber?
I would like to be able to mix javascript scenario with non-javascript scenarios.
I'm using Selenium with rspec on my project and generate code from a custom formatter for Selenium IDE.
There is many selenium for rails but i success using Selenium-RC http://seleniumhq.org/download/ , so download to your pc.
Here are my steps:
Unzip and run> java -jar selenium-server.jar
Open selenium-client-ruby, read the doc, follow it you will get success!
gem install rspec, rspec-rails version 1.2.6 (it not, you need to comment version restrict of selenium-client source code)
gem install selenium-client
Open Selenium-IDE (Firefox of course), Open Options -> Options -> Formats
Click Add, and paste this code in http://www.techdarkside.com/rspec_export.txt
Now, You just export spec to your spec folder for me, I use spec/features/xxxx_spec.rb see code below.
Very similar approach can find at here
For webrat+cucumber, the latest Rspec book will give all you need. (They don't have selenium + cucumber chapter finish yet)
example
require 'rubygems'
gem "rspec", "=1.2.6"
gem "selenium-client", ">=1.2.15"
require "selenium/client"
require "selenium/rspec/spec_helper"
describe "Google Search" do
attr_reader :selenium_driver
alias :page :selenium_driver
before(:all) do
#selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
end
before(:each) do
selenium_driver.start_new_browser_session
end
# The system capture need to happen BEFORE closing the Selenium session
append_after(:each) do
#selenium_driver.close_current_browser_session
end
it "can find Selenium" do
page.open "/"
page.title.should eql("Google")
page.type "q", "Selenium seleniumhq"
page.click "btnG", :wait_for => :page
page.value("q").should eql("Selenium seleniumhq")
page.text?("seleniumhq.org").should be_true
page.title.should eql("Selenium seleniumhq - Google Search")
page.text?("seleniumhq.org").should be_true
page.element?("link=Cached").should be_true
end
end

Resources