Recently I was required to add 3rd party JavaScript Tracking Tags to an order confirmation page. The JavaScript passed the quantities of products purchased onto the 3rd party.
I wanted to validate the tags using Watir. It is easy enough to access the DOM using Watir and previously I have validated tracking tags placed in an iFrame url using xpath:
Access the iframe element using xpath (returns an array):Then simply assert the src to ensure the right data was been passed via the URL.
This time I wanted to actually validate variables set up in the JavaScript. At first I was thinking about how I could access the script tag, then a much simpler idea came to me:
Retrieve the source code!
Since owning a mac, I have been an avid user of TextMate. Therefore I have picked up some of the TextMate shortcuts for Ruby.
I mostly develop on Windows in the workplace (not out of choice) so I use NetBeans as my main Ruby IDE on both platforms. So I was surprised to find NetBeans contains many of the TextMate code template shortcuts (Known as Bundles in TextMate).
Here are some of the most useful ones:
To activate a code template type the shortcut name followed by the ->| (TAB key)
| Shortcut | Result |
|---|---|
| rb | #!/usr/bin/env ruby -wKU |
| req | require "" |
| rw | attr_accessor :attr_names |
| r | attr_reader :attr_names |
| w | attr_writer :attr_names |
| ea | each { |1| } |
| eai | each_index { |1| } |
| eak | each_key { |1| } |
| deli | delete_if { |e| } |
| : | :key => "value", |
| do | do |1| end |
| def | def method_name end |
| if | if condition end |
| unless | unless condition end |
| while | while condition end |
| until | until condition end |
| Dir | Dir.glob() { |3| } |
| File | File.foreach("path/to/file") { |3| } |
| begin | begin
rescue Exception => 1 code end |
cla | ClassName = Struct.new(:attr_names) do def method_name end end |
Unit Test code templates:
| Shortcut | Result |
|---|---|
| ts | require "test/unit" require "tc_test_case_file" require "tc_test_case_file" |
| tc | require "test/unit" require "library_file_name" class Test < Test::Unit::TestCase def test_case_name end end |
| as | assert(test, "Failure message.") |
| ase | assert_equal(expected, actual) |
| asne | assert_not_equal(unexpected, actual) |
The main shortcut that is different is 'cla' in NetBeans this creates a new Struct, but in TextMate it gives you a choice of class options.
Fear not! NetBeans lets you add new code templates by going to: Preferences > Editor > Code Templates.
They're loads more unit test code templates too.