Virtual Service Events
Using Virtual Service Events
To perform custom actions when the virtual service is functioning, you can use script properties of your service and its virtual requests. An alternative way to do this is to create event handlers in the Events manager:
Click Events on the toolbar.
In the subsequent dialog, click
to create a new event handler.
Select the event type, click OK and write the script code:
The event list in the dialog is project-based. That is, in this dialog, you can create event handlers that occur for virtual services and other objects of your current project.
Service-Related Events
Event | Description |
---|---|
| Occurs right after a virtual service starts. Same as the |
| Occurs when a virtual service stops. Same as the |
| Occurs when a virtual service receives a request. Same as the |
| Occurs after a virtual service sends a response to the client. Same as the |
ImportantThe | |
| Occurs right after a JMS virtual service starts. Same as the |
| Occurs when a JMS virtual service stops. Same as the |
| Occurs when a JMS virtual service receives a request. Same as the |
| Occurs after a JMS virtual service sends a response to the client. Same as the |
| Occurs before a virtual service routes an incoming request to a real API. You can use the |
| Occurs after a virtual service receives a response to a routed request. You can use the |
Target Property
All the events have the Target
property. It specifies the name of the virtual service, to which the event will apply. If this property is empty, an event will trigger for any virtual service in your project.
You can create the same event handler for multiple virtual services. You can determine the virtual service in script code by checking its name. To get this name, use the runner.mockContext.modelItem.name
property.
Script Objects
Object | Description |
---|---|
| Contains methods and properties for posting messages to the Script log (to view it, click Logs in the bottom left corner of the ReadyAPI window and switch to the Script log tab). Use this object for logging and debugging purposes. |
| Provides a scripting interface to the incoming request. Use methods and properties of this object to get request headers, URL, contents and other data. |
| Provides a scripting interface to the response a virtual service is going to send to the client. Use methods and properties of this object to change the response data. |
| Provides a scripting interface to the virtual service runner. Use methods of this object to stop the service or get scripting access to the execution context. |
Note
Unlike the script properties, these event handlers are not saved as part of the virtual service data and are not executed when you run your service on a remote computer.
By default, the event handlers are shared among all the virtual services in your project. To determine for which service the event handler was called, you use the
Target
property of your event handlers, or call therunner.mockContext.modelItem.name
property to distinguish the virtual services in script code.ReadyAPI ignores result values of the event handlers.
ReadyAPI does not inform you on exceptions that occur in the code of these event handlers. If an exception occurs, ReadyAPI simply exits the event handler call.
The script debugger does not work for event handlers.