Service Virtualization Discussion Forum
Showing results for 
Search instead for 
Do you mean 

Scripted Rules in HP SV


Scripted Rules in HP SV



Can some please let me know how to use Scripted Rules in SV. And input functions are also very confusing, please let me know if any online support or documentation is available.




Frequent Advisor

Re: Scripted Rules in HP SV

Scripted Rules are Javascript code which enables you to access the message structure directly and manipulate its values.  Before you get into the practice of scripting a rule, you should first rule out the ability to use the normal data model rules to achieve what you are looking to do. 


Even though this feature is new, too many people have jumped in scripting rather than thinking the problem through and leveraging the data model rules BEFORE starting to script.


On aspect that many new users fail to understand is how SV processes the rules.  The common (and mis-guided thought) is the rules are only processed from top to bottom and when it reaches the Default Rule, all processing is done.


This is not the case.


Let's take for example the most simple data model which has an empty Learned Rule and Default Rule (containing default responses).  Most people think that SV would pass through the rules only once but in reality, it passes through them twice.

First pass:

  • No match is found in Learned Rule
  • Match is made on the Default Rule

Since SV knows a rule was triggered, it will make a second pass to see if it can match on any rule that has NOT been triggered already.


Second pass:

  • Still no match is found in the Learned Rule
  • Default Rule has already been triggered so it is skipped

At this point, SV understands that no other possible changes could occur so it returns the values that were established when it executed the Default Rule and returns those values.


This type of processing enables you to create if/then conditions at the data level without ever having to engage in one line of code.  This is truly what "Data Driven" means and no other company has this capability.


Could you share a little bit more in what you are attempting to do with the scripting? 


Also, the objects that are accessible are noted in the comments section of the scripted rule.


Frequent Advisor

Re: Scripted Rules in HP SV

UPDATE - new options for Scripted Rules


Right-click the scripted rule and select

Properties to display the options.


Skip the first simulation pass. To modify or complete data filled in by another rule, the

scripted rule must be have a higher priority than the rule you want to override. However, during

the first simulation pass, all the response data for the script has not yet been filled in by the

other rules. In this case, you may want to skip the scripted rule during the first simulation pass.


Execute the rule only once. As an example, you may be using the script to add an item into

an array, such as assigning an ID to a user. Each time the script is run, the rule will modify the

response, causing the simulation process to continue repeatedly. In this case, use this option

to properly execute the simulation.


This was all able to be done in the previous version as well through scripting but now this makes it easier.

Occasional Contributor

Re: Scripted Rules in HP SV

Hi Dave,


I have a requirement to virtualize a service in which I want to sum up the values coming in request and send the same value in response.


Ex: Request is 



so I want to send the response as 

<addResponse xmlns="">
<Result>x+y</Result>            // (where x and y value should be take in run time)

Can we achieve this using data model? I am trying to implement it using javascript but facing issue.


I have used below code 



var id1=Number(hpsv.request.add.x);
var id2=Number(hpsv.request.add.y);


but I am getting error TypeError: undefined cannot be converted to an object


Can you provide me correct way to do it?


Occasional Contributor

Re: Scripted Rules in HP SV

I have used below code


int id1=Number(hpsv.request.add.x);
int id2=Number(hpsv.request.add.y);

Frequent Advisor

Re: Scripted Rules in HP SV

I guessed you figured out the solution based on my previous post but since you haven't marked anything as solved, I thought I would get a little more specific here with providing a sample project and and a short 2 min video showing the steps.


First to explain the erro you are seeing, it is saying the structure hasn't been created.  You could manually create the structure but it is easier if you let SV create that for you.


So how do I let SV do it for me?


First you  must be aware of how rules are processed:

  1. Rules are process from top to bottom in the order of priorities.  This means that rules at the top have higher priorities than rules toward the bottom.
  2. Rules are iteratively processed.  This means that if you start with a blank data model which contains no data in the Learned rule and the Default rule, SV will go through the attempting to process the rules twice.

So what does #2 really mean.  It means that the first pass the Learn model is matched and therefore it is skipped.  Because the next rule (Default) will match which causes the the reply strcture to be created for you automatically.  Since an action took place, SV will go back and evaluate rule 1 again (Learned) to see if it can match now.  There still is no match so the response will contain only the data from the default rule.  NOTE: the default rule is not checked again as it has been triggered once already and rules can only be triggered once.


When you apply the above knowledge, you can start to see how you can create very robust rules without ever needind to use scripting rules and if you do need to use the scripting rule you would only need to use minimal scripting.


Attached is a sample project for the Calculator service.  Work was only done for the Add operation.  Also attached is a short video showing the exact steps I did.  As a bonus, for those who have UFT, I have attached the UFT script I used to check the Add operation of the Calculator service.