Wizard
Wizard

Reputation: 37

How to write FOR loop and IF statement programmatically with Robot Framework 4.0?

    bot.body.create_keyword('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
    bot.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
    bot.body.create_keyword('log', args=['${x}'])

an example which yielded undesired results. I was hoping that it would have run log 3 times but it just logs latest value of x which is c. 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. A while loop would have been good but I guess a for if would work.

how to do if statement like this example with create if ?

bot.body.create_keyword('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
for_kw = bot.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
for_kw.body.create_keyword(if list[x] == 'b':
            log list[x] (or do any other keyword)
                     elif list[x] == 'c':
            log list[x]
                     else:
            end for loop)

There is an extension to this to when we do not know the number of items in the list. how to code this with keywords -

run loop, pick up items from a file and add to a list, until no items to pick up

Upvotes: 0

Views: 241

Answers (1)

Bence Kaulics
Bence Kaulics

Reputation: 7271

You have to create the Log keyword in the body of the FOR keyword, currently you are creating it in the body of the test case. With the IF you have to call the create_if() without parameters, then on its returned object you could call create_branch(type='IF', condition='"${x}" == "b"') with type and condition. It will return an object of which body should be used to add keywords to be executed inside this IF branch.

from robot.api import TestSuite

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

test.body.create_keyword('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
for_kw = test.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
for_kw.body.create_keyword('log', args=['${x}'])

if_kw = for_kw.body.create_if()

if_branch = if_kw.body.create_branch(type='IF', condition='"${x}" == "b"')
if_branch.body.create_keyword('log', args=['BBBB'])

elif_branch = if_kw.body.create_branch(type='ELSE IF', condition='"${x}" == "a"')
elif_branch.body.create_keyword('log', args=['AAAA'])

else_branch = if_kw.body.create_branch(type='ELSE', condition='"${x}" == "c"')
else_branch.body.create_keyword('log', args=['CCCC'])

suite.run()

For a nested IF and FOR statements, just repeat the same. Get the body of the keyword and then call create_if, create_for, etc.

from robot.api import TestSuite

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

test.body.create_keyword('Create List', args=['a', 'b', 'c'], assign=['@{list}'])
for_kw = test.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
for_kw.body.create_keyword('log', args=['${x}'])

for_kw2 = for_kw.body.create_for(flavor='IN', variables=['${x}'], values=['@{list}'])
for_kw2.body.create_keyword('log', args=['${x}'])

suite.run()

Upvotes: 1

Related Questions