Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import Robot Library with variable arguments

I have a Robot library implemented in python (myLibrary.py), which needs to be initialized with a variable number of arguments (e.g. fileNames):

class myLibrary:
    def __init__(self, *files):

And I have a Robot keyword file (myKeywords.txt), which is using that python library:

*** Settings ***
Library           myLibrary.py    ../folder1/fileA    ../fileX

*** Keywords ***
myKeyword
    pass

In the case, that I put the library arguments statically into the myKeywords.txt file, my library initializes properly.

But I want to make those arguments to myLibrary dynamic, so that I can include myKeywords.txt from different testsuites and initialize it with different arguments.

I am looking for a myKeywords.txt file, that looks like this:

*** Settings ***
Library           myLibrary.py    @{arguments}

And I want to define @{arguments} in my testsuite (as it differs from testsuite to testsuite) and is it when importing the keywords file:

*** Settings ***
Resource          configuration.txt     // defines @arguments
Resource          myKeywords.txt    @{arguments}

*** Test Cases ***
myTest
like image 589
Martin Perzl Avatar asked Jul 16 '26 16:07

Martin Perzl


1 Answers

You can't quite do exactly what you want, because robot syntax doesn't allow you to pass variables to a resource file. However, I don't think there's any reason to pass in @{arguments} -- myKeywords.txt should be able to use the variable directly.

This setup works for me:

configuration.txt:

*** Variables ***
| @{arguments} | file1 | file2

myLibrary.py:

class myLibrary:
    def __init__(self, *files):
        self.files = files

    def getFiles(self):
        return self.files

myKeywords.txt:

*** Settings ***
| # N.B. @{arguments} must be defined before importing this resource file
| Library | myLibrary.py | @{arguments}

*** Keywords ***
| Show files
| | ${files}= | myLibrary.getFiles
| | log to console | the files are ${files}

The test suite:

*** Settings ***
| Resource | configuration.txt
| Resource | myKeywords.txt 

*** Test Cases ***
| Display the list of configuration files
| | Show files
like image 75
Bryan Oakley Avatar answered Jul 18 '26 06:07

Bryan Oakley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!