Running Capybara tests at a different url when on Travis CI - ruby-on-rails-3

I have some integration tests written in Capybara which I'm running on Travis. In the tests I hit a hardcoded url (given by Pow and symlinks) with the visit method. This of course does not work well on Travis. What I need to do is to somehow distinguish environments. So when the tests run on Travis they are hitting a different url like localhost:5000 for example. I put that in my .Travis.yml file that it will start a rails server in the background which works fine. The question is how do I make the tests use that url instead?
My config looks something like this:
language: ruby
rvm:
- 1.9.3
before_script:
- RAILS_ENV=test bundle exec rake db:create
- "bundle exec rails server -p 5000 &"
- "sleep 5" # Wait for the server to start up"
script: bundle exec rspec
I'm using PhantomJS through poltegeist gem. I'm thinking if I could somehow use the Travis env var. Anyone got any suggestions on this?
Thanks!

I'm not very familiar with Travis CI but I don't see any reason you need to hardcode the server name into url.
Instead of
visit 'http://localhost:5000/about'
You can use
visit '/about'
Or better
visit about_path
Less dependency is always better. I suggest your tweaking the tests.

Travis CI sets several environment variables for you that you can use. I think TRAVIS=true may be of interest:
Capybara.app_host = if ENV['TRAVIS']
'localhost:5000'
else
'http://www.example.com'
end

Related

Can Travis-CI run Codeception tests?

I'm creating my tests (though I'm a beginner, learning) using Codeception. This includes acceptance and unit tests for now.
I want to add my repo to Travis CI so I can automate testing process after each commit and put build-status tag.
I would like to ask;
Can Travis-CI run codeception tests?
Can Travis-CI run codeception acceptance tests emulating browser?
If both answers are no, is there any other CI tool which can?
Thank you.
Yes, it is possible to run Codeception tests, including acceptance tests that run using WebDriver, on Travis CI.
It is possible to run your tests with a real browser on Travis, but it is easiest to use a headless browser, since Travis is running on a headless machine. PhantomJS is perfect for this, and it comes pre-installed with Travis CI's build bootstrap.
To run the tests with PhantomJS, you'll need to configure the WebDriver module like this in your .yml Codeception configuration file:
modules:
config:
WPWebDriver:
url: 'http://127.0.0.1:8888'
browser: phantomjs
The URL is important. I have found that attempting to use localhost instead of 127.0.0.1 will not work. Also, if you accidentally leave out the http://, that won't work either. You can use most any 8*** port, since most of them are open, but of course you'll need to have a web server running on that port to serve your static files or run your PHP application. The easiest way to do this, I find, is to use PHP's built-in webserver.
Your .travis.yml file might look something like this:
# Travis CI configuration file.
language: php
php:
- 5.6
- 7.0
before_script:
# Start up a web server.
- php -S 127.0.0.1:8888 -t /path/to/web/root >/dev/null 2>&1 &
# Start up the webdriver.
- phantomjs --webdriver=4444 >/dev/null 2>&1 &
# Install Codeception.
# Doing this last gives the webdriver and server time to start up.
- composer install --prefer-source
script:
- vendor/bin/codecept run
You will of course need to add Codeception to your project's composer.json file:
composer require --dev codeception/codeception
You'll also need to change path/to/web/root above to the path to the directory where you want the server's document root to be.
If you'd like to see a working demo running WebDriver tests against WordPress, you can check out this GitHub repo.
I'd think that it can be done, but gluing everything tohether is not going to be for the faint of heart. Reason why I think it can be done is that codeception, itself, is ci-ed on Travis. See https://travis-ci.org/Codeception/Codeception. I'd contact the people at codeception and ask for their thoughts.
Or you can take a peek at how they do it in the build logs, such as:
https://travis-ci.org/Codeception/Codeception/jobs/14432638
Looks like they're running headless with a downloaded standalone selenium server.
Travis-ci have some information on how to run GUI tests. In particular, they allow you to use a sauce labs account and run distributed selenium tests from there.
I ran into this problem today and I solved it by adding Codeception to my composer.json:
"require-dev": {
"codeception/codeception": "^2.1"
},
and referring to it on my .travis.yml:
install:
- composer self-update
- composer install
before_script:
- #Code that creates and seeds my database and so on
script: php vendor/codeception/codeception/codecept run

