Passing functions as arguments to keywords

Hello Guys,

Most likely this is a stupid question, but I’m new to RF and this struggles me for the last couple of days…

I’m trying to migrate my current PyTest Framework to Robot Framework, and what I try to achieve is to convert one of my “wait_for_condition” functions into RF keyword.

The function “wait_for_condition” looks something like this:
def wait_for_condition(asserted_func, user, execute_func, **kwargs):
…retry = kwargs.get(‘retry’, 3)

… and it basically asserts is “asserted_func” equal to True for given “user”, if no, it retries given amount of times until “asserted_func” is True or retries expire. If/When “asserted_func” is True, it executes the passed “execute_func”.

If I try to import the library and use it in Robot, it should look like this:
Wait For Condition ${asserted_func} ${user} ${execute_func} ${retry}

But I got an error “[FAIL] TypeError: ‘str’ object is not callable”, so I’m doing something wrong.

Could you help me implement this custom wait function? And how to pass the keyword arguments **kwargs to the keyword?

Thank you,
F

Hi Ivan,

Have a look at your log.html file and check what the is in front of the Wait For Condition keyword (<something>.Wait For Condition)?

In the screnn shot below for the keyword Log, you’ll see it’s BuiltIn.Log
image

If the <something> is not your python class there’s a good chance robot framework has picked up a conflicting keyword with the same name from another library, perhaps Wait For Condition?

In that case you’ll probably want to refer to Resolving conflicts

Dave.

Thanks for the quick reply, Dave! It seems that Wait For Conditions uses the correct class - I don’t have Builtin. keyword - only my own.

However, do you am I importing and using my custom Wait For Conditions correctly?

BTW Wait For Condition/Response/Request look pretty good. It seems they require Browser lib which I installed but can’t import for some reason.

Hi Ivan,

Based on what you’ve said it sounds like you have, possibly one of your arguments (${asserted_func}, ${user}, ${execute_func}, or ${retry}) is somehow getting converted to a string, but without seeing how you declared them hard to say which one.

The will only work with Browser, so depending on if you app is a web app or not will determine if they might be useful. Also note you can’t just mix and match keywords, i.e. if you open a web browser with Selenium and then try to use a Browser Library keyword on that Selenium browser session it won’t work, rather you’d need to find the equivalent SeleniumLibrary keyword.

BTW to import Browser Library you just need the line

Library    Browser

Without knowing what your code does that’s as much as I can tell you.

Dave.

1 Like

I really appreciate your help Dave!

Is there a place a can see examples of how Python functions using other functions as arguments, are converted and used as RF keywords?
I looked into the documentation for keywords, arguments and variables but couldn’t find such information.

Hi Ivan,

Well you’re welcome to use robotframework-perfmon as an example, It’s a small library I created

It’s a pretty small library with only 4 keywords and about 100 lines of code.

Dave.

1 Like