Martin Perzl
Martin Perzl

Reputation: 65

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

Upvotes: 3

Views: 7099

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

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

Upvotes: 6

Related Questions