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.

No comments: