Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to variable of a python class from Robot Framework

I have a python file for example Animals.py, inside I defined 3 different classes like that:

#Animals.py
class Animal:
    listAnimal=["dog","cat"] #<------------
    def __init__(self):
        #Animal constructor
        pass

class Dog(Animal):
    def __ini__(self):
        #dog constructor
        pass
class Cat(Animal):
    def __ini__(self):
        #cat constructor
        pass

I would like to get access to the class variable listAnimal

I am not an expert of RF I tried to use that:

Import Library   Animal
${ListAnimal}   Animal.listAnimal

could you give some suggestion on that please?

like image 701
C.med Avatar asked Dec 18 '22 19:12

C.med


2 Answers

You can use the Get Library Instance keyword from the BuiltIn library to get the currently active instance of the specified test library. Then you can use the Extended variable syntax to access the list and its elements.

Example:

${lib}=     Get Library Instance    Animal
Log     ${lib.listAnimal[0]}    # should log 'dog'
Log     ${lib.listAnimal[1]}    # should log 'cat'
${ListAnimal}=    Set Variable   ${lib.listAnimal}
like image 144
Bence Kaulics Avatar answered Dec 28 '22 05:12

Bence Kaulics


I added a new function to the file Animals.py

def getListAnimals():
    return Animal.listAnimal

And I am now only using this function in the robot framework:

Import Library   Animals.py
${ListAnimal}   Get List Animals
like image 37
C.med Avatar answered Dec 28 '22 06:12

C.med