Bamboo + Cucumber + Webkit driver - ruby-on-rails

I'm having a lot of trouble running my cuke test cases that use capybara-webkit driver. Because this is a headless server, I'm trying to run xvfb-run to run the tests, but keep running into roadblocks.
If I try to run
xvfb-run rake cucumber:all
then I'm getting errors from my server that rake doesn't exist. (Even though it does)
If I try to run it via bundle exec
xvfb-run bundle exec rake cucumber:all
then I get an error telling me bundle doesn't exist!
Both commands DO work from the ssh into the server, but its only when coming from Bamboo that they don't. I've tried both the Rake task, and just doing up a script to run the bundle exec, but nada.
I'm wondering if anyone else has this set up, and if they could walk me through how they have it setup within bamboo... This is a pretty typical rails project, and this is my LAST stage before I can get my CI up and running.
Thanks!

I think I have figured it out based on this page I found here, which was in regard to using Jenkins. http://sermoa.wordpress.com/2011/07/02/cucumber-running-headless-selenium-with-jenkins-the-easy-way/
The basic idea is to use the "headless" gem and then put this section in your env.rb for cucumber:
if ENV['HEADLESS'] == 'true'
require 'headless'
headless = Headless.new
headless.start
at_exit do
headless.destroy
end
end
Then run your normal rake task in bamboo with the environment variable "HEADLESS=true".
You have to have xvfb installed on the server too. (sudo apt-get install xvfb)

Here is a working updated version that will setup capybara-webkit and headless for cucumber using before/after hooks. Just include this in your support/env.rb or another support file (I used support/javascript.rb because there are a few related things I do):
Capybara.javascript_driver = :webkit
Before do
# run capybara-webkit headless if not on mac and this test is selenium based.
if Capybara.current_driver == :selenium
require 'headless'
#headless = Headless.new
#headless.start
end
end
After do
#headless.destroy
end
EDIT: Here's a gist with some taggable goodness where you can use :chrome in local dev mode if you want while making sure the CI environment remains headless:
https://gist.github.com/rosskevin/5937888

Related

Run rake assets:precompile before tests that include :js tag and skip otherwise

tl;dr --
Is it possible to run a single command (e.g. rake assets:precompile ) only when there is a :js test included in the body of tests being run?
--
I'm developing a rails 5, ruby 2.3.1 app that has a large rspec test suite.
We recently installed the webpacker gem in the application which has caused us to have to run:
$ bundle exec rake assets:precompile
before running the tests. If the above command is not run the tests will be run against the most recently precompiled assets.
This has caused some headaches as developers have forgotten this step and then banged their heads against a wall until someone remembers to run that before running the test suite.
Ideally I'd like to simply add to the spec/spec_helper.rb:
config.before(:suite) do
system('bundle exec rake assets:precompile')
end
However, this will run the precompile before every single run of the test suite or any subset therein. This will drastically slow down development time for a backend dev that is simply trying to run a single request spec that normally takes 0.15 seconds.
Additionally, we normally skip running the :js tests when developing with guard as they take too long. We just run the :js specs as a sanity check before deploying or pushing to a remote branch.
Unfortunately, adding the {:js => true} option to:
config.before(:suite, js:true)
doesn't work either, as options are ignored for the before(:suite).
Filter using :js tag and run once for all specs.
ENV[ 'ASSET_PRECOMPILE_DONE' ] = nil
config.before(:each, :js) do
if ! ENV[ 'ASSET_PRECOMPILE_DONE' ]
system 'bundle exec rake assets:precompile'
ENV[ 'ASSET_PRECOMPILE_DONE' ] = 'true'
end
end

Change Rails/Rake Test Default Behavior

According to the Rails guide for testing:
"By default, running bin/rails test won't run your system tests. Make sure to run bin/rails test:system to actually run them."
Does anyone know how to change the default behavior so it will run all of your tests including system tests?
I'm trying to do the same.
I think that for having rails test to also run system tests you need to redefine the rake task. For what I could investigate the task is defined at testing.rake file inside the railties gem /lib directory:
desc "Runs all tests in test folder except system ones"
task :test do
$: << "test"
if ENV.key?("TEST")
Minitest.rake_run([ENV["TEST"]])
else
Minitest.rake_run(["test"], ["test/system/**/*"])
end
end
The second argument in the function inside else is a pattern to exclude when running the tests.
Here Overriding rails' default rake tasks
it's explained how to override them, however I couldn't put it to work this way.
If it helps you, I'm using bundle exec rake test:system test instead and it runs both the system and all the other tests together

