• [SoapUI]怎样运用Schema通过*.xsd文件来验证response对应的xml文件


    添加Groovy Script脚本对Test Step进行验证

    脚本如下(已经运行通过):

    import javax.xml.XMLConstants
    import javax.xml.transform.stream.StreamSource
    import javax.xml.validation.SchemaFactory
    
    //Load the XSD from a file
    def xsd = new File('D:\DOAutomationTest\Automation_Test_DO_IpadForAdvisor_SoapUI\Schemas\schema_Clients.xsd').text
    log.info "xsd = "+xsd
    
    //Get the XML from the response
    def response = testRunner.testCase.testSteps["testStepName"].testRequest.response.contentAsString
    log.info "response = "+response
    
    def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))
    def validator = schema.newValidator()
    
    try 
    {
    validator.validate(new StreamSource(new StringReader(response)))
    } 
    catch( Exception ) 
    {
    assert false
    }
    

     一个启发我的Sample(也可以运行成功):

    class XmlExamples {
      static def CAR_RECORDS = '''
        <records>
          <car name='HSV Maloo' make='Holden' year='2006'>
            <country>Australia</country>
            <record type='speed'>Production Pickup Truck with speed of 271kph</record>
          </car>
          <car name='P50' make='Peel' year='1962'>
            <country>Isle of Man</country>
            <record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
          </car>
          <car name='Royale' make='Bugatti' year='1931'>
            <country>France</country>
            <record type='price'>Most Valuable Car at $15 million</record>
          </car>
        </records>
      '''
    }
    
    def xsd = '''
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
      <xs:element name="records">
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="car"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="car">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="country"/>
            <xs:element ref="record"/>
          </xs:sequence>
          <xs:attribute name="make" use="required" type="xs:NCName"/>
          <xs:attribute name="name" use="required"/>
          <xs:attribute name="year" use="required" type="xs:integer"/>
        </xs:complexType>
      </xs:element>
      <xs:element name="country" type="xs:string"/>
      <xs:element name="record">
        <xs:complexType mixed="true">
          <xs:attribute name="type" use="required" type="xs:NCName"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    '''.trim()
    
    import javax.xml.XMLConstants
    import javax.xml.transform.stream.StreamSource
    import javax.xml.validation.SchemaFactory
    
    def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))
    def validator = schema.newValidator()
    validator.validate(new StreamSource(new StringReader(XmlExamples.CAR_RECORDS)))
    

      

  • 相关阅读:
    功能3 -- 基于jquery的load()实现多个.html静态页,引用同一个header.html和footer.html文件
    功能2 -- hover出现遮照效果
    功能1 -- 顶部导航栏和返回顶部效果
    使用swiper简单的h5下滑翻页效果,
    Vue源码阅读,如何渲染代码块生成? 本文详解
    这15道Vue常见面试题,你会几道??
    学会这些Vue小技巧,可以早点下班和女神约会了!
    TypeScript在Model中是如何操作运用的?本文详解
    Vue3教程,抢先学习
    Webpack 5模块联邦会不会引发微前端的革命呢? 本文详解
  • 原文地址:https://www.cnblogs.com/MasterMonkInTemple/p/3939445.html
Copyright © 2020-2023  润新知