Case Insensitive Matching of Real Name String Properties in froglogic’s Squish

Case Insensitive Matching of Real Name String Properties in froglogic’s Squish

Motivation

In Squish, a symbolic name contains multiple constraints on an object search. These constraints can apply to properties of an object, and compare to object references, strings or numerics. When comparing strings, it is possible for Squish to perform inexact comparisons, using wildcards or regular expressions.

Case insensitive matching is most frequently used in combination with the Windows file systems, which can store filenames using upper and lowercase letters, but do not distinguish between upper and lowercase characters when matching filenames on disk. It is therefore possible to open a file on Windows using different filenames, if they differ only in upper or lowercase. If there are GUI labels based on user input of Windows file names, this can result in slightly different GUI labels in the AUT, and this may cause problems with object identification in Squish test cases.

This article will show how to use regular expressions in Squish real names, to perform case insensitive matching of strings, for identification of AUT objects in test cases.

Regular Expression Constraints

To change a property constraint from an exact string to a regular expression, it can be done programmatically, or from the Object Map Editor GUI by double-clicking on the Operator and selecting RegEx from the drop-down menu that appears.

Object Map Editor, with a regular expression property

After saving and letting the refactor operation perform, one can find the entry for that symbolic name in the names.py file, and see that it now has a property value that refers to a RegularExpression object.

address_Book_MyAddresses_adr_MainWindow = {"type": "MainWindow", "windowTitle": RegularExpression("Address Book - MyAddresses.adr")}

In JavaScript, the entry would be in names.js and would look like this:

export var addressBookMyAddressesAdrMainWindow = {"type": "MainWindow",  "windowTitle": new RegularExpression("Address Book - MyAddresses.adr")};

Unfortunately, when representing regular expressions in the object map like this, flags such as the „case insensitive“ flag can not be represented in the entry. This means that case insensitive matching of string properties is a little more difficult than simply matching a regular expression. However, given a string used for identification, it is straightforward to convert that into a regular expression that can match case insensitively, and we can use that in a real name constraint of an object map entry. This article will show you how.

Character Class Meta-Characters

To accomplish case insensitive matching of properties in Squish real names, we will use regular expression meta-characters that specify a class of characters. One of the simpler ones is the [square bracket] meta-character, where a square-bracketed expression can be replaced by a single character, and the possible valid characters are listed between the [square brackets].

For example, a case insensitive regular expression for a string like „heLLO wOrLD“ which would match any possible combination of upper-lowercase characters that spell those two words (separated by a single space) would look like this:

[hH][eE][lL][lL][oO] [wW][oO][rR][lL][dD]

A function in Python that, given an alphanumeric string, returns a RegularExpression like the above for it, would look like this:

from objectmaphelper import *
import re

def caseInsensitive(alphaString):
returnValue = ""
alphaString = re.escape(alphaString.lower())
for ch in alphaString:
if ch.islower():
returnValue += "[{}{}]".format(ch, ch.upper())
else:
returnValue += ch
return RegularExpression(returnValue)

A similar function in JavaScript would look like this:

import { RegularExpression, Wildcard } from 'objectmaphelper.js';
var isAlpha = /[a-z]/
function escapeRegExp(string) {
return string.replace(/([.*+?^${}()|[]\])/g, '\$1');
}
function caseInsensitive(alphaString) {
var returnValue = "";
alphaString = escapeRegExp(alphaString.toLowerCase());
for (i=0; i<alphaString.length; ++i) {
var ch = alphaString[i];
if (ch.match(isAlpha)) {
chu = ch.toUpperCase();
returnValue += "[" + ch + chu + "]";
}
else {
returnValue += ch;
}
}
return new RegularExpression(returnValue);
}

Notice that each of these functions also escapes any regular expression special characters to prevent them from being interpreted in an unexpected way when passed onto Squish’s RegularExpression constructor.

Case Insensitive Real Name Properties

Given a symbolic name, with a property such as windowTitle, and a string we want to use for case insensitive identification, we can use this function for the value in the associative mapping that is a Squish real name. With the Squish 6.4 Scripted Object Map, The Python and JavaScript code would look like this:

names.addressBookMainWindow["windowTitle"] = 
caseInsensitive("Address bOOk - mYaDdreSSes.ADR")

While the Perl would look more like this:

$Names::addressBookMainWindow{"windowTitle"} = 
caseInsensitive("Address bOOk - mYaDdreSSes.ADR");

Conclusion

When testing software that ignores upper-lower case of strings, it is sometimes necessary to test using a case insensitive way of identifying objects. This article shows a way one can do this in froglogic’s Squish.

For another tip about improving object names, see this article.

Trainer and Consultant for froglogic. Co-author of "Introduction to Design Patterns in C++ and Qt"

0 Kommentare

Eine Antwort hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*