Friday, January 28, 2011

Functional Robot

Robot has some interesting keywords that allow users to make keywords that use function delegation or are higher order functions. Here are some examples that I think could be useful.

Keyword that opens and closes a resource

Basic idea is to have keyword that does something (opens a resource) before executing a delegated keyword and does something (closes a resource) after it has executed the delegated keyword.

Here is an example keyword that executes the argument keyword without producing log output.

With No Logging
[Arguments] @{keyword with args}
${original log level}= Set Log Level NONE
${result}= Run Keyword @{keyword with args}
Set Log Level ${original log level}
[Return] ${result}

Example use:

With No Logging Generated Huge Report Is Valid

Keyword for consuming unknown number of items

Consume All
[Arguments] ${producer function} ${consumer function}
${product}= Run Keyword ${producer function}
Run Keyword If ${product} ${consumer function} ${product}
Run Keyword If ${product} Consume All ${producer function} ${consumer function}

Consume All keyword takes a function that produces items and another function that consumes those items. Item could be anything for example a web page url or a custom xml message from a server. Consume All keyword stops when producer returns an empty value.
This keyword is useful in situations where there isn't total control of the number of produced items or when that number isn't really interesting from the testing point of view or it takes too much time to first find the number of items and then go through all of them. For cases where the number of items or the list of items is easy to get robotframework has BuiltIn.Repeat Keyword and :FOR.

Example uses:

All Server Messages Have Valid Hash
Consume All Server.Pull Message Validate Message Hash

All Pages Contain Company Logo
Consume All PageCrawler.Next Page Page Contains Company Logo

No comments: