1
votes

Is it possible to do a conditional import of a resource file in robot framework? Depending on the test environment, I want to import a resource file with different variables. The variable could be read from the robot CLI (e.g. robot --variable VAR:production myTestSuite)

Illustrating Example:

*** Settings***
Resource    variables_url_environment_a.robot
Resource    variables_url_environment_b.robot

Run keyword if     '${VAR}'=='production'    Import resource    variables_url_environment_b.robot
4

4 Answers

1
votes

You could use Arguments file that will have different Environmental variables, You could use something like

QA.args

    --variable Enviroment:http://sample.url/QA:1111
    --variable USER:John
    --variable PASSWORD:John

Then in your Robot.test

*** Test Cases ***
Run Argument File
     Go To  ${Enviroment}
     Login With User   ${USER}  ${PASSWORD}

NOTE: This is just an example of the argument file use Login with User is not an actual keyword

And then execute command

robot --argumentfile "QA.args" tests

You can also overwrite the variables on the command line.

robot --argumentfile "QA.args" --variable Enviroment:http://sample.url/Staging:1111 tests
0
votes

You could use a variable in the name of import file. Set the value of the variable from pom.xml file in case you are using maven.

Something like below, where ${PLATFORM} is a variable :

*Settings*
Resource    ../platforms/settings_${PLATFORM}.tsv                       
Resource    ../platforms/settings_default.tsv   
*Variables*                         
${PLATFORM} ${ENV_PLATFORM} 

Below is snippet from POM.xml

....
<env.platform>Platform1.</env.platform>
....
<configuration>
    <variables>
        <param>ENV_PLATFORM:${env.platform}</param>
    </variables>
</configuration>
....

Also, this way you can pass the value of platform from jenkins (if used) using -Denv.platform=Platform_5

0
votes

I dont think conditional imort is possible in Robot Framework in the way you liked. However,what you can do is instead of importing envorimnent file as resoucres , you can pass them to your test as --variablefile

How i will do it?

variables_url_environment_a.py

msg='env a'

variables_url_environment_b.py

msg='env b'

Test.robot

*** Settings ***
*** Variables ***
*** Test Cases ***
print message to console
    print msg
*** Keywords ***
print msg
    log to console  ${msg}

Now just run your test suite as per the enviroment you need by creating a simple python script.

Python_run_script

import subprocess
var='Production'
command_a='pybot -V variables_url_environment_a.py Test.robot'
command_b='pybot -V variables_url_environment_a.py Test.robot'
if var='Production': 
    procId = subprocess.Popen(command_a,stdout = subprocess.PIPE)
else:
    procId = subprocess.Popen(command_b,stdout = subprocess.PIPE)

For more information about how to use --variablefile , you can also refer below url

https://automationlab0000.wordpress.com/2018/11/20/how-to-pass-python-variable-file-in-robotframework/

0
votes

Run Keyword If '${VAR}' == 'iOS' Import Library a.py