1. What's WebUnit
WebUnit is Ruby API for testing html/cgi without browser, and is idealy suited for automated unit testing of web sites when combined with a Ruby unit test framework such as RubyUnit. It was designed and implemented by Masaki Suketa.
I used HttpUnit with JUnit, however, I didn't like writing test in Java, when I write program in Ruby. Then, I made it.
2. Libraries Used
WebUnit uses these software follow. Now, these files of htmlrepair/htmlsplit and html-parser are included in WebUnit package, so, you don't have to install them. You have to install RubyUnit and NQXML, before using WebUnit.
- RubyUnit - http://homepage1.nifty.com/markey/ruby/rubyunit/index_e.html
- htmlrepair/htmlsplit - http://www.moonwolf.com/ruby/
- html-parser - http://www.jin.gr.jp/~nahi/Ruby/html-parser/README.html
- NQXML - http://www.io.com/~jimm/downloads/nqxml/index.html
3. Download and Install
Download webunit-20YYMMDD-*.tar.gz from http://xpenguin.biz/download/webunit/, and, extract it to some directory, then, do ``ruby install.rb''.
4. Example
Execute test/test_*.rb with test runner like runtest.rb, after make symbolic link cgi directory to accessable directory via httpd, with name `webunit'.
% ln -s webunit/cgi $DOCUMENT_ROOT/tmp/webunit % cp webunit/cgi/auth/.htpasswd /tmp % URLBASE=http://localhost/tmp/ runtest.rb test_link.rb ----- require 'webunit/webunit' # get start HTML page url = 'test-1.html' response = WebUnit::Response::get( url ) # test HTML page contents assert_title( 'test-1', response ) assert_equals( 1, response.forms.size ) form = response.form # test form of HTML page contents assert_equals( 'ntext', form.parameters[0].name ) assert_equals( 'npassword', form.parameters[1].name ) # execute CGI script, after setting parameters form.parameters[0].value = "this is test" form.parameters[1].value = '!@#$%' response = form.submit # test output from CGI script assert_equals( 'result', response.title ) assert_equals( 1, response.links.size ) assert_equals( 'http://....', response.links[0] ) # read link response = response.links[0].read -----
5. Copying
This Program is copyrighted free software by yuichi TAKAHASHI.
You can redistribute it and/or modify it under either the terms of the GPL (see COPYING file), or same as Ruby.
6. Bug Report
7. Changes
20020410-alpha -> now
20020330-alpha -> 20020410-alpha
- XmlDocument -> DomWalker : rename
- fix for 1.7 parser.(Thanks to Kazuhiro NISHIYAMA)
- fix install.rb for "--program-suffix".(Thanks to Kazuhiro NISHIYAMA)
20011019-alpha -> 20020330-alpha
- XmlDocument#size : new.
- mail addres of author is changed.
20011019-alpha -> 20011022-alpha
- XmlDocument#change_node : bug fix.
20011002-alpha -> 20011019-alpha
- XmlDocument : new - class to test xml document.
20010919-alpha -> 20011002-alpha
- Parser#do_input : support input tag without form tag. (Thanks to IKAGAWA, Kauztomo)
- Response#init_http : append header(Content-Type) for POST method.
- Response#init_http : catch IOError of HTTP::finish (for ruby 1.6.5)
20010912-alpha -> 20010919-alpha
- Response#get,post,init_http : to use BasicAuth.
- sample/cgi : remove space charactor between '#!' and '/usr/bin/env'
20010714-alpha -> 20010912-alpha
- Responss : use Net::HTTP::new_implementation.
- HtmlElem : fix inspect output when @tag is nil.( as toplevel )
- sgml-parser.rb(line:257) : bug fix, fail when nested tags.
- html-parser/install.rb : fix.
20010618-alpha -> 20010714-alpha
- Parser : add link tag to SingleTags.
- Parser#start_a : execute complete_url, unless the href begins '#'.
- Link : append onClick attribute, and reader.
- Response#init_http : retry, when Errno::EPIPE
- Response::html : append <html> tag, when there is no <html> tag.
20010611-alpha -> 20010618-alpha
- add sample
20010604-alpha -> 20010611-alpha
- HtmlElem#name : new
- Response#init_html : new, to use WebUnit without http access.
- (code using Response#new) : change to Response#new.init_http
- Response#init_http,feed, : divide initialize.
- perse_url : correspond to relative url and 'file:' prefix.
- perse_url : add '-' to regexp, will match to hostname.
- assert_urls : add success case, use orthop_url for expected url.
20010521-alpha -> 20010604-alpha
- include contrib/{html-parser,htmlrepair} : Thanks these authors.
- html-parser : Takahiro Maebashi
- htmlrepair : MoonWolf
- installer supports to install contrib/{html-parser,htmlrepair}
20010507-alpha -> 20010521-alpha
- Response#initialize : use $URLBASE when url is relational
- Parser#do_img: new
- Response#add_image, Response#images, Response#image : new
- Image : new
20010330-alpha -> 20010507-alpha
- sample, contrib: only directory
- install.rb : new
- all files : reject TAB code ( expect COPYING and Makefile )
- bin : make some commands
- doc : separate document files from cgi directory
- tests : split files, and add testcase methods.
- Utils#complete_url : bug fix
- TestCase#setup : set $URLBASE as @urlbase, Response::reset, restore Cookies
- TestCase#teardown : Response::reset, keep Cookies
- Cookies#cookies/cookies= : new
- Response#reset: close http connection
- assert_urls : new
- WebUnit#assert_equal_url : -> assert_urls
- Select#value : new
- assert_select_option : new
- HtmlElem#initialize : add default value for 2nd argument.
- assert_title : new - show response.body with message.
- HtmlElem#extract : new - make string with nested HtmlElems.
- Response#pbody : use Response#format
- Response#format : new
- MemWriter : new - used by Response#format
20010314-alpha -> 20010330-alpha
- Response#pbody : new - print orthopped Response#body.
- Select#select : accept Array of String(value|data) and Integer(index)
- assert_include : add using HtmlElem#has?
- Link#target : new
- Response#freeze,Form#freeze : new
- Select#value= :
- Form#submit :
20010223-alpha -> 20010314-alpha
- Paser#start_a : replace "&" -> "&" of href. bug of sgml-parser?
- Table#row_index,col_index : new
- Form#store_test_data : new
- assert_input,assert_attrs,assert_include : new
- InputRadio#value= : bug fix
- Textarea#initialize, Select#initialize, SelectOption#initialize : set type
- Parser#attrs_to_hash : bug fix
- Response#push : no longer support, puts WARNING
- Response#click : support for readlink and submit
- Utils#complete_url : <- Response#complete_url & Parser#complete_url
- JSciriptOpenObject : new
- Response#redirect : new
- Form#submit, Response#submit : support InputImage ( add args )
- InputImage : new
- Input#inspect : bug fix.
- Response#initialize : better performance with keeping connection.
- Table#at : to access TableRow with one argument. ( two args for Cell )
- Parser#complete_url : use orthop_url
- Utils#orthop_url : '//', '/\./', '[^/]*/\.\./' -> '/'
- TestCase#initialize : append '/' if last char of URLBASE isn't '/'
- TableRow : new ( Table is also changed )
- Response : attr_accessor -> attr_reader
- HtmlElem#inspect : change format, and stop recursive.
- HtmlElem#print : change format as inspect.
- Response#readlink : use HtmlElem#readlink
- HtmlElem#readlink : new
- Response#click : alias of Response#readlink
- Response#submit,push : same as Response#forms[0].submit
- Response#params : same as Response#forms[0].params
- Response#link,form,table,frame : same as Response#*[0]
- Response#search,find : new
- HtmlElem#search : new
- change file struct.
20010216-alpha -> 20010223-alpha
- Cookies#header : bug fix. ( handle when path == nil )
- Parser#endtag : raise BadHtmlTags exception
- Response#readlink : new ( raise ElemNotFound when failed )
- rename exception : NotFound -> HttpNotFound
- unify linefeed code to '\n'(0x0a) on each files.
20010215-alpha -> 20010216-alpha
- Frame#read : bug fix.
- Response#initialize : raise NotFound exception
- Param#query_data : rename method from data
- InputRadio#query_data : rename method from data
- InputCheckBox#query_data : rename method from data
- InputSubmit#query_data : rename method from data
- InputReset#query_data : rename method from data
- Select#query_data : rename method from data, and so on.
- SelectOption#query_data : rename method from data
- Form#submit : Param#query_data -> Param#query_data
- SelectOption#value : return data, when value is nil
- SelectOption#value= : remove
- SelectOption#data : remove
- SelectOption#data= : remove
- SelectOption#stat= : remove
- Param#value : method -> attr_accessor
- InputRadio#value= : new ( from Param#value )
- Textarea#value : new ( as SelectOption#value )
- Cookies#add : new