I have shown you how to authenticate to Microsoft Graph from Python. In this post, I will show you how to use the Python methods as Robot Framework keywords. We will implement a few new methods, go through the basics of new keywords implementation, and finally, we will use them in Robot Framework test. Let’s get to work!
Implementing new methods
At Valo, I work with a solution that creates and manages teams, groups, and sites. Our tests cover this process and create a bunch of groups during every execution. To keep a tenant in a good shape we have to clean up those artifacts. Graph API seems to be the best solution in this case. That’s why I’ve created two useful methods. The first one returns the group ID. The second method deletes a group with the specified ID.
def get_group_id(self, groupName):
group = self._graph_get_call("/groups?$filter=displayName eq '{0}'".format(groupName))
if group and group['value'] and group['value'][0]['id']:
return group['value'][0]['id']
raise RuntimeError('*ERROR:{0}* Group not found'.format(time.time()*1000))
def delete_group_by_id(self, group_id):
return self._graph_delete_call('/groups/{0}'.format(group_id))
def delete_group_by_name(self, group_name):
group_id = self.get_group_id(group_name)
self.delete_group_by_id(group_id)
def _graph_delete_call(self, url):
request_url = self.BASE_URL_V1.format(url)
response = requests.delete(url=request_url, headers=self._get_default_headers())
return response
You can find methods that are not described in my previous post or on my GitHub repository. The code is pretty simple. There are two things worth pointing out:
- DELETE method doesn’t return JSON body,
- I’ve created a method delete_group_by_name, which gets a group ID and deletes group. It makes it easier to use.
Python class in Robot tests
You can use a Python class in a Robot test. Import it like a library and specify a file path. Use the same name for the file and the class.
- If your class has a constructor, you have to specify its parameters while importing,
- Methods starting with an underscore are treated as private class methods. Robot Framework won’t recognize them as keywords,
- You can use every other method as a keyword. Underscores are replaced with spaces.
- There is a specific format for logs. Robot Framework uses it to generate a nice HTML execution log.
# Available log levels TRACE, DEBUG, INFO, WARN, ERROR and HTML
print(f'*{logLevel}:{time.time()*1000}* {message}')
print(f'*TRACE:{time.time()*1000}* Example log')
Here is an example of the test case:
*** Settings ***
Library ../../Python/Graph.py ${GRAPH_TENANT_ID} ${GRAPH_APP_CLIENT_ID} ${GRAPH_APP_CLIENT_SECRET}
*** Test Cases ***
Delete Office 365 group
Delete Group By Name FooBar
That’s it!
We’ve implemented a few methods in Python and made them available as keywords. Now, you know how to extend your tests with Python. Check my next post and learn how to make your methods available in the Visual Studio Code IntelliSense.If you want to learn more about extending Robot Framework, check the official Robot Framework User Guide.
I’m SharePoint enthusiast working currently at Avenga. I like to test new technologies, unconventional solutions and share my ideas. I’m dealing with the online version of SharePoint, Azure, and some other Office 365 applications but I have experience with on-premises as well.