Back to Blog

Working with Objects, Variables, and Code in TruClient – Part 4

Introduction to TruClient – Part 1
Recording in TruClient – Part 2 
Enhancing a TruClient Script – Part 3 


Objects can be identified several ways in TruClient.  The recommended way is to use Automatic whenever possible.  Unfortunately, this doesn’t always work.  One of the best solutions is to use XPath.  Under the Object sections you have the ID Method.  This is usually set to Automatic.  You can also change this to XPath, which allows you to see the XPath value or to JavaScript which allows you to add code around the object.  More than likely, you’ll be working with the JavaScript ID Method.

One of the great features to XPath is that it can handle an array of objects.   Say you have an XPath value for a grid item that looks like this.


This XPath selects the 3rd row in that grid.  Well, what if you wanted to select a random row or evaluate all the rows and select the one you wanted?  You can easily do this by removing the tr ordinal [3].   So, your XPath would look like this.


This XPath will now return an array of all the objects in that column.

You can also use advanced XPath functionality to get the exact information you want.   XPath has all sorts of functions and operators that allow you to navigate the axis and nodes of the HTML/XML page.  Regular expressions are also predominantly used in XPath evaluation.

Note:  Some of the TruClient functions that use XPath will have an Ordinal argument separate of the XPath value; this means the XPath returns multiple results.  If there is an Ordinal, you can have VuGen select a value in that array randomly by setting the Ordinal to 0.

Variable Scope

Variable scope acts a little differently in TruClient, there are several scopes you have to deal with, global, action, step, and object.   If you assign a value to a variable within the argument section of TruClient function and call it “x”, x can now be used in any future argument in the action as a variable.  But if you assign the variables in an object it will only be available to that object.  So, if you use var to declare a variable in JavaScript that variable x will not be available to an object.  To get around this assign the variable to the Global object.  Thus, Global.x is available in any function or evaluated JavaScript.   It has been my experience it is best to try and always work with global variables since it is easy to get confused with action scope in JavaScript.

Function Scope in Objects

TC functions will typically only work in the argument section of TruClient functions.  If you want to use TC functions within an object or user function you should add ArgsContext to the front of it., for example ArgsContext.TC.getParam(“item”) would allow you to use an item name as part of the object’s identification.

Special Code Snippets

If you want to click a link whose name might be changing all the time, you can use this function and parameterize or correlate the name.

evalXPath("(//a[text()=\"Home Page\"])");

Another example is:

evalXPath("(//a[text()=\"" + ArgsContext.TC.getParam("param_name") + "\"])");

If you have a list of options and want to look through all of them and select one, with xpath there are multiple solutions to this problem, this was just one option I found.  Be sure to remove the ordinal from the element you want to evaluate.  The function below will capture all the elements as options.  Then it will go through each of those values, options[i].textContent, and find the one that matches Global.po.  It will then select that option.

var i;
 var options = evalXPath("/html/body/div/div[2]/table/tbody/tr/td");
 for (i=0;i<options.length;i++)
   if (options[i].textContent==Global.po)

You could also click a series of radio groups in a similar way by accessing DOM functions:


You can get additional information from the objects by using some of the debugging functions by using or window.alert for a pop-up or window.console.log function to write to browser console.  Example:


Key Functions

You can use either LR or TC for function prefix.  LR prefix only works in LoadRunner and may not work in StormRunner or TruClient standalone.

Function Example Notes
TC.setParam(name, value) TC.setParam(“c_po”, “12345”); Replaces lr_save_string
TC.getParam(name) po = TC.getParam(“c_po”); lr_eval_string
TC.getLRAttr(name) url = TC.getAttr(“a_url”); Replaces lr_get_attrib_string
TC.evalC(funcname) TC.evalC(“f_start”);
TC.log(text, level) TC.log(“text”, “Error”); Replaces lr_debug_message
valid level arguments
TC.userDataPoint(name, value) TC.userDataPoint(“CPU”, value) lr_user_data_point
evalXPath(XPath) See Above
TC.vuserStatusMessage(string) TC.vuserStatusMessage(“Username is:” + username); lr_vuser_status_message
window.alert(string) window.alert(“PO is ” + c_po);
Back to Blog