如何使用Groovy在Canoo Webtest步骤中针对其doctype(dtd)验证网页?
答案 0 :(得分:1)
其实我知道答案。但是由于我花了一段时间才使它工作,我想我会分享我的解决方案。这是一个webtest宏。如果您愿意,也可以只使用顺序...
<macrodef name="verifySchema" description="Validate the current document against its 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>
如果您想对警告测试失败,请相应地向处理程序添加throw语句。