Rails - Run Rake tasks with Capistrano

Im scratching my head here wondering if I'm barking up the wrong tree. I have a server which I've deployed a Rails app onto using Capistrano. Recently I added a new data type to one of the models, and now I need to run a Rake task to update the existing records.
After a lot of Googling I'm starting to wonder if people use Rake tasks with Capistrano. Some forum posts from 2013 or so mention that Capistrano supports the .rake extension. Whereas other posts I've found indicate that Capistrano has its own task automation system, and is incompatible with rake.
I found Cape, but I'm unsure if this is what I'm looking for as it seems to convert Rake tasks into cap recipes. Its possible I'm mistaken on this point, I really don't have any experience working with Capistrano or even working in the full stack spectrum.
What I'm wondering is this: How do I run a simple Rake task on my remote server?
Some quick points for clarity, I've installed the app on the latest Ubuntu LTS, 14.10 if memory serves. I followed the tutorial found here. I have full sudo access and I can ssh into the server.
thanks in advance for helping a noob
If you need to update models, you can of course write a Rails migration - this will ensure that it's run if it hasn't been run yet.
The easiest way to execute a rake task on the server would be just via ssh if it's a one-time task. See the last paragraph in the tutorial you mentioned:
cd /opt/www/testapp/current ; bin/rake RAILS_ENV=production db:seed
To answer your original question about rake: you can execute rake tasks via capistrano similar to how you would execute it locally, only within the capistrano script. Here's an example:
deploy.rb:
namespace :rake do
desc "My task"
task :my_task do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, "my_task"
# !!!see NOTE at end of answer!!!
end
end
end
end
end
You can view all your cap tasks via cap -T locally. The capistrano task I wrote above should show up as cap tasks:my_rake_task.
If you want to be ably to run any available rake task without configuring, do the following:
namespace :rake do
desc "Invoke rake task"
task :invoke do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, ENV['task']
# !!!see NOTE at end of answer!!!
end
end
end
end
end
Then you can write:
cap production deploy:invoke task=my:rake:task
NOTE: you might want to replace the execution line with
run "bin/rake RAILS_ENV=#{rails_env} #{ENV['task']}"
to use the same syntax as the tutorial (without the binstubs you might need to configure capistrano/bundler and capistrano/rbenv first ...)
Check out capistrano-rake
Once installed, run any rake task on your production/staging servers without messy capistrano recipes by simply doing this:
$ cap production invoke:rake TASK=your:rake:task
Full Disclosure: I wrote it

Running rake task from a daemon

I have a daemon that i made using the daemons gem
I run it and it just closes without error, but the script doesn't seem to be actually running.
I'm running a rake task from it, will daemons run the commands inside from the directory it was called from?
This is the script
require 'rubygems'
require 'daemons'
Daemons.run('rake mytask')
the rake task has a loop which should stop it from exiting.
I'm not sure the way you're doing this will work with daemons. Can you move the rake task into an .rb file? Daemons is going to pass what ever you have in the run command to ruby, so in essence it is trying to run "ruby rake mytask"

running tests via the terminal and rake, load test_helper

I am having a problem running my tests from the terminal and from rake, e.g. rake test:integration
At the moment, I have the requires for test_helper.rb specified like this:
require File.dirname(__FILE__) + '/../test_helper'
This works fine when running them from the terminal but obviously when it is ran from rake, the directory is different and the process cannot find the test_helper file.
I think I want to add to this to my $load_path but I am not sure how to add it when running only in the test environment.
Can anyone help me out?
You can revert to just require 'test_helper' (the default for integration tests, at least with Rails 2.3.x). This will allow tests to run from a rake task, and as long as you cd to the test directory within your rails app, you can run tests via the terminal with ruby integration/your_test.rb.

Resources