External content

Single step and slow motion for cucumber scenarios using @javascript selenium

Single step and slow motion for Cucuber scenarios can come in handy, especially in @javascript scenarios.

# features/support/examiners.rb
AfterStep('@slow_motion') do
  sleep 2

AfterStep('@single_step') do
  print "Single Stepping. Hit enter to continue"

If you’re using spreewald, these tags are available as @slow-motion and @single-step (with dashes instead of underscores).

Note: You can also [prevent the selenium webbrowser windo…

How to use CSS3 gradients in Opera

Since version 11.10 Opera provides support for linear gradients using -o-linear-gradient.

The syntax is pretty similar to Mozilla’s -moz-linear-gradient. This will create a vertical gradient from yellow to red:

background-image: -o-linear-gradient(top, #ff0, #f00);

The first parameter defines where the gradient starts and which direction it will go. \
You can use top/left/bottom/right (and combinations of those) but also set any angle you like (0° being the left side, going counter-clock-wise):

background-image: -o-l...
External content

Making the rails 3.1. asset pipeline and asset precompiling work in production mode

Recently, we had an interesting lunch-break with the rails 3.1. asset-pipeline in production mode. Daniel Zahn made a blogpost about our journey, precompiling assets, fingerprinting, Haml, Sass & Compass and what he calls “the dark heinous hutch”.

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


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))

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. ————————————————————————————————————-…

Remove duplicate lines from a text file

You can use this shell command:

uniq -u input.txt output.txt

Open a MySQL shell using credentials from database.yml

In order to open a MySQL shell without the need to enter user and password, you can say the following in any Rails 2 project:

script/dbconsole -p

In Rails 3 you can say:

rails dbconsole -p

If you’d like to enter a database for an environment other than development you can say:

script/dbconsole -p staging

Use SSL for Amazon RDS / MySQL (and your Rails app)

In case you have sensitive data within your RDS instance, you want to use encrypted connections between your application and RDS instances. If you’re using MySQL on RDS, here’s what to do:

  1. Download the AWS CA file and copy it to the machine you want to connect from:
    As far as I could find out, you (currently) cannot access further details of the SSL configuration (such as public key).
  2. Try to connect using MySQL client

    `% mysql -uyour_username -p -h rds_hostname_from_…

Removing ANSI color codes from Rails logs

The colors in Rails log files are helpful when watching them but, since they are ANSI color codes like ^[[4;36;1m, can be annoying when you are reading the logs with a tool that does just prints those control characters (like less or vim).

Remove them with sed:

cat staging.log | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g"

This will print the log without colors to your terminal. You can pipe the result into less for example.

To have a file you can vim around with, just write that output into a new file:

External content

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:


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

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 `…

External content

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.

Install the Oniguruma gem

Oniguruma is an advanced regular expression engine for Ruby.

Install Oniguruma with binary dependencies like this:

sudo apt-get install libonig2 libonig-dev
sudo gem install oniguruma

On the Mac do:

brew install oniguruma
sudo gem install oniguruma

Send and receive encrypted file attachments with Thunderbird / Enigmail / OpenPGP

Sending and receiving file attachments using Thunderbird and the Enigmail addon is not as straightforward as it could be:

  • When sending file attachments, Enigmail will ask you for the encryption method. Choose Encrypt/sign each attachment separately.
  • When receiving an encrypted mail with file attachments, decrypting the message will not decrypt the attachment. Instead you need to right-click on the attachment and choose Decrypt and save as….

Usage of RSpec's raise_error

Never use raise_error without specifying the Error you expect.

expect { do_a_lot_of_complicated_stuff }.to raise_error

will be green if you make any error in programming. E.g. a simple typo would make the test above green. The block will catch the Spec:: exception and the test will be happy.

Be sure to always have custom errors in your models and raise them in a manner that lets you know what went wrong.

expect { execute_payment! }.to raise_error(PayPal::Payment...

Using heredoc for prettier Ruby code

You can use heredoc to avoid endlessly long lines of code that nobody can read. It looks like this:

def long_message
    Here goes a very long message...
    Dr. Foobear

<<-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. T0 be more verbose you can use something else – your IDE may e…

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(:+)"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
External content

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
2747 cards