Reputation: 911
I have a python API wrapper that I can use like so:
from api.MyApi import *
client = MyApi(server)
users = client.user.get_users()
I would like to write a Test Library that uses it which I can use within Robot Framework, but I'm having trouble getting it to work the way I want. I have tried the following ways:
test.robot
*** Settings ***
Library api.MyApi ${SERVER} WITH NAME client
*** Variables ***
*** Keywords ***
Get users
${response}= client.user.get_users()
Log ${response.content}
*** Test Cases ***
Test: Test 1
Get users
Which results in
No keyword with name 'client.user.get_users()' found.
how can I create and use an instance of my API client?
Upvotes: 2
Views: 6624
Reputation: 385910
You shouldn't try to directly use your api library in a robot test since it wasn't designed to work as a keyword library.
Instead, create your own keyword library that can call the api to do the work. Then, instead of creating a get keywords
keyword in your test, you do it in the library.
For example, create a file named "APIKeywords.py" which will establish a connection to your server. In it, create a keyword named get_users
which uses the connection to get the users:
from api.MyApi import *
class APIKeywords() :
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
def __init__(self, server):
self.server = server
self.client = MyAPI(self.server)
def get_users(self):
return self.client.user.get_users()
You can use this keyword library like any other library. For example:
*** Variable ***
${SERVER} localhost
*** Settings ***
Library APIKeywords.py ${SERVER} WITH NAME client
*** Test cases ***
Example
${users}= get users
If you want to explicitly use client
when calling the keyword, you can change that last line to:
${users}= client.get_users
or
${users}= client.get users
Upvotes: 5
Reputation: 2862
You can have keyword file and a Library file.
To have an library file you need to create a class and then call it within robot framework script, Then in your test library you should created methods that will act as Keywords in robot framework
example:
HelloWorld.py
class HelloWorld():
def Keyword_Robot(hello, world):
print(hello + " " + world)
Keyword.robot
*** Settings ***
Library HelloWorld.py
*** Test Cases ***
First custom Keyword
Keyword Robot "Hello" "World"
Output:
Hello World
Note
that this keyword is taking arguments which needs to be passed inside of robot framework just after the custom keyword.
Upvotes: 2