REXML logging

A

aidy

Sorry to bother you again

I have two test classes

<snip>
class TEST_1
def initialize
$log.xml_logger(self.class, 'pass')
end
end
<snip>

<snip>
class TEST_2
def initialize
$log.xml_logger(self.class, 'fail')
end
end
<snip>

I also have a logging class
<snip>
class Logging
def initialize
@doc = Document.new
xmldecl = XMLDecl.default
@doc.add(xmldecl)
end

def xml_logger(test_name, vp_text)
test_result = @doc.add_element 'testresults'
test = test_result.add_element 'test'
test.attributes["id"] = test_name
if vp_text.upcase == 'PASS' then
test_status= test.add_element 'pass'
test_status.text = 'PASS'
else
fail = test.add_element 'fail'
fail.text = 'FAIL!'
end

end

def xml_to_file
@doc.write($stdout, 1)
@doc.write(File.open("C:/test_results.xml","w"))
end

end
<snip>

I have a file that starts the logger, runs the tests and then closes
the log

$log = Logging.new
TEST_1.new
TEST_2.new
$log.xml_to_file

Now, what I am currently getting is this

<testresults>
<test id='TEST_1'>
<pass>PASS</pass>
</test>
</testresults>

and what I actually want is this
<testresults>
<test id='TEST_1'>
<pass>PASS</pass>
</test>
<test id='TEST_2'>
<fail>FAIL</fail>
</test>
</testresults>

what I really want to do, is keep adding to the xml file until, I
invoke the #xml_to_file method.

Should I be creating a Document.new every time I log?

Cheers

Aidy
 
Z

zycte

Each time you call xml_logger, you are creating a new root element of
the document. Since an XML document can only have one valid root, you
will only have one entry in your file.

Solution is to add the root node "testresults" in the Logging
constructor, and add a child element to the "testresults" every time
xml_logger is called.

class Logging
def initialize
...
@root = @doc.add_element 'testresults'
end

def xml_logger(test_name, vp_test)
test = @root.add_element 'test'
...
end
end
 
A

aidy

Hi zycte

Thanks for the help. The XML is perfect

<testresults>
<test id="ST_LTD_1">
<pass>PASS</pass>
</test>
<test id="ST_LTD_2">
<fail>FAIL!</fail>
<failmessage>security not implemented gonna fail </failmessage>
</test>
</testresults>

I've written some XSLT so when I am happy with it, I'll post it to the
Watir\Ruby group.

Aidy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,293
Latest member
BonnieHamb

Latest Threads

Top