Dealing with dynamic object names

Dealing with dynamic object names

Now that Squish 3.2 is out I want to write about a scenario where you can take advantage of new Squish 3.2 features.

One common problem, esp. when testing web applications, is to identify objects with dynamic parts (such as IDs) in their object names. Usually these properties have a dynamic part, such as a unique number, in it which we want to ignore in the object name.

With Squish 3.2, this is now easily solvable.

Squish is extensible so you can extend the name generation method for such objects. Details can be found at https://www.froglogic.com/download/book32/book/ug-jsapi.html. Additionally, Squish 3.2 supports regular expressions and wild-cards in object names. With a combination of those two features, the problem can be easily solved.

So let’s assume we have DIV elements in our web application representing widgets which we want to access from our test scripts. These DIVs have ID properties, which we can use for the identification. But the IDs contain values like id=’fileOpen_2376327′ where the section after the underscore is dynamic and should be masked out when identifying such an object because it will be different on every run of the application. So we want to identify the object using id?=’fileOpen_*’ in the real name (the =? tells Squish that we want to do a wild-card match instead of a strict match).

But we don’t want to manually change every entry in the object map after recording a test. Squish should automatically generate such names when we record an interaction on such an object.

To allow this, Squish now comes with an extension API. So we create a JavaScript extension file with the following code


function myNameOf(o)
{
if (o.tagName == "DIV" && o.id) {
var id = o.id;
var start = id.indexOf('_');
if (start == -1) // generate default name when we don't find an underscore
return undefined;
id = id.substr(0, start) + "*";
var n = '{' + Squish.propertiesToName(o, ["tagName", "className"]) + " id?='" + id + "'}";
return escape(Squish.unifyName(n, o));
}
return undefined;
}


// install our name hook
Squish.addNameOfHook(myNameOf);

What this code does is to generate a name with the wild-card ID for our DIV elements. The API which can be used in such Squish extensions is documented in https://www.froglogic.com/download/book32/book/ug-jsapi.html.

Now we need to register that extension file by opening the file squish.ini, which you we find in the ‘etc’ directory of your Squish directory. There we add an entry


Wrappers/Web/ExtensionScripts="[path]/[myextension].js"

If we now record a test and access a DIV element with such a dynamic ID, a wild-card name will be generated automatically.

The example here is quite simplistic but shows the concept of such an extension. Much more can be done with the extension API. So refer to the documentation referenced above for more details or write a comment here!

Similar extensions and customizations are also possible in all other Squish editions.