xml - Validate a webpage against its doctype (dtd) within a Canoo Webtest step using a Groovy script -
how can validate webpage against doctype (dtd) within canoo webtest step groovy?
actually know answer. took me while working, thought i'd share solution. it's webtest macro. can use sequential if like...
<macrodef name="verifyschema" description="validate current document against schema"> <sequential> <groovy description="validate schema" > import javax.xml.parsers.parserconfigurationexception import javax.xml.parsers.saxparser import javax.xml.parsers.saxparserfactory import java.io.inputstreamreader import org.xml.sax.errorhandler import org.xml.sax.inputsource import org.xml.sax.saxexception import org.xml.sax.saxparseexception import org.xml.sax.xmlreader class myhandler implements org.xml.sax.errorhandler { void warning(saxparseexception e) throws saxexception { println 'warning: ' + e.getmessage() } void error(saxparseexception e) throws saxexception { println 'error: ' + e.getmessage() throw e } void fatalerror(saxparseexception e) throws saxexception { println 'fatal: ' + e.getmessage() throw e } } def factory = saxparserfactory.newinstance() factory.setvalidating(true) factory.setnamespaceaware(true) def parser = factory.newsaxparser() def reader = parser.getxmlreader() reader.seterrorhandler(new myhandler()) def response = step.context.currentresponse.webresponse reader.parse(new inputsource(new inputstreamreader(response.contentasstream,"utf-8"))) </groovy> </sequential>
if want fail test on warnings too, add throw statement handler accordingly.
Comments
Post a Comment