1
如何使用Groovy在Canoo Webtest步驟中針對其網上論壇的doctype(dtd)驗證網頁?使用Groovy腳本在Canoo Webtest步驟中針對其doctype(dtd)驗證網頁
如何使用Groovy在Canoo Webtest步驟中針對其網上論壇的doctype(dtd)驗證網頁?使用Groovy腳本在Canoo Webtest步驟中針對其doctype(dtd)驗證網頁
其實我知道答案。但是,我花了一段時間才搞定它,我想我會分享我的解決方案。這是一個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語句的處理程序。