making things better, making better things

Tuesday, September 6, 2011

awake! from nib!

Tonight’s MacRuby/Xcode lesson: An object (say, an instance of a subclass of NSArrayController) created in Interface Builder won’t necessarily have its initialize method called when the program runs. This, I guess, is because the object was instantiated when you created it in Interface Builder, and cryogenically frozen, and when your program runs, it doesn’t instantiate the object, it merely wakes it up. So you should put the initialization code in awakeFromNib. I think.

(The init method on an instance of a subclass of NSObject was called. I don’t know why init and not initialize. I’ve got a lot to learn.)

posted by erik at 7:32 pm  

Wednesday, June 29, 2011

Tumblr Tags

I just put another hobby project on GitHub. It’s a web service that’s meant to help me (or, in theory, someone else) insert a dynamic list of most-used tags on my Tumblr blog. It doesn’t really help with the inserting, yet, but it will get me the list. And it gave me some experience with Sinatra and MongoDB!

posted by erik at 12:26 pm  

Friday, June 10, 2011

I wrote something with MacRuby, finally. It’s a tool to help me post music every day over on my non-technical blog. The MacRuby part is that it uses iTunes to convert uncompressed audio to MP3, add metadata, and post it to my web site. And add it to my library along the way. That’s pretty cool. The script also uploads the track to my web site, but that’s straight Ruby.

In retrospect I think iTunes is not the right tool for this job. It’s a hammer, not a scalpel. It doesn’t give you a lot of fine control, and it takes more energy than the task really warrants. For example, sometimes I need to find a specific track in the library. The API iTunes offers through Scripting Bridge doesn’t let me look up a track by multiple criteria (track 28 from the album “Music Every Day” in MP3 format) – it just gives me the same keyword search you find in the UI. So I can search for the album “Music Every Day” and then loop through the returned tracks looking for the one I need. It gets the job done, but it’s not ideal.

So if I were starting over I might use LAME and RubyMp3info, or something. But it was interesting to get some practice with MacRuby, and if I ever decided to put a GUI on this it’ll be good to have had the experience. The code is on GitHub, if you’re curious.

posted by erik at 11:51 am  

Friday, May 6, 2011

my plans for Minnebar

Posted here, because why write something down if you’re not going to put it on the web somewhere?

9:40: Geeks Connecting Neighbors. Because talking with a bunch of fellow geeks about how to use technology to help people connect with their neighbors seems easier than actually talking to my neighbors. Backup: Mirah.

10:40: Making Music Like a Nerd II. I make software and I make music, but not at the same time, and for decades now I’ve been alternately intrigued and irritated by the tools to do it. Time to look again. Backup: Maps? Fonts? Android?

11:40: Building the Tech Community You Want to See. Because I like diversity. Backup: ?

1:40: I don’t know, maybe social networks.

2:40: OpenStreetMap 101. I like maps! And openness! I used OSM on Touring Machine and was impressed. Backup I should be going to instead: Negotiating Consulting Contracts That Work.

3:40: Color Theory & Consumption. Not sure why. Backup: Get More From Your Data, or Why Don’t You Blog More?

4:40: Personal Rapid Transit. I’ve been skeptical about this concept since I read Bruno Latour’s Aramis back in the ’90s, but I don’t really remember why. Time for a refresher course!

Minnebar is “an (un)conference aimed at getting those in Minnesota’s tech and design communities together to discuss topics that interest them.” It happens tomorrow, and it’s sold out.

posted by erik at 3:08 pm  

Thursday, January 13, 2011

“Element not found in the cache”

I was getting the above error, intermittently, from Selenium/WebDriver via Cucumber/Capybara. My code looked something like this:

When /^I wait for the popup to close$/ do
  wait_until { !page.find('#nyroModalContent').try(:visible?) }

What’s happening, I think, is this:

  1. Capybara asks Selenium for a handle to the #nyroModalContent element.
  2. Selenium, after returning it, gets the response from my server that tells it to close the popup.
  3. Capybara asks Selenium, hey, that element you just gave me a handle to? Is it still visible?
  4. Selenium is all, “What element?”

For my present purposes, it turns out I don’t need the “visible” check, so I could just do this:

  wait_until { page.has_no_css?('#nyroModalContent') }

If I had needed to get the element and then act on it, though, I think the way to do it would be to write JavaScript that performs both actions, and call it with page.evaluate_script.

