Test that a form field has an error with Cucumber and Capybara

You can use the step definition below to say this:

Then the "Last name" field should have an error

Capybara

Then /^the "([^\"]*)" field should( not)? have an error$/ do |field, negate|
  expectation = negate ? :should_not : :should
  page.send(expectation, have_css('.field_with_errors', :text => field))
end

Prevent the selenium webbrowser window from closing after a failed @javascript step

When cucumber encounters a failing step in a @javascript feature, the selenium browser window instantly closes. Sometimes you do not want that, because you need to see what is going on. You can click through the data your feature created, when you add the following file to your features/support directory:

#features/support/examiners.rb

After('@leave_the_window_open') do |scenario|
  if scenario.respond_to?(:status) && scenario.status == :failed
    print "Step Failed. Press return to close browser"
    STDIN.getc
  ...

Using heredoc for prettier Ruby code

You can use heredoc to avoid endlessly long lines of code that nobody can read. Heredoc strings preserve linebreaks and can be used like this:

def long_message
  puts(<<-EOT)
    Here goes a very long message...
    Sincerely,
    foobear
  EOT
end

<<-EOT will be somewhat of a placeholder: anything you write in the line after you used it will be its value until you write EOT in a single line.

You can use any string to flag your heredocs. To be more verbose you...

Change / Update SSL certificate for Amazon Elastic Load Balancer

There is a new card about how to do this with the new AWS Command Line Interface


At first you need the IAM Cli Tools.
-------------------------------------------------------------------------------------------------------------...

How to fix: Gems are unavailable although they are installed

  • If Rails or Rake are complaining about a missing gem that is listed in your Gemfile.lock and the listed version is properly installed, something is seriously wrong and needs to be fixed.
  • If you accidently executed bundle install some_gem although you wanted bundle update some_gem

What is wrong

Let's say your Gemfile asks for some-gem which you can see when running gem list but bundle show some-gem just gives you an error:

Could not find gem 'some-gem', in any of the sources

Another indicator: Doing a `...

Use a Ruby method like a block or lambda

Sometimes you want to use a vanilla Ruby method like a block. You can use Object#method to obtain a method reference that responds to #call:

foo_plus = "foo".method(:+)
foo_plus.call("bar") # => "foobar"

The method reference also understands #to_proc so you can feed it to block-taking methods by prefixing it with &:

printer = method(:puts)
[1, 2, 3].each(&printer) # will print one line per number

Remove duplicate lines from a text file

You can use this shell command:

uniq -u input.txt output.txt

Runaway Regular Expressions: Catastrophic Backtracking

This article explains why some regular expressions take years to match against certain strings. A common culprit are nested repetition operators like in (x+x+)+y.

Git: List remote branches

Sometimes you may need to figure out what branches exist on a remote repository so you can pull them down and check them out, merge them into your local branches, etc. You can see the remote branches by saying

git branch -r

Or, if you want to see both local and remote branches, you can say

git branch -a

The Skinny on CSS Attribute Selectors

Good guide to different ways you can write CSS selectors that select elements by their attribute values.

How Non-negotiable Features Kill Software Products

features are pre-sold without any option to negotiate what’s important and what may be left out, you inevitably end up with too much complexity. Such pre-sold features not only tie your hands, but the client is also not able to change what he needs over time.

Truncate files to zero length

This will reduce the filesize of foo and bar to 0 bytes:

truncate -s0 foo bar

If the files do not exist they will be created.

You can use this to easily truncate your application's log files:

truncate -s0 log/*.log

Selenium WebDriver 2.5.0, 2.6.0 fails when selecting options from select boxes

We are consistently having trouble with selenium-webdriver > 2.5.0 where whenever we try to select an option from a <select> Capybara complains:

No such option 'Foo' in this select box. Available options: 'Foo', 'Bar', 'Baz' (Capybara::OptionNotFound)

This seems to happen with both old and new versions of Firefox. Our workaround so far is to freeze the gem at version 0.2.2.

Couldn't create database for ...

When you run rake db:create and get this error message

Couldn't create database for {"encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql", "database"=>"project_development", "password"=>"topsecret"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)

make sure the user you have specified (root/topsecret) in your database.yml has access to MySQL. You can check this by running mysql -uroot -p.

Solving "cannot remove Object::ClassMethods"

Most likely you run rake and your code is causing an exception which is not the one shown in your terminal.

Rails tries to catch this exception and clean up constants but -- while it's still booting up -- fails on this which causes another exception:

rake aborted!
cannot remove Object::ClassMethods

Running rake with the --trace parameter will give you no love; the backtrace is useless in most cases.

Try these approaches:

First: Check if there is a helpful error message

  • Ha...

Fixing Homebrew "Permission denied" issues

When installing your first formula, Homebrew may complain about not being able to access certain directories. The easiest solution to this is:

chown <username> /usr/local/Cellar /usr/local/share # create these directories if they do not exist

Then sudo brew install abc and you're going.

Dealing with Solr's "404 Not Found" response

When your Solr seems to be started properly (a process is running with the correct data directory) but never responds properly and replies (via the API or web interface) with...
404 "Not Found"

... check if Solr's log directory is actually writable for the user running it.

When Rails does not recognize Rake tasks in lib/tasks

When you put a Rake task into lib/tasks, but running it fails with...

Don't know how to build task name:of:task

... does your Rake task's filename end in .rb? It needs to end in .rake instead.

What's My DNS? Global DNS Propagation Checker

whatsmydns.net is an online service that allows you to instantly perform a DNS lookup to check a hostnames current IP Address and other DNS information against a selection of random name servers around the world. This is especially useful to check the current state of DNS propagation after making changes to your domains zones.

Disable output when using cURL

cURL makes a web request and shows you the response body.

You can redirect the response body to /dev/null just like for many other programs. But if you do that, cURL will display some short information about the request you are making:

$ curl http://www.example.com/ > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 30865  100 30865    0     0   4793      0  0:00:06  0:00:06 --:--:-- 10199

If you wan...

Make an HTTP request to a machine but fake the hostname

Consider you have a website vhost listening to www.example.com, redirecting all incoming requests that do not talk about the configured hostname (this is often used to redirect users to http://www.example.com when entering only http://example.com/).

If you want to make a request to that site's web server without actually talking to www.example.com (e.g. because this is a load balancer's address but you want to access one specific machine), you cannot just request machine1.example.com or localhost as the above vhost will redirect...

sstephenson/execjs - GitHub

ExecJS lets you run JavaScript code from Ruby. It automatically picks the best runtime available to evaluate your JavaScript program, then returns the result to you as a Ruby object.

We forked craken

We forked craken today and fixed one of the issues we had with new (>0.8.7) rake versions.

The craken:install rake task raised a "can't convert Hash into String" error e.g. while deploying with Capistrano.

https://github.com/makandra/craken

gpg encrypted backup fails in crontab

If you using gpg for your encrypted backup and it fails after you put it in crontab make sure you have the "--no-tty" option enabled for gpg.

Otherwise you get this error: "cannot open /dev/tty: No such device or address" and the backup size is 0 byte.