Showing posts with label regular expressions. Show all posts
Showing posts with label regular expressions. Show all posts

February 15, 2012

Using Callbacks on the Regexp.Replace function

On our testprojects, I use regular expressions most of the time for the matching or testing of strings on our Application Under Test. But sometimes I have to use the Replace method of the RegExp object. This day, I discovered an advanced usage of the RegExp.Replace() method. Let me show you with an example:

Dim replacement
Dim regEx : Set regEx = New RegExp ' Create a regular expression.
Dim myString : myString = "Cake, and grief COUNSELING, will be Available at the CoNcLuSiOn of the test."

regEx.Pattern = "\b\w+\b" ' \b = inside word boundary, \w = word character,
                          ' so this pattern matches each single word
regEx.Global = True       ' Set the scope to Global

' normally we replace like this:
replacement = "word"
MsgBox regEx.Replace(mystring, replacement)

' resulting in: "word, word word word, word word word word word word word word word."

This looks familiar I think. But it is not very sophisticated. Wouldn't it be nice if we can replace each match with a custom replacement. With the use of a Function Reference, we can!

 ' We can actually perform a custom action on replace instead of doing a fixed replace
' We do this with making a reference to a function with getref
Set replacement = getRef("Capitelize")

' We need to create a function of course with the same name and three parameters
' 1. singleMatch : the string that matched to the pattern
' 2. position : the position the singlematch string was found
' 3. fullString : the full string (same as 'myString' in this case) without _any_ replacements
Function Capitelize(singleMatch, position, fullString)

    ' Capitelize returns the string singleMatch changed with the first character
    ' in uppercase and all others in lowercase
    Capitelize = ucase(left(singleMatch,1)) & lCase(mid(singleMatch,2))
End Function

MsgBox regEx.Replace(myString, replacement)
' This results in: "Cake, And Grief Counseling, Will Be Available At The Conclusion Of The Test."

Nice huh? Now we can match for a date and replace it on the fly by the correct format. We can replace straight quote characters for curly quotes with the correct orientation with much more ease, or only do a replace if it is within a certain character range in the string. "All new possibilities arise!"

June 5, 2008

Real Regular Expressions in WinRunner


Automated testing without regular expressions is almost unthinkable nowadays. But there was a time, almost nobody knew what regex's were. Only a handful of developers and they were all working on Unix.

WinRunner started with support of regex's years ago, because it seemed to be handy for testers. Unfortunately they did it only with their own set of limited meta characters. You can use the . for any character, the * for zero to unlimited of the previous character, the square brackets [ ] for ranges or included characters and that was it.
Probably they thought that the full set of regular expression characters was overkill for non technical users like testers, and they could not know that regular expressions would become so regular.

I presume that for compatibility purposes Mercury never upgraded their set of regular expressions, but it is a real handicap when you are deep into the test automation.

After a bit of searching, I found this site on the internet: Misha's regex. Misha did a great job, porting C++ regular expressions to a dynamic library that can be loaded into WinRunner. Also, he provides some WinRunner functions to give you a kick start when you want to work with it.

As far as I know, this is the only ready to use solution for WinRunner to implement full regular expressions. And even better, you can now use "label_like" and "id_like" as attributes in object recognition.

The only thing I did was writing to simple functions to do a simple match:

public function re_match_simple(in orig_str, in pattern) {

    auto outPos, outLen, outDetail;


    return re_match(orig_str, pattern, outPos, outLen, outDetail);
}

public function re_search_simple(in orig_str, in pattern) {

    auto outPos, outLen, outDetail;

    if (re_search(orig_str, pattern, outPos, outLen, outDetail))
        return outPos+1;    # +1, otherwise 0 (=FALSE) will be returned if found on first position

    return FALSE;
}

This allows me to do quick checks like

if (!re_match_simple(outDate, "^\d{1,2}-[JFMASOND][aepuco][nbrylgptvc]-(19|20)\d\d$")
    myReport("Date format for '"outDate"' is not according standard: dd-Mmm-yyyy", eCHECK);

or

customerId = substr(fullText, re_search_simple(fullText, "\d{8}"), 8);

Even the test analists are working with regular expressions. (on that particular project we worked with separated roles: testautomation and testanalysis). The only thing they had to do was flag a string with an exclamation mark (!) in front and it was checked as an regular expression. The exlamation mark for obvious reasons.