By Alex on Nov 06, 2009
Recently wile working on a project I came across a challenging script to write, the default behaviour of OpenScript is to record the parameters sent to the server and then play them back once correlated, meaning that if we have extra navigations we need to create them manually, so lets take at an example from a PeopleSoft implementation where depending on the client account that we used during an iteration we would have 1 or more parameters, the interface looked like the picture below with 2 parameters:
Furthermore we had another challenge, each time we updated the drop down menu we generated a new navigation, that gave us 2 challenges a script with X navigations and X parameters for each navigation, right not what we recorded ......
So we have to build the navigations and parameters dynamically, that is to my opinion where OpenScript it's at its best making use of the Java API that it provides to add logic to your scripts, so first of all we need to find out how many parameters we have for each iteration, to do so we will use a regular expression to find the parameters:
Note the null parameter in which we state that we would like all the matching values that we find in the HTML page to be stored as list of variables in the script, meaning that it will give us a list of all the parameters that it finds matching that regular expression.
I like to add the the parameters where easily identified as they used the following HTML;
OK once we have the list we know how many parameters we need to update, we can get that number with the following code:
Will give us the number of navigations that we need to reproduce to update each drop down menu
Ok so now we have a list lsSens containing all parameters, next we will need a list to keep track of the updated parameters, we ill call it isDone:
Ok now we have 2 lists one with all the parameters and another one (empty at the moment) containing the parameters that have been updated, also we know how many navigations we need to reproduce to update each parameter.
Next we will need a method to update that list, we will place that just after the finish method in the script and call it findInParams
We are now ready to start building the navigations, first of all we will need to loop the step to generate lsSens.length number of navigations, we will use a for loop for that
Then we need to create an array to store all the parameters, the array size will be the number of static parameters + the new parameters found * 2 as there are 2 parameters to update in each case.
that using an ArrayList would have been easier for performance
reasons I have chosen an Array, not that ArrayLists are slower but
Arrays are still faster.
Then we will start assigning the static parameters to the array.
PeopleSoft needs to now which parameter is being updated so we will need to update this parameter with the value from the loop.
Then we need to finish adding the static navigations
and finally we will update the array with our new parameters, we will keep a count to update the array index and use a for loop to store the new parameters in the array:
Then we need to update the parameters that have been processed:
Then update the parameter that we are currently working on and add it to the isDone list
and finally set the rest of parameters to its original value.
We now have an array with all the parameters that we need to send to the server, first we need to close the for loop that updates the array as we do not need it any more:
Then we finally need to post that navigation substituting the list of parameters with the array that we just created.
Remember to close the first loop
And we now have a navigation that will be executed x times with x number of parameters each time !!!!
Probably the most important lessons learnt from the script are the fact that we can get a list of matching regular expressions:
Making it possible to build a dynamically generated list of parameters.