How to load environment variables in the Rails console?

I think this is a little, easy question!
I'm using .env file to keep all my environment variables, and i'm using foreman.
Unfortunately, these environment variables are not being loaded when running rails console rails c so, i'm now loading them manually after running the console, which is not the best way.
I'd like to know if there any better way for that.
About a year ago, the "run" command was added to foreman
ref: https://github.com/ddollar/foreman/pull/121
You can use it as follow:
foreman run rails console
or
foreman run rake db:migrate
rails does not know about the environmental variables specified in .env file as it is specific to foreman. You need to set the environment explicitly before invoking rails console. Have a look at this question.
I personnaly use dotenv in development and testing environements. With this approach, you don't have to prefix your commands, just call the initializer in your config/application.rb :
Bundler.require(*Rails.groups)
Dotenv::Railtie.load
HOSTNAME = ENV['HOSTNAME']

how can I troubleshoot "rake test" missing from my list of rake tasks?

I have two apps. One is a very simple app that I built with rails new..., added a unit test to and ran the unit test. The other is an existing app that is running fine but I'd like to add some tests to it. In AppA (the simple one) when I run rake -vT I see:
...
rake test # Runs test:units, test:functionals, test:integrati...
rake test:recent # Run tests for {:recent=>"test:prepare"} / Test re...
rake test:single # Run tests for {:single=>"test:prepare"}
rake test:uncommitted # Run tests for {:uncommitted=>"test:prepare"} / Te...
...
Which seems normal. But when I run that same command in AppB (the existing app) I don't see any of the commands related to rake test. My first thought was to just 'bring over' the tests from AppA to AppB to see if that would help. So I wiped all content from the test directory in AppB and copied over the test directory from AppA. Still no rake test in the list of apps. But I can run a unit test in AppB via ruby -Itest test/unit/first_test.rb (oddly I have to comment out fixtures :all to get it to work, maybe that's a clue).
Found the answer to this last night. Where a new app's application.rb has:
require 'rails/all'
I had:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
I did that because I was following a guide about MongoMapper I think. Go forward a few versions and the last line is commented out -- that's the real reason. I commented it out at the same time I switched my ODM over to Mongoid. I'm not sure why I commented it out, but that definitely did it.
Without knowing the contents of your Rakefile it's really hard to debug this, but you might be missing the .load_tasks call in the Rakefile.
If you're using Rails 3 you should have something like this:
MyApplication::Application.load_tasks
That line will take care of loading the default Rails tasks. You might be able to accomplish the same calling require "rails/tasks" in your Rakefile.

rails 3.1 assets pipeline + acceptance test

we have a rather extensive acceptance test suite, running on our CI server triggered by a Github push hook. i'd like to add a feature or spec to basically test wether rake assets:precompile runs smoothly. we run this task usually in a capistrano callback, but it has happened, that, for whatever reason, some assets couldn't/wouldn't be precompiled, and i missed the warning in that painfully cluttered capistrano output. in my opinion, it would make sense if this breaks a build in our CI.
now my question is, is anybody doing that? is there a 'standard' or easy solution? is there something more comprehensive than just invoking the rake task with should_not raise_error ...?

System rake test task doesn't run my tests

I'm sure I'm doing something naive or stupid, I'm just not sure what it is.
I'm writing a simple library for parsing data URIs. Being so simple, I figured I'd go ahead and just give ruby-1.9's minitest a whirl. The tests run great when I run them by hand, but when I try to run them with 'rake test', hoping to invoke the system rake test task, I get no joy. Specifically, with trace and verbose:
Donalds-Decisiv-MacBook-Pro:data_uri dball$ rake test -t -v
(in /Users/dball/src/data_uri)
** Invoke test (first_time, not_needed)
I've got tests in the test folder, they all start with test_ and end in .rb. Any ideas?
The repository of the project is http://github.com/dball/data_uri
Tests are not invoked because you didn't give rake any information about them.
Put this task in Rakefile:
require 'rake/testtask'
Rake::TestTask.new do |i|
i.test_files = FileList['test/test_*.rb']
i.verbose = true
end
Or grab a patch for your project.