【问题标题】:How to pass objects to keywords in robot framework?如何将对象传递给机器人框架中的关键字?
【发布时间】:2016-10-26 19:38:49
【问题描述】:

我有一个python类MyClass写在文件MyClass.py中:

class MyClass(object):
    def __init__(self):
        self.myvar = list()

    def setvar(self, val):
        self.myvar = val

    def mymethod(self):
        return self.myvar

我在 Robot Framework 中导入如下:

Library         MyClass    WITH NAME    my_component

我还有一个关键字,它调用传递给它的对象的方法:

testing component
    [Arguments]       ${cmp}
    log to console    ************* ${cmp}
    ${result} =       ${cmp}.mymethod

我有多个从 MyClass 类实例化的对象,每个对象都有不同的属性。无论对象本身如何,我都想使用testing component 关键字获取它们的属性。

当我通过my_component 调用testing component 时:

Test Method Call
    testing component    my_component

我明白了:

No keyword with name '${cmp}.mymethod' found.

如果我在关键字testing component中这样称呼它:

${result} =    call method     ${cmp}   mymethod

我明白了:

Object 'my_component' does not have method 'mymethod'.

我还尝试了call method my_component mymethod 进行测试,我又得到了Object 'my_component' does not have method 'mymethod'.

但是当我使用${result} = my_component.mymethod 时,一切正常。

【问题讨论】:

  • MyClass.py 是否定义了一个名为 MyClass 的类?还是MyClass.py 是一个简单的有功能的模块?
  • @BryanOakley 请查看我对问题的编辑

标签: python object robotframework method-call


【解决方案1】:

关于如何调用 python 对象的方法的问题的字面答案是您可以使用扩展变量语法(例如:${cmp.mymethod()} 或者您可以使用call method 关键字(例如:call method ${cmp} mymethod)。

例如,robot 中的普通标量变量是 python 字符串对象。因此,我们可以在它们上调用方法,例如lower()upper()。以下测试用例展示了如何使用我之前提到的两种机制在字符串上调用这些方法:

*** Variables ***
${message}    Hello, world    # a python string object

*** Test cases ***
Example
    # Example using "Call method" keyword
    ${lower}=    call method    ${message}    lower
    should be equal    ${lower}    hello, world

    # Example using extended variable syntax
    ${upper}=    set variable    ${message.upper()}
    should be equal    ${upper}    HELLO, WORLD

您的代码的问题是您误解了my_component${cmp} 代表什么。它们是 not python 对象。相反,它是机器人框架库的名称。尽管在底层它可能作为对库中定义的类实例的引用存在,但在测试中my_component 只是库的名称

这就是 my_component.my_method 起作用的原因 - my_component 是库的名称,my_method 是该库中关键字的名称。这是标准的机器人框架语法。请参阅机器人框架用户指南中的Specifying a keyword explicitly

如果您希望能够像传递对象一样传递my_component,您可以使用run keyword 来运行在该库中实现的关键字。

例如,首先使用以下代码创建MyClass.py

class MyClass(object):
    def mymethod(self):
        return "Hello, world"

如果您将call method 替换为run keyword,您的代码就可以工作:

*** Keywords ***
testing component
    [Arguments]       ${cmp}
    log to console    ************* ${cmp}
    ${result} =       run keyword   ${cmp}.mymethod

最后,如果你真的想传递实际的库object,你可以使用内置关键字Get Library Instance 来获取实际的库对象,然后可以与@987654323 一起使用@

*** Keywords ***
testing component
    [Arguments]       ${cmp_name}
    ${cmp}=  Get library instance    ${cmp_name}
    log to console    ************* ${cmp}
    ${result} =       call method    ${cmp}    mymethod
    [return]          ${result}

奇怪的是,扩展变量语法在这种情况下不起作用。我不知道为什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-17
    • 2018-07-03
    • 2020-01-26
    • 1970-01-01
    • 2017-12-23
    • 2013-11-08
    • 1970-01-01
    • 2019-04-09
    相关资源
    最近更新 更多