【问题标题】:Creating Soap messages with objectTypes using SUDS library and MessagePlugin in Robot Framework在 Robot Framework 中使用 SUDS 库和 MessagePlugin 创建带有 objectTypes 的 Soap 消息
【发布时间】:2016-07-27 08:32:18
【问题描述】:

这是这个问题的延续:Creating Soap messages with objectTypes using SUDS library in Robot Framework

在那里确定,使用 RF SUDS 无法单独使用 SUDS 创建具有 objectTypes 的消息。我想尝试 MessagePlugin 方法,但不幸的是文档中的信息对我来说还不够:

from robot.libraries.BuiltIn import BuiltIn
from suds.plugin import MessagePlugin

class _MyPlugin(MessagePlugin):
    def marshalled(self, context):
        body = context.envelope.getChild('Body')
        foo = body[0]
        foo.set('id', '12345')
        foo.set('version', '2.0')

class SudsLibraryExtensions(object):
    def attach_my_plugin(self):
        client = BuiltIn().get_library_instance("SudsLibrary")._client()
        # prepend so SudsLibrary's plugin is left in place
        plugins = client.options.plugins
        if any(isinstance(x, _MyPlugin) for x in plugins):
            return
        plugins.insert(0, _MyPlugin())
        client.set_options(plugins=plugins)

有没有人有关于如何使用上述 sn-p 的完整机器人示例?我应该将什么作为上下文传递给编组?我需要在某个时候调用 attach_my_plugin() 吗?

【问题讨论】:

    标签: soap robotframework suds


    【解决方案1】:

    消息插件的一般描述可以在Suds documentation 中找到。更多细节在class documentation。你不叫 marshalled,suds 可以。为了更好地理解如何实现编组方法,请阅读Element 的文档。 suds 插件本质上是一个监听器。我的示例使用公共 Web 服务进行演示。

    假设您的请求如下所示:

    ...
    <ns0:Body>
        <ns1:GetStatistics>
          <ns1:X>
    ...
    

    但你需要它看起来像这样:

    ....
    <ns0:Body>
        <ns1:GetStatistics type="specialType">
          <ns1:X>
     ...
    

    这是一个将 type 属性添加到 GetStatistics 元素的插件。当元素发送了子元素和属性时,这可能是必需的。 Suds 0.4 不支持这一点,但它是有效的 SOAP。可能有一个分支确实支持这一点。

    *** Settings ***
    Library           SudsLibrary
    Library           c:/SudsLibraryExtensions.py
    
    *** Test Cases ***
    Message Plugin
        Create Soap Client    http://www.webservicex.net/Statistics.asmx?WSDL
        Attach My Plugin
        Set GetStats Type    specialType
        ${dbl array}=    Create Wsdl Object    ArrayOfDouble
        Append To List    ${dbl array.double}    2.0
        Append To List    ${dbl array.double}    3.0
        ${result}=    Call Soap Method    GetStatistics    ${dbl array}
        Should Be Equal As Numbers    ${result.Average}    2.5
    

    c:/SudsLibraryExtensions.py: 的内容:

    from robot.libraries.BuiltIn import BuiltIn
    from suds.plugin import MessagePlugin
    
    class _MyPlugin(MessagePlugin):
    
        def __init__(self):
            self._type = 'defaultType'
    
        def marshalled(self, context):
            body = context.envelope.getChild('Body')
            call = body.getChild('GetStatistics')
            call.set('type', self._type)
    
        def set_getstats_type(self, value):
            self._type = value
    
    class SudsLibraryExtensions(object):
        def attach_my_plugin(self):
            client = BuiltIn().get_library_instance("SudsLibrary")._client()
            plugins = client.options.plugins
            if any(isinstance(x, _MyPlugin) for x in plugins):
                return
            # prepend so SudsLibrary's plugin is left in place
            plugins.insert(0, _MyPlugin())
            client.set_options(plugins=plugins)
    
        def set_getstats_type(self, value):
            self._get_plugin().set_getstats_type(value)
    
        def _get_plugin(self):
            client = BuiltIn().get_library_instance("SudsLibrary")._client()
            plugins = client.options.plugins
            my_plugin = next((plugin for plugin in plugins if isinstance(plugin, _MyPlugin)), None)
            if my_plugin is None:
                raise RuntimeError("Plugin not found. Did you call Attach My Plugin?")
            return my_plugin
    

    只要插件使用关键字附加我的插件附加,类型属性将始终设置。有一个默认类型。要更改类型的值,请使用关键字 Set GetStats Type。任何类型集都将在未来的所有请求中使用,直到它被更改。这里使用两个类的唯一原因是防止“marshalled”成为暴露的关键字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多