简体   繁体   English

我怎样才能从机器人框架中的python类中获取self对象

[英]How could I get self object from python class in robotframework

Engine.py will import several classes as self object Engine.py将导入几个类作为自身对象

Engine.py

from api import api
from cloud import cloud
class Engine(object):
    def __init__(self, env):
        session = dict()
        self.api = api.API(session)
        self.cloud= cloud.CLOUD(session)

api.py

class API(object):
    def __init__(self, session):
        self.session = session

    def api_keyword(self):
        return SOMETHING

My question is : 我的问题是:

How can I use the keyword under api.py and cloud.py and ONLY import Engine.py into robot file 如何在api.py和cloud.py下使用关键字,并且只将Engine.py导入机器人文件

test.robot

*** Settings ***
Library         Engine.py  ${env}


*** Test Cases ***
python class test
    [Tags]    class
    Engine.api.api_keyword

And I got error message: 我收到错误消息:

No keyword with name 'Engine.api.api_keyword' found. 找不到名为“Engine.api.api_keyword”的关键字。

Robot Framework maps only class methods to keywords; Robot Framework只将类方法映射到关键字; your class Engine does not expose any methods from api and cloud - it probably uses them internally, but doesn't define any as its own. 你的类Engine没有暴露来自apicloud任何方法 - 它可能在内部使用它们,但是没有将它们定义为它自己的方法。
So here's your first solution - create wrapper methods for all you need in the cases: 所以这是您的第一个解决方案 - 为案例中所需的所有内容创建包装器方法:

def an_api_method(self):
    self.api.something()

And now you'll have the An API Method keyword at your disposal in the cases. 现在,您可以在案例中使用An API Method关键字。


Solution two - make your class inherit the other two: 解决方案二 - 让你的类继承另外两个:

class Engine(api, cloud):

, and your cases will have access to all their public methods. ,您的案件将可以访问他们所有的公共方法。
This one is more involving - you'll have to call their constructors (with super() ), and if you maintain a state in your class, you'll have to accommodate for that. 这其中更涉及到-你必须调用它们的构造函数(与super()如果你在你的类维持的状态,你必须适应这一点。 Ie more drastic code changes are needed. 即需要更加激烈的代码更改。


The third solution doesn't require any changes to the Enhine code - but, disclaimer: I don't know will it work :) (I'm not at a computer). 第三种解决方案不需要对Enhine代码进行任何更改 - 但是, 免责声明:我不知道它是否可行:)(我不在电脑上)。
It consists of two calls - first to use Get Library Instance to get the object of your imported library (from the Builtin library), and then - Call Method : 它包含两个调用 - 首先使用Get Library Instance获取导入库的对象(来自Builtin库),然后Call Method

${ref}=     Get Library Instance    Engine
Call Method     $ref.api    api_keyword

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM