【问题标题】:Testing Soap 1.2 service in RobotFramework using suds library使用 suds 库在 RobotFramework 中测试 Soap 1.2 服务
【发布时间】:2018-12-05 13:13:50
【问题描述】:

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

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

在我们对使用 suds 的 soap 1.2 服务进行的测试中发生的情况是:suds 无法解释它从 web 服务获得的响应并给出以下错误:SAXParseException: :159:229: mismatched tag

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

我在网上找到了一些 sn-ps,建议我可以让 suds 库与 soap 1.2 一起用于我的 RobotFramework 测试。但我几乎没有编程经验,也不知道如何将这些 sn-ps 合并到 suds 中。 有人在这个 sn-p 上评论说,这解决了他与 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()

sn-p 来自:https://gist.github.com/kgaughan/858851

【问题讨论】:

  • 你使用的是哪个 Python 版本?
  • 验证 1.1 和 1.2 之间的关键区别是 Content-Type 的 HTTP 标头值已更改为 'application/soap+xml'。这应该是你调查的重点。我对 SudsLibrary 的体验并不好,所以我建议尝试分叉或评估自定义实现。
  • 感谢您的回复。我们是 2.7。升级不是现在的选择。到目前为止,出于测试目的,Suds 运行良好(robotframework-sudslibrary)。我曾尝试专注于将 HTTP 标头更改为 application/soap+xml,但到目前为止,我还没有弄清楚如何以一种可行的方式做到这一点。自定义实现实际上是我试图通过集成命名 sn-p 来做的事情。我只是不知道怎么做

标签: soap robotframework webservice-client suds


【解决方案1】:

简而言之,Suds 不支持 SOAP 1.2 绑定。开发已经停止了很长时间。由于这个原因,SudsLibrary 也不支持它。

我使用example service 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 标头属性,这应该对您有用。

Test Case.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)

【讨论】:

  • 非常感谢您的回答!我们正在开发的服务暂时恢复到 1.1。一旦下一个soap 1.2服务可用(应该在几周内),我会尝试解决方案并更新。
猜你喜欢
  • 1970-01-01
  • 2011-02-08
  • 2011-02-12
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多