使用suds库在RobotFramework中测试Soap 1.2服务



我正在尝试使用RobotFramework测试soap 1.2服务。到目前为止,我们只使用RobotFramework的suds库测试了soap 1.1服务,并且suds与soap 1.2不兼容。

向后兼容性是新服务的一种选择,但最好有一个更长期的解决方案。我不是一个经验丰富的程序员,但如果告诉我要编辑什么以及在哪里编辑,我可以编辑代码。

在我们对使用suds的soap 1.2服务的测试中发生的情况是:suds无法解释它从Web服务获得的响应,并给出以下错误:SAXParseException::159:229:不匹配的标记

soap消息很好,在SoapUI中使用它没有问题。

我在网上发现了一些片段,建议我可以让suds库与soap 1.2一起工作,用于我的RobotFramework测试。但我几乎没有编程经验,也不知道如何将这些片段合并到sud中。有人在这个片段上评论说,这解决了他使用RobotFramework和suds的问题。

有人愿意解释我是如何做到这一点的吗?我一个人似乎想不通。如有任何建议,我们将不胜感激。

from suds.client import Client
from suds.bindings import binding
import logging

USERNAME = 'username'
PASSWORD = 'password'
# Just for debugging purposes.
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
# Telnic's SOAP server expects a SOAP 1.2 envelope, not a SOAP 1.1 envelope
# and will complain if this hack isn't done.
binding.envns = ('SOAP-ENV', 'http://www.w3.org/2003/05/soap-envelope')
client = Client('client.wsdl',
username=USERNAME,
password=PASSWORD,
headers={'Content-Type': 'application/soap+xml'})
# This will now work just fine.
client.service.someRandomMethod()

片段来自:https://gist.github.com/kgaughan/858851

简而言之,Suds不支持SOAP 1.2绑定。发展早就停止了。因此,SudsLibrary也不支持它。

我使用示例服务SOAP 1.1/1.2观察到的一些差异是:

  1. HTTP头Content-Type
    • 1.2="application/soap+xml"
    • 1.1="text/xml"
  2. HTTP标头
    • 1.2=Action
    • 1.1=SOAPAction
  3. Envelope Namespace
    • 1.2="http://www.w3.org/2003/05/soap-envelope"
    • 1.1="http://schemas.xmlsoap.org/soap/envelope/"

下面的示例中分别实现了一个单独的解决方案。内容类型可能被覆盖。可以添加Action,但不能删除SOAPAction。还可以使用扩展库覆盖命名空间。如果您的服务忽略SOAPaction头属性,那么这应该对您有效。

测试用例.robot

*** Settings ***
Library    SudsLibrary
Library    SudsLibraryExtension
Library    Collections    
*** Test Cases ***
TC
${BASE_URL}    Set Variable         http://www.holidaywebservice.com
${SERVICE}     Create Dictionary    
...                                 name=HolidayService_v2    
...                                 wsdl=HolidayService2.asmx?WSDL
${PORT}        Set variable         HolidayService2Soap12
${METHOD}      Set variable         GetCountriesAvailable
Set Binding     SOAP-ENV    http://www.w3.org/2003/05/soap-envelope
Create Soap Client     ${BASE_URL}/${SERVICE.name}/${SERVICE.wsdl}
Set Port    ${PORT}
Set Headers    Content-Type    application/soap+xml
Set Headers    Soapaction      ${EMPTY}
Set Headers    Action          "${BASE_URL}/${SERVICE.name}/${METHOD}"
${result}          Call Soap Method     ${METHOD}

SudsLibraryExtension.py

import suds.bindings
from robot.libraries.BuiltIn import BuiltIn, RobotNotRunningError
class SudsLibraryExtension(object):
"""
Extension on the SudsLibrary
"""
ROBOT_LIBRARY_SCOPE = 'GLOBAL'    
ROBOT_LIBRARY_VERSION = 1.0
def __init__(self, LibraryName='SudsLibrary'):
"""SudsLibraryExtension can be imported with an optional argument.
- ``LibraryName``:
Default value for `LibraryName` is SudsLibrary if not given.
The name can by any Library Name that implements or extends the
SudsLibraryExtension.
"""        
try:
self.SudsLibrary = BuiltIn().get_library_instance(LibraryName)
# This is useful for when you run Robot in Validation mode or load
# the library in an IDE that automatically retrieves the documen-
# tation from the library. 
except RobotNotRunningError:
pass
def set_binding(self, binding, url):
"""Set Binding can be used to add a binding to the message.
Example    Set Binding     SOAP-ENV    http://www.w3.org/2003/05/soap-envelope
"""
suds.bindings.binding.envns = (binding, url)

最新更新