繁体   English   中英

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

[英]In robot framework how do you to create object of class and call the methods in corresponding class?

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

*** 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

这是您可以获得所需结果的方法。

让我们以具有 Sample 类的 demo.py 为例

示例类有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}

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

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

暂无
暂无

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

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