From: Curt Sampson Date: 2005-01-24T17:07:07+09:00 Subject: Re: Web Testing in Ruby On Fri, 14 Jan 2005, Jim Weirich wrote: > But I'm running Linux at home and a non-IE solution would be great. Have you > considered writing a ruby-based browser skeleton? Just enough browser to > request web pages and give information to your browser controller software. > Actually rendering wouldn't be necessary (I think). It would be quite useful. I've got a simple little framework that I use for testing my web-based application. I originally hacked it together for testing some Java servlets, but since then it's also been used on a ruby website and is currently testing a PHP website. It sends HTTP requests to a server and pulls back the result, so it can be used for testing anything served by a web server. I suspect it's rather similar to webunit (http://www.xpenguin.biz/download/webunit/index-en.html). A bunch of the code I use is not in this framework itself, but is in the abstract testcase from which my concrete testcases inherit. I don't know how much I should move up; some if it is pretty application-specific. But here's a not untypical test, and its support routine: def test_logout send_request('/ID0123456789abcdef0123456789abcdef') # logs in user assert_image("logo image", "images/logo") assert_page_match("user name", %r|Welcome #{user_name}|) assert_link('Options', 'Options', 'user_setting.php') assert_link('Logout', 'Logout', 'logout.php') click("Logout") assert_link('Login', 'Login', 'login.php') assert_link('Register', 'Register', 'page/register') end def click(link_text) xpath = '/html//' # XXX Quick hack - what made xpath unhappy about the HTML thing? xpath = '//' new_request = response.get_request_from_link(link_text, xpath) if (new_request.nil?) message = "No A tag enclosing "#{link_text}" found. Text present:" response.get_text(xpath + 'a').each { |text| message += "n " + text } fail(make_assert_failure_message(message)) end @request = new_request send_request end You can find it as the "test-httpweb" project on rubyforge; it requires xpath and html-parser. Not really documented or anything, but feel free to play around with it a bit and send me e-mail if you want help. cjs -- Curt Sampson +81 90 7737 2974 http://www.NetBSD.org Make up enjoying your city life...produced by BIC CAMERA