[Ran across John Nunemaker's exhortation to "clog it before you forget it."]

posted by erik at 11:25 am  

Monday, April 19, 2010

sane Sinatra stack traces in Cucumber

I’m building an app in Sinatra and testing it (in part) with Cucumber. When exceptions were raised in my application, I’d get a stack trace wrapped in pages and pages of HTML – very nice when you’re testing in a browser, but almost impossible to work with in a command-line test. After a surprisingly long investigation, I added these lines to env.rb:

# Don't generate fancy HTML for stack traces.
disable :show_exceptions
# Allow errors to get out of the app so Cucumber can display them.
enable :raise_errors

Both are required. If you just disable :show_exceptions, you’re spared the fancy HTML, but you’re also spared any useful information about what’s going wrong. (You just get “Internal Server Error”.) But if you just enable :raise_errors, well, the errors don’t get raised – they’re trapped by the exception-showing code.

posted by erik at 8:52 am  

Wednesday, December 23, 2009

setting up browser automation with FireWatir

Ruby-based testing will only get you so far in the web development world – sooner or later you need to make sure your code works in a real browser. So for the last few years I’ve been occasionally trying to get a browser-based test automation tool running – often something in the Selenium family. It usually takes a full day for me to give up, usually because I just can’t get the tool to actually drive the browser.

Today I got FireWatir working! It wasn’t that hard, but I had to consult a few different web pages, so here it is all in one place.

  1. Via FireWatir Installation, install the gem:
    sudo gem install firewatir

    and the JSSh Firefox extension (Mac FF 3.5 version) to make Firefox steerable.

  2. Set up a Firefox profile, so you can run tests without all your weird extensions in play, and without messing with your personal browser session and history. Mine is called “tester”.
  3. Start up Firefox with JSSh and your new profile (you can do this while Firefox is running):
    /Applications/ -jssh -P "tester"
  4. In an irb session, try some stuff – for example, on a new Spree instance:
    ff =
    ff.goto 'http://localhost:3000/', 'Clothing').click

Voila! Now I want to try Cucumber and Watir.

posted by erik at 12:14 pm  

Friday, December 11, 2009

John Resig on JavaScript testing, performance analysis, and jQuery 1.4

John Resig, of jQuery and other fame, spoke tonight at the BayJax meetup in the Yahoo! cafeteria. His slides are probably online somewhere, but they are lucid, informative, illustrated with pictures and code samples, and written by someone who knows what he’s talking about, not to mention who was paying attention while writing them. (Update: Video on iTunes and not on iTunes.) If that’s not your bag, you’ll want to read my notes instead:

posted by erik at 11:39 pm  

Saturday, October 31, 2009

sometimes I’m not smart

You know those times when you’re looking all over the house for something and then somebody says, “Is that it in your other hand?”

Last night I spent at least an hour trying to figure out why I was getting a nil.rewrite error from Rails’s url_for method. I’ve seen hard-to-debug errors from there before, so I dug right down into it, but I couldn’t figure out why the @url variable was set in some cases and not in others… and then I determined that it actually was being set in all cases, but then it was mysteriously blank by the time url_for needed it….

And then I realized it was because I had a @url variable in my own controller action. And then I went to bed.

Most of the time I really love how dynamic and open Ruby is as a language, but sometimes private instance variables are just what you’d like to have had.

posted by erik at 11:39 am  

Saturday, August 22, 2009

assert_select from arbitrary text

A useful testing method in Rails is assert_select. In a functional test, you can use it to make sure the controller’s response has the HTML you expect:

should "display the suggestion as a link" do
  assert_select 'a[href=?]', @click_url, @display_url

… says there should be an <a> tag with the given href attribute and content. Clearer and less brittle than computing a string and searching for it.

But what if you want to assert something about the HTML structure of something that isn’t the controller response? Like, if you want to look for a link in the flash hash?

I just added this to my test_helper.rb:

class ActiveSupport::TestCase
  def assert_select_from(text, *args)
    @selected =

Now in my tests I can use the full power of assert_select on an arbitrary string:

should "provide an undo/delete link" do
  assert_select_from(flash[:notice], 'a[href=?]',
    venue_path(assigns(:venue)), 'undo')
posted by erik at 8:57 pm  
Next Page »

Powered by WordPress