Capistrano: Different usernames for each server

If you have different users for different servers, don't use set :user. Encode the username into the server definition instead:

server "username@servername.tld", :app, :web, :cron, :db, :primary => true

Turn off SSL for scenarios with Selenium

Selenium does not speak SSL because it uses WEBrick that doesn't. When you use Selenium for Cucumber scenarios that visit pages with SSL, they will fail.

To turn off SSL only for scenarios that are executed on WEBrick, put this method into your application controller.

def ensure_proper_protocol
  request.headers['SERVER_SOFTWARE'].andand.include?('WEBrick') || super
end

Select an unknown option with Capybara

When you don't know which options are available, but need to have an option selected, use this step.

When /^I select the second option from "([^"]*)"$/ do |id|
  second_option = find(:xpath, "//*[@id='#{id}']/option[2]").text
  select(second_option, :from => id)
end

How to accept or deny JavaScript confirmation dialogs in Capybara/Selenium

These methods are available to you:

page.driver.browser.switch_to.alert.accept
page.driver.browser.switch_to.alert.dismiss
page.driver.browser.switch_to.alert.text # the confirmation text

Spreewald gives you steps like these:

When I confirm the browser dialog
When I cancel the browser dialog

Note that recent versions of Selenium will automatically dismiss confirmation dialogs. [Here is how to fix that](https://makandracards.com/makandra/617366-configure-selenium-webdriv...

ActiveRecord 3+ auto-converts times to UTC by default. Hilarity ensues.

Remember how Rails 2 came with an awesome feature that broke all code using Time.now or Time.parse?

This behavior is now the default for Rails 3. Disable it by adding the following to your config/application.rb:

config.active_record.default_timezone = :local
config.active_record.time_zone_aware_attributes = false    

Let a Rails 3 application make a request to itself

Ever wondered how Rails talks to itself in a Cucumber feature? In Rails 3 you can do it like this:

def rack_env(path)
  { "rack.input" => {},
    "PATH_INFO"=>"#{path}",
    "REQUEST_METHOD"=>"GET" }
end

request = rack_env('/users/new')
response = Rails.application.call(request)
status, headers, body = response

puts status # e.g. 200
puts headers.inspect # hash of headers
puts body.body # html of response body

Instead of Rails.application you can also call any Rack application.

Windows 7: Open terminal from Explorer

Let's say you have an Explorer window showing a directory and want a cmd terminal to be opened there.\
While you needed a PowerToy on Windows XP, this is a build-in feature for Windows 7.

Simply press and hold the Shift key and right-click a folder icon or into empty space of an Explorer window.\
Then, choose "Open Command Window Here" from the context menu.

Let Webrat make a POST request

Just add the parameter :post to the visit method:

visit publish_entry_path, :post

Webrat doesn't follow redirect because it considers the url external

Rails doesn't know which host it is running on. For generating links, it strips the hostname off the request URL, which can lead to errors when you have absolute URLs in your Cucumber tests.

If you really need to use absolute URLs somewhere, say in an email you send, either throw away the host when parsing it (e.g. body.scan(/http:\/\/[^\/]+\/([^\s"<]+)/)) or tell Webrat you're back on your site.

Downgrade Firefox 6 to Firefox 5 on Ubuntu

Note that if you plan to downgrade Firefox because your Selenium tests broke after a Firefox upgrade, there is a better way that doesn't involve downgrading. Mozilla has stated that they will no longer provide security patches for any but the most recent versions of Firefox. So running an old Firefox should not be a long-term solution for anything.

If you still want to downgrade your Firefox for other reasons, here is how I downgra...

Prevent your Firefox from auto-updating

Note that if you plan to freeze your Firefox versions because your Selenium tests break whenever Firefox updates, there is a better way that lets you keep an up-to-date Firefox. Mozilla has stated that they will no longer provide security patches for any but the most recent versions of Firefox. So running an old Firefox should not be a long-term solution for anything.

If you still wish to disable the auto-update in Firefox, a poste...

Best GitHub feature

When browsing a repository, pressing "t" allows you to quickly search for file names. Very awesome!

Go here to try it out.

Detect the current Rails environment from JavaScript or CSS

Detecting if a Javascript is running under Selenium WebDriver is super-painful. It's much easier to detect the current Rails environment instead.

You might be better of checking against the name of the current Rails environment. To do this, store the environment name in a data-environment of your <html>. E.g., in your application layout:

<html data-environment=<%= Rails.env %>>

Now you can say in a pi...

How to stub class constants in RSpec

Hint: There's another card with this helper for Cucumber features.


Sometimes you feel like you need to stub some CONSTANT you have defined in an other class. Since actually constants are called constants because they're constant, there's no way to easily stub a constant.

Here are three solutions for you.

Easiest solution

Rethink! Do you really need CONSTANT = %w[foo bar] to be constant? In many cases, setting it as a...

Capybara - The missing API

The Capybara API is somewhat hard for parse for a list of methods you can call on a Capybara node. Below you can find such a list. It's all copied from the Capybara docs, so all credit goes to the Capybara committers.

When you talk to Capybara from a Cucumber step definition, you always have page as the document root node, or whatever you scoped to by saying within(selector) { ... }. You can select child notes by calling page.find(selector) or page.all(selector). You can call the same ...

How to grep through the DOM using the Capybara API

When your Cucumber feature needs to browse the page HTML, and you are not sure how to express your query as a clever CSS or XPath expression, there is another way: You can use all and find to grep through the DOM and then perform your search in plain Ruby.

Here is an example for this technique:

Then /^I should see an image with the file...

Rails I18n fallback locales

When you need to create a locale for a language variant (like Austrian for German), you probably don't want to duplicate your entire de.yml file only to change a few minor exceptions for our Austrian friends.

Luckily, the I18n gem used by Rails has a fallback feature where you can make one locale file fall back to another if no translation is available.

In the example above you would have a config/locales/de_DE.yml:

de_DE:
  # hundreds of translations here

... and another...

Use Capybara on any HTML fragment or page

I think this pattern is really useful not just for upgrading suites from Webrat, but really anywhere you have an HTML fragment or string that you’d like to use Capybara’s matchers on.

Hide your Selenium browser window with a VNC server

This is now part of geordi. Please don't follow the instructions below, if you use geordi.

Inspired by the recent headless Selenium note, I found yet another solution for the problem to hide your selenium tests away.

This has the advantages
^

  • not to require a gem (so you do not force this on others)
  • to allow you to take a look at the running webdriver if necessary

Simply make a script th...

How to look at hidden X screens

When you have a program running in a hidden X screen (like with Xvfb for Selenium tests) you may want to look at that hidden screen occasionally.

First, find out what X displays are currently active:

netstat -nlp | grep X11

This should give you some results like these:

unix  2      [ ACC ]     STREAM     LISTENING     8029600  4086/Xvfb           /tmp/.X11-unix/X99
unix  2      [ ACC ]     STREAM     LISTENING     8616     -     ...

How to hide your selenium browser window with "headless"

Note: While the solution in this card should still work, we prefer another solution now: Hide your Selenium browser window with a VNC server.


If you would like to hide the annoying selenium browser window that always gets the focus and prevents you from working, you can use the headless gem. This note provides some instructions how you can get it to work with your cucumber accepta...

Delete a Clearance session after some time of inactivity

This note describes how to kick a user out of a Rails application after she hasn't requested an action for a while. Note that this is different from deleting sessions some time after the last login, which is the default.

Also note that this is probably a bad idea. Most sites keep sessions alive forever because having to sign in again and again is quite inconvenient for users and makes your conversion rates go down the toilet. [The Clearance default is to keep sessions around for one year](https://makandracards.com/makandra/701-when-sessio...

Fix for: Your Firefox profile cannot be loaded. It may be missing or inaccessible.

If you use Selenium and Launchy to open web pages, you might run into an error saying "Your Firefox profile cannot be loaded. It may be missing or inaccessible.".

This happens because Launchy tries to use your Firefox running the web driver to open the page.

For Launchy < 0.4.x, as a workaround you can set an environment variable

export LAUNCHY_BROWSER=`which google-chrome`

For newer Launchys you also need to set:

export BROWSER=`which google-chrome`

If you want to do this in a ruby script, you can say

chrome_path ...

Fix "private method `select' called for Capybara::Node::Element

API breakage ahoy. You need to either upgrade your Capybara or downgrade your selenium-webdriver gem.

Alternatively, this could solve your trouble.