首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Groovy脚本在Canoo Webtest步骤中根据网页的doctype (dtd)验证网页

使用Groovy脚本在Canoo Webtest步骤中根据网页的doctype (dtd)验证网页
EN

Stack Overflow用户
提问于 2010-11-15 22:02:16
回答 1查看 306关注 0票数 1

如何在使用Groovy的Canoo Webtest步骤中根据其文档类型(dtd)验证网页?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-15 22:07:16

实际上我知道答案。但由于我花了一段时间才让它工作,我想我应该分享我的解决方案。这是一个webtest宏。您也可以只使用序列,如果您喜欢...

代码语言:javascript
复制
<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语句。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4185052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档