簡體   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