On Sat, 2006-05-20 at 06:06 +0900, Brian C. wrote:
<name>button 1</type>
somehow i have to get THAT modeled into an object
script object contains test-cases, test-cases contain test-steps etc…
Looks like an ideal DigestR1 opportunity, if you’re able to get
Libxml-ruby installed too(*):
#!/usr/local/bin/ruby
require ‘xml/digestr’
require ‘pp’
class Script
attr_accessor :name, :starturl, :testcases
def initialize; @testcases = []; end
end
class TestCase
attr_accessor :id, :steps
def initialize; @steps = []; end
end
class TestStep
attr_accessor :id, :tests, :checks
def initialize; @tests, @checks = [], []; end
end
class Check
attr_accessor :elements
def initialize; @elements = []; end
end
class Test < Check
attr_accessor :interaction
end
class Element
attr_accessor :name, :type
end
d = XML::Digester.new(true)
d.add_object_create(‘/script’, Script)
d.add_call_method(‘/script/project-name’, :name=)
d.add_call_param(‘/script/project-name’)
d.add_call_method(‘/script/start-url’, :starturl=)
d.add_call_param(‘/script/start-url’)
d.add_object_create(‘/script/test-case’, TestCase)
d.add_set_properties(‘/script/test-case’)
d.add_link(‘/script/test-case’) { |sc,tc| sc.testcases << tc }
d.add_object_create(‘/script/test-case/test-step’, TestStep)
d.add_set_properties(‘/script/test-case/test-step’)
d.add_link(‘/script/test-case/test-step’) { |tc,ts| tc.steps << ts }
d.add_object_create(‘/script/test-case/test-step/test’, Test)
d.add_link(‘/script/test-case/test-step/test’) { |ts, t| ts.tests << t }
d.add_call_method(‘/script/test-case/test-step/test/interaction’,
:interaction=)
d.add_call_param(‘/script/test-case/test-step/test/interaction’)
d.add_object_create(‘/script/test-case/test-step/check’, Check)
d.add_link(‘/script/test-case/test-step/test’) { |ts, t| ts.checks << t
}
d.add_object_create(‘/element’, Element)
d.add_link('/element’) { |p, ele| p.elements << ele }
d.add_call_method(‘/element/name’, :name=)
d.add_call_param('/element/name’)
d.add_call_method(‘/element/type’, :type=)
d.add_call_param('/element/type’)
script = d.parse_file(‘watir.xml’)
pp script
END
This outputs (with the data you posted, with some mismatched close tags
fixed up):
#<Script:0xb7e8d64c
@name=“My Project”,
@starturl=“http://localhost:3000/”,
@testcases=
[#<TestCase:0xb7edcaec
@id=“1”,
@steps=
[#<TestStep:0xb7edae90
@checks=
[#<Test:0xb7ed9978
@elements=[#<Element:0xb7ed7dbc @name=“button 1”,
@type=“button”>],
@interaction=“double click”>],
@id=“1”,
@tests=
[#<Test:0xb7ed9978
@elements=[#<Element:0xb7ed7dbc @name=“button 1”,
@type=“button”>],
@interaction=“double click”>]>,
#<TestStep:0xb7e63ff0 @checks=[], @id=“2”, @tests=[]>]>,
#<TestCase:0xb7e63a14 @id=“2”, @steps=[]>]>
Which I think is what you’re after?
(*): If you can’t/won’t install native extensions, DigestR’s API is
intended to be mostly compatible with an older, REXML-based (IIRC)
digester at http://rubyforge.org/projects/xmldigester