简体   繁体   中英

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

In robot framework how do you to create object of class and call the methods in corresponding class? This is the code snippet.

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

Here is a way you can achieve the desired result.

Lets take example of demo.py which have class Sample

Sample class has init ,getting_path() as methods

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

    def getting_path(self):
            return self.path 

Lets use these methods in 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}

If you want to use a specific instance of a class you can use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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