简体   繁体   English

无法使用机器人框架中定义的关键字调用类方法

[英]Unable to call class methods with keyword defined in robot framework

Hi I am trying to create my own keywords and call it from robot scripts : 嗨,我正在尝试创建自己的关键字,并从机器人脚本中调用它:

Below is my sample code : 以下是我的示例代码:

util.py util.py

from robot.api.deco import keyword

@keyword('Add Num')
def add(a,b):
    print "Simple method"
    return int(a)+int(b)
class Geo :
    @keyword('Class Add Num')
    def addd(self,a,b):
        print "Inside class method "
        return int(a)+int(b)

Sample.robot Sample.robot

*** Settings ***
Library   util.py

*** Test Cases ***
TC_01
    [Documentation]  sample test
    [Tags]  Sample
    Add Num  10  20
    Class Add Num  10  2

Which I am running using pybot command - pybot sample.robot 我正在使用pybot命令运行-pybot sample.robot

Add Num keyword is works fine , but Class Add Num gives an error : Add Num关键字可以正常工作,但是Class Add Num给出错误:

No keyword with name 'Class Add Num' found.

I have tried looking into stackoverflow and official robot framework docs , but could find any relevant help . 我曾尝试研究stackoverflow和官方机器人框架文档,但可以找到任何相关帮助。

How to resolve this error , Or I need to implement it in a different way ?? 如何解决此错误,或者我需要以其他方式实现它?

Robot will not automatically instantiate in classes in your libraries, except in one special case which is when the class name is the same as the base part of the filename. 除了一种特殊情况(即类名与文件名的基本部分相同)外,Robot不会自动在库中的类中实例化。

The proper way to write a library is to either use a single class with the same name as a filename, or functions, but not both. 写一个库的正确方法是要么使用一个类具有相同的名称作为文件名, 功能,但不能同时使用。 If you want to use both, it is up to you to create instances of your class and expose the keywords. 如果要同时使用两者,则由您决定创建类的实例并公开关键字。

The third way is to use the dynamic library api in which you create special functions to get the list of available keywords ( get_keyword_names ), and to execute a keyword ( run_keyword ). 第三种方法是使用动态库api,在其中创建特殊功能以获取可用关键字的列表( get_keyword_names ),并执行关键字( run_keyword )。 This is all documented in the user guide. 所有这些都记录在用户指南中。

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

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