在机器人框架中如何创建类的对象并调用相应类中的方法?

问题描述 投票:0回答:3

在机器人框架中如何创建类的对象并调用相应类中的方法?这是代码片段。

*** Settings ***
Documentation     A resource file with reusable keywords and variables.
...               Use keywords in this file in testcases directory.
Library           /home/kirti/src/Helper/utilities.py
Library           /home/kirti/src/Helper/config_parser.py
#Library          /home/kirti/qa/src/executor/cleanup.CleanUp
Library           /home/kirti/qa/src/executor/cleanup.py

*** Variables ***
${RESULT}         0

*** Keywords ***
Read Json Config Values
    Log To Console     "Setting up the config values globally"
    config_parser.Json Config Parser
    Import Variables    /home/kirti/src/Helper/variables.py
    Log Variables    INFO

Check Machines Reachability
utilities.Check All Machines Status

Check SNMP Counter
    utilities.Get Snmp    192.178.1.2    PPSessionCount

Call Clean Up
    #${cleanupobj}=     cleanup.create cleanup
    #${name}=     ${cleanupobj.cc()}
    Import Library     /home/kirti/src/executor/cleanup.py
    ${cmp}=    Get library instance    CleanUp
    Log To Console     ${cmp}.__class__.__name__
    #${name}=    Call method    ${cmp}    Create cleanup
    ${name}=    Call method    ${cmp}    cc
    #${name}=    Call method    ${cleanupobj}    env cleanup
    #Log To Console     "${name}"
    #Log Variables    INFO
    utilities.Check All Machines Status
python network-programming robotframework
3个回答
4
投票

这是一种可以达到预期结果的方法。

让我们以 demo.py 为例,其中包含 Sample 类

示例类有 init 、getting_path() 作为方法

class Sample(object):
    def __init__(self,path,device):
            self.device=device
            self.path = path

    def getting_path(self):
            return self.path 

让我们在Robotfile中使用这些方法

*** Settings ***
#in the Library section you reference python class in below format 
# (file.class_name) so file is demo.py and class is Sample 

Library      demo.Sample    ${path}    ${device}    WITH NAME    obj

#path and device are two arguments required by __init__,'obj' will be used to 
#access the methods in python class

Library    Collections

*** Variables ***
${path}    c:
${device}    samsung


*** Test Cases ***
Test
    Test_python_class

*** Keywords ***
Test_python_class

    #with obj you now call the method of python file 
    ${result} =    obj.getting_path

    #if method need any argument , this can be passed like
    #${result} =    obj.getting_path    ${arg1}    ${arg2}
    log to console    ${result}

1
投票

如果您想使用类的特定实例,您可以使用

${instance} =  obj  arg1
log to console  ${instance.function(args)}

0
投票

非常感谢。它解释得很好并且很有用。我还有一个后续问题。是否无法从 Robot Framework 调用私有方法?我在下面举个例子。

这是示例.py

class Sample(object):
    ROBOT_LIBRARY_SCOPE = 'SUITE'

    def __init__(self):
        self._var1 = 1
        self._var2 = 2

    def _get_var1(self):
        return self._var1

    def get_var2(self):
        return self._var2

这是示例.机器人

*** Settings ***
Library     sample.Sample       WITH NAME       obj1
Library     sample.Sample       WITH NAME       obj2
Library     Collections

*** Keywords ***
Test Python Objects
    ${obj1var1}=    obj1._get_var1
    ${obj1var2}=    obj1.get_var2
    ${obj2var1}=    obj2._get_var1
    ${obj2var2}=    obj2.get_var2

    log to console  ${obj1var1} ${obj1var2} ${obj2var1} ${obj2var2}

*** Test Cases ***
Test 101
    Test Python Objects

这里出现错误

"No keyword with name 'obj1._get_var1' found."

当我在sample.py和sample.robot中使用

get_var1
而不是
_get_var1
时,效果很好。

问题:

  • 这是否记录在机器人框架用户指南中?
  • 有没有一种方法(无需在python类中添加任何方法)来调用 机器人框架中的私有方法?
© www.soinside.com 2019 - 2024. All rights reserved.