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
.
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.
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...
Defining custom errors in Ruby
class Errormaster
CoffeeIsOut = Class.new(StandardError)
# is prettier than
class CoffeeIsOut < StandardError; end
end
Reference such an error class with Errormaster::CoffeeIsOut
.
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...
mojombo/grit - GitHub
Grit gives you object oriented read/write access to Git repositories via Ruby.
Rails 3.1.0 has been released!
jQuery as new default Javascript library, streaming response support, attr_accessible with roles, prepared statements, easier migrations.
Ruby: Convert a time string to your local time zone
If you have a time given in a different time zone than your local one, parsing will convert it for you:
>> Time.parse('September 2nd, 3pm PST')
=> 2011-09-03 01:00:00 +0200
Note that in pure Ruby you need to require "tzinfo"
(Ruby 1.9) or require "time"
(Ruby 1.8) for Time.parse
to be available.
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
-----------------------------------------------------...
How to use Rails URL helpers in any Ruby class
If you have any class which requires access to some path methods generated by your routes. Even though you could technically include Rails.application.routes.url_helpers
, this may include way too many methods and even overwrite some class methods in the worst case.
Instead, most of the time the following is advised to only make the desired methods available:
class Project
delegate :url_helpers, to: 'Rails.application.routes'
def project_path
url_helpers.project_path(self)
end
end
Invoices: How to properly round and calculate totals
While it might seem trivial to implement an invoice that sums up items and shows net, gross and vat totals, it actually involves a lot of rules and caveats. It is very easy to create invoices where numbers don't add up and a few cents are missing. A missing cent is a big deal for an accountant, so it is important for your invoices to list correct numbers.
Note that this is not legal advice. Also note that while this note has a number of code examples in Ruby and MySQL, the concepts apply to all programming languages and data stores.
When ...
Always show all form errors during development
You've been there: A form cannot be submitted, but you don't see a validation error because the field at fault has no corresponding input field on the form. Because this is usually a bug, you insert debug information listing all errors into the form view. And once the bug is fixed, you forget to take out that debug information.
There is a better way. By copying one of the attached initializers into config/initializers
, your forms will always render a small box listing all form errors in the bottom right corner of the screen. This box is n...
Time#utc, Time#gmt and Time#localtime are destructive methods
Calling Time#utc
, Time#gmt
or Time#localtime
will not create a converted copy. Instead these methods modify the receiving Time
object:
>> time = Time.now
=> Thu Aug 25 09:52:28 +0200 2011
>> time.utc
=> Thu Aug 25 07:52:28 UTC 2011
>> time
=> Thu Aug 25 07:52:28 UTC 2011
This can have unexpected side effects when other code is holding pointers to the Time
object you are modifying. To be safe, call these methods on a clone of the Time
object. You can clone a Ruby object by using [#dup
](http://...
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
...
Random list of ActiveSupport goodies
I recently browsed through the ActiveSupport code and found some nice stuff I did not know about:
ActiveSupport::Callbacks
-
ActiveRecord-like callbacks, if you need callbacks in non ActiveRecord objects
ActiveSupport::MessageEncryptor
-
encrypt and decrypt ruby objects
[ActiveSupport::MessageVerifier
](https://github.com/rails/rails/blob/mast...
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
.
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...
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...
Downloading files from Ruby on Rails
To offer files for download, use send_file
.
def download(file)
send_file file.path, :disposition => 'attachment'
end
Note that a send_file
replaces the default :render
action.
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...