Wednesday, February 17th, 2010

Harmony: Bringing together great libraries to enable awesome JS testing in Ruby

Category: JavaScript, Rails, Ruby, Testing

<p>Martin Aumont has released Harmony, which “provides a simple DSL to execute JavaScript and DOM code within Ruby.”

This enables you to do very cool things such as unit test JavaScript in the same area as your Ruby tests:

  1. require 'test/unit'
  2. require 'harmony'
  3.  
  4. class JavascriptTest < Test::Unit::TestCase
  5.   def setup
  6.     @page = Harmony::Page.new
  7.     @page.load('public/javascripts/foo.js')
  8.   end
  9.  
  10.   def test_foo
  11.     assert_equal "world", @page.execute_js(<<-JS)
  12.       foo = new Foo;
  13.       foo.hello();
  14.     JS
  15.   end
  16. end

and you can even use JavaScript libraries…. as script tags are autofetched:

  1. require 'harmony'
  2.  
  3. page = Harmony::Page.new(<<-HTML)
  4.   <html>
  5.     <head>
  6.       <script src="javascripts/jquery.js" type="text/javascript"></script>
  7.     </head>
  8.     <body>
  9.       <div id="widget">ohaie</div>
  10.     </body>
  11.  
  12. HTML
  13.  
  14. page.execute_js("$('#widget').innerHTML") #=> "ohaie"

This library builds on the shoulders of giants, one of which is Mr. Johnson, John Barnette who I had the pleasure of working with many moons ago. He is the person I think of when I remember that the best engineers that I have worked with haven’t been computer scientists, but musicians and biologists. He is also a great fun guy.

Anyway, sorry for the aside.

If you are a Rails chap, you may also be interested in the Rails plugin holygrail.

Related Content:

Posted by Dion Almaer at 6:05 am
3 Comments

+++--
3.1 rating from 21 votes

3 Comments »

Comments feed TrackBack URI

“the best engineers that I have worked with haven’t been computer scientists, but musicians and biologists.”

So true.

Comment by jeffwad — February 17, 2010

This is a browser-less, console based javascript testing environment. It’s not useless but not what we normally need to test browser based applications. For most scenarios Selenium or the like are still the way to go. Furthermore it uses Mozilla’s JS engine. With Selenium we can use any JS engine.

Comment by pmontrasio — February 17, 2010

The name is unfortunate. People familiar with JavaScript development already associate “Harmony” with the next ECMAScript version, which is supposed to unite the efforts of the ES3 and ES4 working groups.
But maybe it’s intentional; they’re joining the ranks of other confusingly named JS projects: Prototype, Closure, Scope… Me, I’m probably going to name my new project “Script”. Or maybe “Java”, that could get interesting.

Comment by wspace — February 18, 2010

Leave a comment

You must be logged in to post a comment.