1

I have been exploring Robot framework and came across this example which I am trying to use. This example works great except I wanted to try adding a for loop and if statement. I haven't even began the if statement yet as I am stuck with the for loop. Please may I have help to suggest how to construct a for loop and if statement.

This is a basic attempt of a for loop to add at the end of the script to test:

test.keywords.create('For', args=['1','IN','10'], type='for')
error - TypeError: __init__() got an unexpected keyword argument 'flavor'

The code below just shows i am trying to add basic for loop but compiles with above error

from robot.api import TestSuite

suite = TestSuite('Activate Skynet')
suite.imports.library('OperatingSystem')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
test.keywords.create('Set Environment Variable', args=['SKYNET', 'activated'], type='setup')
test.keywords.create('Environment V`enter code here`ariable Should Be Set', args=['SKYNET'])
test.keywords.create('For', args=['1','IN','10'], type='for')

origin - https://robot-framework.readthedocs.io/en/2.8.1/autodoc/robot.running.html

test.keywords.create('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
for_kw = ForLoop(['${l}'], ['@{list}'], flavor='IN')
for_kw.keywords.create('log', args=['${l}'])
test.keywords.create()
test.keywords.append(for_kw)
0

1 Answer 1

0

UPDATE: With Robot Framework this has changed and became easier to do.

Release note: Running and result models have been changed.

  • TestSuite, TestCase and Keyword objects used to have keywords attribute containing keywords used in them. This name is misleading now when they also have FOR and IF objects. With TestCase and Keyword the attribute was renamed to body and with TestSuite it was removed altogether. The keywords attribute still exists but it is read-only and deprecated.
  • The new body does not have create() method for creating keywords, like the old keywords had, but instead it has separate create_keyword(), create_for() and create_if() methods. This means that old usages like test.keywords.create() need to be changed to test.body.create_keyword().

For examples check out this other answer: How to write FOR loop and IF statement programmatically with Robot Framework 4.0?.


BEFORE Robot Framework 4.0:

IF statement

The if statement should be a Run Keyword If keyword with the arguments you need. It is a keyword like any other so you should list everything else in its args list.

  • The condition.
  • The keyword name for the True branch.
  • Separately any args to the keyword for the True branch if there is any. Listed separately.

  • The ELSE IF keyword if needed.
  • The ELSE IF condition.
  • The keyword name for the ELSE IF branch.
  • Separately any args to the keyword for the ELSE IF branch if there is any. Listed separately.

  • The ELSE keyword.
  • The keyword name for the ELSE branch.
  • Separately any args to the keyword for the ELSE branch if there is any. Listed separately.
from robot.api import TestSuite

suite = TestSuite('Activate Skynet')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])

test.keywords.create('Run Keyword If', args=[True, 'Log To Console', 'Condition was TRUE', 'ELSE', 'Log To Console', 'Condition was FALSE'])
test.keywords.create('Run Keyword If', args=[False, 'Log To Console', 'Condition was TRUE', 'ELSE', 'Log To Console', 'Condition was FALSE'])

suite.run()

This is how it looks like in the logs:

enter image description here


FOR loop

As for the for loop. It is a special keyword implemented by robot.running.model.ForLoop class that bases on the robot.running.model.Keyword class. Here is the constructor:

enter image description here

It has a flavor parameter, which is the loop type to be say. So it is IN, IN RANGE, IN ZIP, etc.

Now you instantiating a robot.running.model.Keyword which despite you could set its type to for, it won't have flavor attribute. So when you execute your code it will throw the error you see. It is because the ForRunner will try to access the flavor attribute.

  File "/usr/local/lib/python3.7/site-packages/robot/running/steprunner.py", line 52, in run_step
    runner = ForRunner(context, self._templated, step.flavor)
AttributeError: 'Keyword' object has no attribute 'flavor'

So you have to use the ForLoop class. Also I am using Robot Framework 3.1.2 so the errors might be different in my case but the approach should be the same.

Here is how it should look like:

from robot.running.model import ForLoop

for_kw = ForLoop(['${i}'], ['10'], flavor='IN RANGE')
test.keywords.append(for_kw)

No this will still fail with error:

FOR loop contains no keywords.

So you have to populate it like:

for_kw.keywords.create('No Operation')

The complete example:

from robot.api import TestSuite
from robot.running.model import ForLoop

suite = TestSuite('Activate Skynet')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
test.keywords.create('Log Many', args=['SKYNET', 'activated'], type='setup')
test.keywords.create('Log', args=['SKYNET'])

for_kw = ForLoop(['${i}'], ['10'], flavor='IN RANGE')
for_kw.keywords.create('No Operation')
test.keywords.append(for_kw)
suite.run()

If you run this it will only produce an output XML, you have to run rebot manually to have a log and report HTML file.

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you Bence, this is much appreciated... it works perfectly. i will attempt the if then else statement except i am aware of the run keyword if and i assume i will have to go the route you pointed out by using the constructor for the if statement as run.keyword.create can only accept 1 args parameter and not sure how to cator for the else. i have also updated my post with code to iterate through a list
Thank you @ Bence Kaulics, i have tested and answer works like a charm and i have upvoted and accepted. appreciated
@Wizard Robot Framework 4.0 is released and much of what is described in my answer have changed. I have updated it.
thank you for update. i did get deprecation error and .body resolved that and am currently exploring the for and if. wish they included a while but i am trying to make some complex examples with these like a nested if, and a for loop that reads x variables in a list until all of x is captured and validate each x with if statement against a condition. i will share once i figure out. Pls share if you already have examples. thank you
also looking through libraries to find if any improvements to get variable from python script into testsuite and variable value like the list out of testsuite and into python
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.