Advanced Topics
This tutorial section covers some of the advanced features of GUARDARA that you can use to customize or improve your web service testing capability further.
Test Scope Customisation
Suppose you wanted to test the createUser
operation but you wanted to focus only on testing the id
, username
and email
fields. You can tell GUARDARA to skip testing the password
field by editing the Message Template named "createUser" using the Message Template Designer.
Follow the instructions below to make the necessary change.
- Go to the Test Assets page.
- Expand the "createUser" project.
- Click the Edit button of the "createUser" message template to open the Message Template Designer.
- Under the Message Layout tab at the left side of the Message Designer, expand the "body" group. As you click on the group to expand, the properties of the group become visible on the right side of the screen. Close the Property Editor Panel of the group by scrolling down to the bottom of the screen and clicking the Cancel button.
- Click on the "property_password" group in the Message Layout to bring up the properties of the group.
- In the Editor Panel of the group change the value of the "Test Field" property to "No".
- Scroll to the bottom of the screen and click on the "UPDATE" button below the Editor Panel.
- Save the message template by clicking on the File menu in the Editor Menu and selecting the "Save" option.
Changing Default Values
Suppose you wanted to test the createUser
operation but you wanted to change the default value of the username and email address. You can customize default values on a per-target basis by editing the Project on the Test Assets page.
Follow the instructions below to implement the desired changes.
- Go to the Test Assets page.
- Open the "createUser" project by clicking the Edit button of the project.
- Navigate to the TEST TARGETS tab of the poject.
- Select the test target using the dropdown menu in the top-right corner of the screen.
- Expand the General Variables section of the target configuration screen.
- Update the value of the username and email fields.
- Click the Update button at the bottom of the screen to save the changes.
The screenshot below shows the expanded General Variables section.
Database Instrumentation
You may have noticed that testing the createUser operation of the demo web service resulted in the creation of thousands of users in the database.
mysql> SELECT COUNT(*) FROM users;
+----------+
| COUNT(*) |
+----------+
| 2606 |
+----------+
1 row in set (0.02 sec)
You can create a Callback to delete all users from the database and add the Callback to the end of the Test Flow Template of the Project right before the Disconnect action. This way, GUARDARA will clear out the users
table after each test case.
Environment Setup
To implement the callback mentioned above, you will need a Python MySQL library, such as mysqlclient, installed on the system running the Engine so that the Callback can use it to communicate with the database.
Installing the Python MySQL library on Ubuntu:
sudo apt-get install libmysqlclient-dev
pip3 install mysqlclient
Installing the Python MySQL library on MacOS:
brew install mysql
pip3 install mysqlclient
Then, run Python and try to import the library to make sure it's available.
ENGINE % python3
Python 3.9.8 (v3.9.8:bb3fdcfe95, Nov 5 2021, 16:40:46)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
Next, create a new user in MySQL for the callback.
CREATE USER 'callback'@'%' IDENTIFIED BY 'callbackpass';
GRANT DELETE ON webservice.users TO 'callback'@'%';
FLUSH PRIVILEGES;
Callback Implementation
Finally, we create a callback that connects to the database and deletes the users from the users
table.
- Go to the Test Assets page
- Click the Create button and select Callback from the menu
Copy-paste the below example callback into the text area of the editor.
import MySQLdb
def callback(context):
if context.runtime.is_analysis() is True:
return
db = MySQLdb.connect(
host="127.0.0.1",
port=3306,
user="callback",
passwd="callbackpass",
db="webservice"
)
cursor = db.cursor()
cursor.execute("DELETE FROM users")
db.commit()
cursor.close()
db.close()
Finally, name your callback and save it by clicking File > Save.
To make the Callback more sophisticated and not hardcode credentials and connection details, you could create new Session Variables in the Project's Variable Collection to hold the database connection information. Then, the Callback could obtain those from the Session Variables (via context.session
) during runtime. As a result, the database connection would become configurable from the Project configuration on a per-target basis.
Test Flow Update
Open the Test Flow Template (named "createUser Flow") of the Project. Then:
- Drag the Callback action on the left and drop it onto the workspace are in the middle. The Callback action gets added to the end of the test flow.
- Scroll to the bottom of the test flow and use the drag handle of the Callback action to move it before the Disconnect action.
- Open the Callback using the expand button (arrow) and select your Callback from the dropdown list.
- Save the Test Flow Template.
You Test Flow Template should look similar to the one shown below.
Start Testing
You can now start your test. If you check the users
table during the test, you will find there's never more than 1
user in the table as GUARDARA deletes users after each test case due to the Callback we have just implemented.