Skip to content

Commit

Permalink
Merge pull request #44 from Altran-PT-GDC/response_header
Browse files Browse the repository at this point in the history
New keyword: Get Last Response Object
  • Loading branch information
samuelpcabral authored Oct 12, 2022
2 parents eb85762 + 9f88123 commit 29de58a
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Doc/SoapLibrary.html

Large diffs are not rendered by default.

69 changes: 57 additions & 12 deletions Doc/SoapLibrary.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<keywordspec name="SoapLibrary" type="LIBRARY" format="ROBOT" scope="SUITE" generated="2022-10-07T15:38:18Z" specversion="4" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\__init__.py" lineno="4">
<version>1.1</version>
<keywordspec name="SoapLibrary" type="LIBRARY" format="ROBOT" scope="SUITE" generated="2022-10-12T14:35:47Z" specversion="4" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\__init__.py" lineno="4">
<version>1.3</version>
<doc>SoapLibrary is a library for testing SOAP-based web services.

SoapLibrary is based on [https://python-zeep.readthedocs.io/en/master/|Zeep], a modern SOAP client for Python.
Expand Down Expand Up @@ -31,7 +31,7 @@ make a request through an XML file, and receive the response in another XML file
<inits>
</inits>
<keywords>
<kw name="Call SOAP Method" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="234">
<kw name="Call SOAP Method" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="233">
<arguments repr="name, *args, status=None">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="name">
<name>name</name>
Expand Down Expand Up @@ -59,12 +59,16 @@ this behavior can be modified using the argument status=anything
| args | List of request entries |
| status | if set as anything, return the error as a string |

Note, this keyword uses the most basic method of sending a request, through zeep that creates the xml based
on the wsdl definition. So this keyword does not store the response object, and therefore it is not possible
to use the keyword `Get Last Response Object` after this one.

*Example:*
| ${response}= | Call SOAP Method | operation_name | arg1 | arg2 |
| ${response}= | Call SOAP Method | operation_name | arg1 | arg2 | status=anything |</doc>
<shortdoc>If the webservice have simple SOAP operation/method with few arguments, you can call the method with the given `name` and `args`.</shortdoc>
</kw>
<kw name="Call SOAP Method With String XML" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="284">
<kw name="Call SOAP Method With String XML" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="287">
<arguments repr="string_xml, headers={'Content-Type': 'text/xml; charset=utf-8'}, status=None">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="string_xml">
<name>string_xml</name>
Expand Down Expand Up @@ -126,7 +130,7 @@ this behavior can be modified using the argument status=anything
| ${response}= | Call SOAP Method With XML | ${CURDIR}${/}Request_status_500.xml | status=anything |</doc>
<shortdoc>Send an XML file as a request to the SOAP client. The path to the Request XML file is required as argument, the SOAP method is inside the XML file.</shortdoc>
</kw>
<kw name="Convert XML Response to Dictionary" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="201">
<kw name="Convert XML Response to Dictionary" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="200">
<arguments repr="xml_etree">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="xml_etree">
<name>xml_etree</name>
Expand All @@ -143,7 +147,7 @@ this behavior can be modified using the argument status=anything
| ${dict_response}= | Convert XML Response to Dictionary | ${response} |</doc>
<shortdoc>Convert the webservice response into a dictionary.</shortdoc>
</kw>
<kw name="Create SOAP Client" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="33">
<kw name="Create SOAP Client" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="34">
<arguments repr="url, ssl_verify=True, client_cert=None, auth=None, use_binding_address=False">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="url">
<name>url</name>
Expand Down Expand Up @@ -182,8 +186,7 @@ For HTTP Basic Authentication, you can pass the list with username and password
to the ``auth`` parameter.

If you want to use the binding address in the requests, you need to pass use_binding_address=True
in the argment, note that this will only affect the keywords `Call SOAP Method With XML`
and `Call SOAP Method With String XML`
in the argument.

*Example:*
| Create SOAP Client | http://endpoint.com?wsdl |
Expand All @@ -194,7 +197,7 @@ and `Call SOAP Method With String XML`
| Create SOAP Client | https://endpoint.com?wsdl | auth=${auth} |</doc>
<shortdoc>Loads a WSDL from the given ``url`` and creates a Zeep client. List all Available operations/methods with INFO log level.</shortdoc>
</kw>
<kw name="Decode Base64" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="266">
<kw name="Decode Base64" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="269">
<arguments repr="response">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="response">
<name>response</name>
Expand All @@ -213,7 +216,7 @@ Returns the decoded response
| ${response_decoded}= | Decode Base64 | ${response} |</doc>
<shortdoc>Decodes texts that are base64 encoded.</shortdoc>
</kw>
<kw name="Edit XML Request" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="136">
<kw name="Edit XML Request" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="135">
<arguments repr="xml_file_path, new_values_dict, edited_request_name, repeated_tags=All">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="xml_file_path">
<name>xml_file_path</name>
Expand Down Expand Up @@ -252,7 +255,7 @@ Returns the file path of the new Request file.
| ${xml_edited}= | Edit XML Request | request_filepath | ${dict} | New_Request | repeated_tags=0 |</doc>
<shortdoc>Changes a field on the given XML to a new given value, the values must be in a dictionary. xml_filepath must be a "template" of the request to the webservice. new_values_dict must be a dictionary with the keys and values to change. request_name will be the name of the new XMl file generated with the changed request.</shortdoc>
</kw>
<kw name="Get Data From XML By Tag" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="105">
<kw name="Get Data From XML By Tag" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="104">
<arguments repr="xml, tag, index=1">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="xml">
<name>xml</name>
Expand Down Expand Up @@ -282,7 +285,49 @@ Returns the string representation of the value.
| ${value}= | Get Data From XML By Tag | ${response} | SomeTag | index=9 |</doc>
<shortdoc>Gets data from XML using a given tag. If the tag returns zero or more than one result, it will show a warning. The xml argument must be an etree object, can be used with the return of the keyword `Call SOAP Method With XML`.</shortdoc>
</kw>
<kw name="Save XML To File" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="181">
<kw name="Get Last Response Object" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="313">
<arguments repr="">
</arguments>
<doc>Gets the response object from the last request made. With the object in a variable, you can use the
dot operator to get all the attributes of the response.

Response object attributes:
| apparent_encoding |
| close |
| connection |
| content |
| cookies |
| elapsed |
| encoding |
| headers |
| history |
| is_permanent_redirect |
| is_redirect |
| iter_content |
| iter_lines |
| json |
| links |
| next |
| ok |
| raise_for_status |
| raw |
| reason |
| request |
| status_code |
| text |
| url |

Note, this keyword only works after the execution of `Call SOAP Method With XML`
or `Call SOAP Method With String XML`

*Example:*
| ${response}= | Call SOAP Method With XML | ${CURDIR}${/}Request.xml |
| ${response_object}= | Get Last Response Object |
| ${response_header}= | Set Variable | ${response_object.headers} |
| ${response_status}= | Set Variable | ${response_object.status_code} |</doc>
<shortdoc>Gets the response object from the last request made. With the object in a variable, you can use the dot operator to get all the attributes of the response.</shortdoc>
</kw>
<kw name="Save XML To File" source="C:\Repos\Robot-Framework-SOAP-Library\SoapLibrary\SoapLibrary.py" lineno="180">
<arguments repr="etree_xml, save_folder, file_name">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="etree_xml">
<name>etree_xml</name>
Expand Down
67 changes: 61 additions & 6 deletions SoapLibrary/SoapLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SoapLibrary:
def __init__(self):
self.client = None
self.url = None
self.response_obj = None

@keyword("Create SOAP Client")
def create_soap_client(self, url, ssl_verify=True, client_cert=None, auth=None, use_binding_address=False):
Expand All @@ -49,8 +50,7 @@ def create_soap_client(self, url, ssl_verify=True, client_cert=None, auth=None,
to the ``auth`` parameter.
If you want to use the binding address in the requests, you need to pass use_binding_address=True
in the argument, note that this will only affect the keywords `Call SOAP Method With XML`
and `Call SOAP Method With String XML`
in the argument.
*Example:*
| Create SOAP Client | http://endpoint.com?wsdl |
Expand Down Expand Up @@ -92,11 +92,10 @@ def call_soap_method_xml(self, xml, headers=DEFAULT_HEADERS, status=None):
| ${response}= | Call SOAP Method With XML | ${CURDIR}${/}Request.xml |
| ${response}= | Call SOAP Method With XML | ${CURDIR}${/}Request_status_500.xml | status=anything |
"""
# TODO check with different headers: 'SOAPAction': self.url + '/%s' % method}
raw_text_xml = self._convert_xml_to_raw_text(xml)
xml_obj = etree.fromstring(raw_text_xml)
response = self.client.transport.post_xml(address=self.url, envelope=xml_obj, headers=headers)
logger.info('Status code: %s' % response.status_code)
self._save_response_object(response)
etree_response = self._parse_from_unicode(response.text)
self._check_and_print_response(response, etree_response, status)
return etree_response
Expand Down Expand Up @@ -248,6 +247,10 @@ def call_soap_method(self, name, *args, status=None):
| args | List of request entries |
| status | if set as anything, return the error as a string |
Note, this keyword uses the most basic method of sending a request, through zeep that creates the xml based
on the wsdl definition. So this keyword does not store the response object, and therefore it is not possible
to use the keyword `Get Last Response Object` after this one.
*Example:*
| ${response}= | Call SOAP Method | operation_name | arg1 | arg2 |
| ${response}= | Call SOAP Method | operation_name | arg1 | arg2 | status=anything |
Expand Down Expand Up @@ -299,14 +302,56 @@ def call_soap_method_string_xml(self, string_xml, headers=DEFAULT_HEADERS, statu
| ${response}= | Call SOAP Method With String XML | "<sample><Id>1</Id></sample>" |
| ${response}= | Call SOAP Method With String XML | "<sample><Id>error</Id></sample>" | status=anything |
"""
# TODO check with different headers: 'SOAPAction': self.url + '/%s' % method}
xml_obj = etree.fromstring(string_xml)
response = self.client.transport.post_xml(address=self.url, envelope=xml_obj, headers=headers)
logger.info('Status code: %s' % response.status_code)
self._save_response_object(response)
etree_response = self._parse_from_unicode(response.text)
self._check_and_print_response(response, etree_response, status)
return etree_response

@keyword("Get Last Response Object")
def get_last_response_object(self):
"""
Gets the response object from the last request made. With the object in a variable, you can use the
dot operator to get all the attributes of the response.
Response object attributes:
| apparent_encoding |
| close |
| connection |
| content |
| cookies |
| elapsed |
| encoding |
| headers |
| history |
| is_permanent_redirect |
| is_redirect |
| iter_content |
| iter_lines |
| json |
| links |
| next |
| ok |
| raise_for_status |
| raw |
| reason |
| request |
| status_code |
| text |
| url |
Note, this keyword only works after the execution of `Call SOAP Method With XML`
or `Call SOAP Method With String XML`
*Example:*
| ${response}= | Call SOAP Method With XML | ${CURDIR}${/}Request.xml |
| ${response_object}= | Get Last Response Object |
| ${response_header}= | Set Variable | ${response_object.headers} |
| ${response_status}= | Set Variable | ${response_object.status_code} |
"""
return self.response_obj

@staticmethod
def _convert_xml_to_raw_text(xml_file_path):
"""
Expand Down Expand Up @@ -363,6 +408,16 @@ def _check_and_print_response(self, response, etree_response, status):
raise AssertionError('Request Error! Status Code: %s! Reason: %s' % (response.status_code, response.reason))
self._print_request_info(etree_response)

def _save_response_object(self, response):
"""
Log the response status code in Robot Framework log and save the response object
for use in the keyword 'Get Last Response Object'
:param response, zeep response object
"""
logger.info('Status code: %s' % response.status_code)
self.response_obj = response

@staticmethod
def _convert_string_to_xml(xml_string):
"""
Expand Down
2 changes: 1 addition & 1 deletion SoapLibrary/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '1.2'
VERSION = '1.3'
7 changes: 7 additions & 0 deletions Tests/keyword_tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ Test read utf8
${City} Get Data From XML By Tag ${response} City
should be equal as strings Fundão ${City}

Test Get Last Response Object
[Tags] ip2geo
Create Soap Client ${wsdl_ip_geo}
Call SOAP Method With XML ${requests_dir}${/}request_ip.xml
${response_object} Get Last Response Object
Dictionary Should Contain Key ${response_object.headers} Content-Type

Test Save File Response
[Tags] ip2geo
Remove File ${CURDIR}${/}response_test.xml
Expand Down
Binary file removed dist/robotframework-soaplibrary-0.6.tar.gz
Binary file not shown.
Binary file removed dist/robotframework-soaplibrary-0.7.tar.gz
Binary file not shown.
Binary file removed dist/robotframework-soaplibrary-1.1.tar.gz
Binary file not shown.
Binary file added dist/robotframework-soaplibrary-1.3.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion robotframework_soaplibrary.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: robotframework-soaplibrary
Version: 1.2
Version: 1.3
Summary: SOAP Library for Robot Framework
Home-page: https://github.com/Altran-PT-GDC/Robot-Framework-SOAP-Library
Author: Altran Portugal
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
exec(code)

setup(name = 'robotframework-soaplibrary',
version = '1.2',
version = '1.3',
description = 'SOAP Library for Robot Framework',
long_description = 'Test library for Robot Framework to create automated test like using SOAPUI',
author = 'Altran Portugal',
Expand Down

1 comment on commit 29de58a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass %
21 0 0 21 100

Passed Tests

Name ⏱️ Duration Suite
Test Call Soap Method 0.26 s Keyword Tests
Test Call Soap Method Error 0.163 s Keyword Tests
Test read 0.267 s Keyword Tests
Test read With Binding Address 0.319 s Keyword Tests
Test read string xml 0.248 s Keyword Tests
Test Edit and Read 0.187 s Keyword Tests
Test Call SOAP Method with XML Anything 0.31 s Keyword Tests
Test Call SOAP Method with String XML Anything 0.093 s Keyword Tests
Test read utf8 0.232 s Keyword Tests
Test Get Last Response Object 0.05 s Keyword Tests
Test Save File Response 0.11 s Keyword Tests
Test Read tags with index 2.6189999999999998 s Keyword Tests
Test Response to Dict 0.963 s Keyword Tests
Test Edit XML Request 1 0.014 s Keyword Tests
Test Edit XML Request 2 0.011 s Keyword Tests
Test Edit XML Request 3 0.011 s Keyword Tests
Test Edit XML Request 4 0.01 s Keyword Tests
Test Edit XML Request 5 0.012 s Keyword Tests
Test Edit XML Request 6 0.012 s Keyword Tests
Test Edit XML Request 7 0.01 s Keyword Tests
Test Edit XML Request 8 0.011 s Keyword Tests

Please sign in to comment.