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
.
Auto-generate state_machine graphs as PNG images
The state_machine gem comes with a rake task that lets you generate PNG graphs from any model using state_machine
.
Install the required dependencies like this:
sudo apt-get install graphviz
sudo gem install ruby-graphviz
You can now generate a graph like this:
rake state_machine:draw CLASS=ModelUsingStateMachine
Replace ModelUsingStateMachine
with the name of your model class.
If it the raketask does not exist for you, add to Rakefile
(in your pr...
How to install a frozen version of Firefox for your Selenium tests
Whenever Firefox updates, all your Cucumber features that use Selenium break. This is annoying.
In order to remedy this, version 0.5.0 of our geordi gem comes with a script that helps you create an unchanging version of Firefox for your Selenium tests. In particular, this new copy of Firefox will have the following properties:
- It won't update itself with a newer version
- It can co-exist with your regular Firefox installation (which you can update at will)
- It will use a profile separate from the one...
Fix: "undefined method `bytesize' for #<Array>"
I believe that when WEBrick has trouble bringing up your Rails application, the WEBrick component that is supposed to print you a pretty error message has a bug and sometimes fails with this message:
"undefined method `bytesize' for #<Array>"
Starting the application in Passenger gave me a stacktrace in log/development.log
that pointed to the actual problem.
Possible causes discovered by looking at the logs
-----------------------------------------------------...
Fix Rubygems warning: Gem.source_index is deprecated, use Specification
After updating Rubygems you see a wall of deprecation warnings like this:
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from /usr/local/lib/site_ruby/1.8/rubygems/source_index.rb:197.
NOTE: Gem::SourceIndex#add_specs is deprecated with no replacement. It will be removed on or after 2011-11-01.
Gem::SourceIndex#spec_dirs= called from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.14/lib/bundler/rubygems_integration.rb:175
...
Ruby GetText will eval scripts containing ActiveRecord classes
When the Ruby parser module of Ruby-GetText comes across a file in one of its search directories (e.g. lib/scripts/
) and finds out that you are defining ActiveRecord classes inside it, it evaluates the whole file. Here is how to avoid that.
What's happening?
Let's say you have the following script which is only run once, manually, via script/runner
:
# lib/scripts/doomsday.rb
class User < ActiveRecord::Base; end
User.destroy_all
In that case we ...
Marry Date::Performance and Spreadsheet gems
Date::Performance is a gem that replaces various method in Ruby's Date
class with fast C implementations. Unfortunately it doesn't fully implement an internal method (Date.ajd_to_jd
) which makes your code blow up when you use it together with the Spreadsheet gem.
A solution is to restore the Ruby implementation of this particular method. To do this, copy the attached file to lib/fix_date_performance.rb
to config/initializers
.
Resque + God + Capistrano
Attached is a working config to deploy an application with Capistrano that needs to monitor Resque workers with God.
God will run as the deployment user, no need to register it as a system service.
Put this into your config/deploy.rb
:
namespace :god do
def god_is_running
!capture("#{god_command} status >/dev/null 2>/dev/null || echo 'not running'").start_with?('not running')
end
def god_command
...
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...
How to diff two strings in Ruby
When you need to use diff
in either some Ruby code or your Rails app, use the differ gem.
puts Differ.diff "foo", "boo"
# => {"boo" >> "foo"}
Usage
There are several variants available, all using the base method diff(to, from, separator = "\n")
.
You have diff_by_line
, diff_by_word
, diff_by_char
and may of course use your own separator:
puts Differ.diff 'Hauptsatz, und mein Nebensatz.', 'Hauptsatz, und dein Nebensatz.', ','
# => Hauptsatz,{" und dein Nebensatz." >> " un...
Zip files with Ruby
When you need to zip up files in Ruby, use zipruby
.
sudo gem install zipruby
You can add existing files, add files from strings and even add directories.
Example usage:
require 'zipruby'
cars = %w[audi bmw mercedes]
zipfile = Tempfile.new('my.zip', 'tmp')
Zip::Archive.open(zipfile.path, Zip::CREATE) do |zip|
zip.add_file '/tmp/me.txt'
zip.add_dir 'cars'
cars.each do |car|
zip.add_buffer "cars/#{car}.txt", "This #{car} is mine!"
end
end
Credits go to winebarrel for the Ruby bin...
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 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...
A few hints when upgrading to Ruby 1.9
Note: If you are currently working with Ruby 1.8.7 or 1.9.3, we recommend to upgrade to Ruby 2.1 first. From our experience this upgrade is much smoother than the jump from 2.1 and 2.2, while still giving your the huge performance gains of Ruby 2. Also, if you're on Ruby 1.8.7, we recommend to skip a troublesome upgrade to 1.9.3 and go straight to 2.1.
When trying to make a Rails app run on Ruby 1.9, you're likely to encounter several issues. Here are a few solutions (obviously not exhaustive):
When running `bundle ...
Shell script to generate a Git commit with Pivotal Tracker story ID and title
We usually generate our commit messages from Pivotal Tracker IDs and titles, like
[#15775609] Index view for conflicts
The geordi command commit
automates this. (See: Pretty Commit messages via geordi).
Just run geordi commit
and it will connect to PT and let you select from a list of all started and finishes stories. Then it runs git commit
with the generated message (i.e. all staged changes will be commited).
When running for the first time, Geordi will request your PT...
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.
Check gem dependencies before installation
With gem dependency
it is possible to check the dependencies for your gem before you install it.
Here is an example output for Nokogiri:
Gem nokogiri-1.4.4
hoe (>= 2.6.2, development)
minitest (>= 1.6.0, development)
racc (>= 0, development)
rake-compiler (>= 0, development)
rexical (>= 0, development)
rubyforge (>= 2.0.4, development)
Use look-behind assertions in regular expressions with Ruby 1.8
Regular expressions can have something called "zero-width look-behind assertions". This means that you want a pattern to be preceded by another pattern, but not include the preceding pattern in your match or search cursor. E.g. (?<=x)y
matches y
in xyz
but not in syz
. There are also negative look-behind assertions, e.g. (?<!x)y
matches y
in syz
but not in xyz
.
Unfortunately look-behind assertions are only available in Ruby 1.9. With Ruby 1.8 you need to use an alternative regular expression library called [Oniguruma](http://...
Installing Rails on a fresh system
- Install Ruby from the Ubuntu repository:
sudo apt-get install ruby ruby-dev
\
ruby
is the meta package. If you want to explicitly install 1.8 or 1.9, installruby1.8
orruby1.9
instead (the same applies forruby-dev
). - Do not install RubyGems from the repository but install the version from the webpage instead.
- Get Bundler:
sudo gem install bundler
Rails and other gems for a project should now be installed via bundle install
from the...
console-for opens a Rails console remotely on a Capistrano deployment target
We're adding a script console-for
to open a remote Rails console with one command. Also have a look at shell-for
, which this script is relying on.
Run it from any project directory like this, passing a Capistrano multistage deployment target:
console-for staging
This script is part of our geordi gem on github.
Install the SQLite 3 gem for Ruby under Ubuntu
sudo apt-get install sqlite3 libsqlite3-dev
sudo gem install sqlite3-ruby
How to fix: undefined method `specifications' (caused by RubyGems 1.8)
Sometimes, when running a rake task, RubyGems 1.8.5 raises an error:
rake aborted!
undefined method `specifications' for "/usr/lib/ruby/gems/1.8":String
This has been fixed since May 31 but is still not available as a new RubyGems version.
Either wait for a new version to eventually come out, downgrade to some really old version (1.6.2 works for some) or apply the fix manually:
- Find your rubygems.rb -- mine was located at `/usr/local/lib/sit...
"no such file to load require_relative (MissingSourceFile)" after installing ruby-debug
If you encounter above mentioned failiure message after installing the ruby-debug gem then you have to explicitly require linecache version 0.43 in your Gemfile.
gem 'ruby-debug'
gem 'linecache', '=0.43'
New Cucumber Factory makes it easier to associate records
I pushed a new version of the Cucumber Factory gem. This new release lets you refer to a previously created record by any string attribute:
Given there is a movie with the title "Before Sunrise"
And there is a movie with the title "Limitless"
And there is a movie with the prequel "Before Sunrise"
Note how we didn't have to explicitly give the prequel a name in the example above. This is still possible, but will rarely be necessary now:
Given "Before Sunrise" is a movie with...