Category: Ruby

Validate JavaScript variables with WATIR

Sunday - 23 November 08

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):
frame = @ie.elements_by_xpath("//[@id='my_iframe_id']")

Access the src of the iframe:
frame[0].src

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!
html = @ie.html

Assert the contents using a regular expression:
assert(html.match(/SOME_PRODUCT="\+escape\(1\)/), "SOME_PRODUCT quantity should be: 1")

Watir resources



NetBeans has TextMate style code templates

Saturday - 09 August 08 :: 2 Comments

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
rattr_reader :attr_names
w attr_writer :attr_names
eaeach { |1| }
eaieach_index { |1| }
eakeach_key { |1| }
delidelete_if { |e| }
::key => "value",
dodo |1|
end
def def method_name

end
if if condition

end
unless unless condition

end
while while condition

end
until until condition

end
DirDir.glob() { |3| }
FileFile.foreach("path/to/file") { |3| }
beginbegin

rescue Exception => 1
   code
end
claClassName = Struct.new(:attr_names) do
   def method_name
   end
end

Unit Test code templates:

Shortcut Result
tsrequire "test/unit"

require "tc_test_case_file"
require "tc_test_case_file"
tcrequire "test/unit"
require "library_file_name"

class Test < Test::Unit::TestCase
   def test_case_name
   end
end
asassert(test, "Failure message.")
aseassert_equal(expected, actual)
asneassert_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.