Wednesday, July 18, 2007

data-driven test framework with Socialtext wiki as repository

Socialtext has a tradition of having "Wiki Wednesday" once a month, where people hack little wiki-related projects.

I had a little Wiki Wednesday myself today. Socialtext wikis have a very nice (and not very well documented) REST API. I've been figuring out to use this API in Ruby. I got to the point today where I could read and write pages to the wiki, so I hacked up a little framework using Watir that reads test data from one page and updates another page with the results of the tests.

Wiki markup makes parsing the data really easy, and the REST API makes updating pages really easy.

@test_data_page looks like
| Google Test | Google | Google Search | I'm Feeling Lucky |
This displays as a table in the wiki.

It's in dire need of some refactoring. Reading pages with more than one line of test data is left as an exercise for the reader. :-)

If you want to muck around with it yourself, you could get a free account from socialtext, or try the open source version or the VMWare image.




require 'net/http'
require 'test/unit'
require 'watir'
include Watir

class TC_goog < Test::Unit::TestCase
def test_goog

@user = 'user@socialtext.com'
@pass = 'password'
@host = 'mineral.socialtext.net'
@port = '33333'
@test_data_page = "/data/workspaces/admin/pages/testdata"
@test_result_page = "/data/workspaces/admin/pages/testresults"
@test_results = ""

def get_page(loc)
http = Net::HTTP.new(@host, @port)
http.start do |http|
request =
Net::HTTP::Get.new(loc,initheader = {'Accept' => 'text/x.socialtext-wiki'})
request.basic_auth @user, @pass
response = http.request(request)
response.value
@page = response.body
end
return @page
end

def put_page
req = Net::HTTP::Put.new(@test_result_page, initheader = {'Content-Type' => 'text/x.socialtext-wiki'})
req.basic_auth @user, @pass
req.body = @test_results
response = Net::HTTP.new(@host, @port).start {|http| http.request(req) }
puts "Response #{response.code} #{response.message}:
#{response.body}"
end

get_page(@test_data_page)
puts @page

test_data = @page.split("|")
test_name = test_data[1]
page_title = test_data[2]
button_name1 = test_data[3]
button_name2 = test_data[4]

ie = IE.new
ie.goto("http://www.google.com")

@test_results << @page
@test_results << "|" + test_name
begin
assert_equal(page_title,ie.title)
@test_results << "|PASS"
rescue
@test_results << "|FAIL"
end

begin
assert_equal(button_name2, ie.button(:name, "btnG").value)
@test_results << "|PASS"
rescue
@test_results << "|FAIL"
end

begin
assert_equal(button_name2, ie.button(:name, "btnI").value)
@test_results << "|PASS"
rescue
@test_results << "|FAIL"
end

@test_results << "|"
puts @test_results

put_page

end
end

2 comments:

Christian said...

Seems like a nice example for an easy alternative to FitNesse

Alister S said...

Good idea.

I did something similar with Confluence. Read details here.

Check it out and let me know what you think.