Thursday, February 2, 2012

How to Use Regular Expressions in Loadrunner Using Gnu Regex for Windows Library

The following describes how to use the Gnu Regex for Windows regular expression library with loadrunner.  The following summarizes the steps:

  • Download gnuwin32 Regex for Windows at  http://gnuwin32.sourceforge.net/packages/regex.htm
  • From gnuwin32 zip, copy regex.h to <loadrunner_install_directory>/include
  • In regex.h, comment out or remove the line  "#include <sys/types.h>" 
  • From gnuwin32 zip, copy regex2.dll to <loadrunner_install_directory>/bin
  • In the loadrunner script, load the regex2.dll
  • In the loadrunner script, compile the regular expression pattern
  • Call the regexec method with a string and the regular expression pattern
  • Check the return code for match or no-match

Simplest Example

/* 
 * Example of how to use gnuwin32 Regex with loadrunner
 * Download gnuwin32 Regex for Windows 2.7
 *   http://gnuwin32.sourceforge.net/packages/regex.htm
 * Comment out line "#include <sys/types.h>" from gnuwin32 regex.h
 * Put gnuwin32 regex.h in <loadrunner_install_directory>/include
 * Put gnuwin32 regex2.dll in <loadrunner_install_directory>/bin
 */
#include "regex.h" 


// preprocessor macros giving names to regex response codes
#define MATCH 0
#define NOMATCH 1
#define ERROR   2


int dll_loaded = 0;


Action()
{
  char *test_string = "test";
  char *pattern = ".est";
  regex_t pattern_buffer;
  int status;


  // load gnuwin32 regular expression dll if not already loaded
  if (!dll_loaded) 
  {
    lr_load_dll("regex2.dll"); 
  }


  // compile the regular expression pattern
  if (regcomp(&pattern_buffer, pattern, REG_EXTENDED)) // REG_EXTENDED = POSIX Extended Regular Expression syntax
  {
    lr_error_message("Failed to parse regex pattern: %s", pattern);
    return ERROR;
  }


  // see if the pattern matches the input string
  status = regexec(&pattern_buffer, test_string, 0, NULL, 0);  // For meaning of last 3 params, see api doc: http://gnuwin32.sourceforge.net/packages/regex.htm


  // log the results
  lr_log_message("%s string \"%s\" to regex pattern \"%s\"",
  status==MATCH ? "Matched" : "Failed to match",
  test_string,
  pattern);
}

Console Output

Running Vuser...
Starting iteration 1.
Starting action Action.
Matched string "test" to regex pattern ".est"
Ending action Action.
Ending iteration 1.
Ending Vuser...



Test Script
The following example runs through a list of tests of various regular expression patterns used with both match expected and no-match expected test cases.


/* 

 * Example of how to use gnuwin32 Regex with loadrunner
 * Download gnuwin32 Regex for Windows 2.7
 *   http://gnuwin32.sourceforge.net/packages/regex.htm
 * Comment out line "#include <sys/types.h>" from gnuwin32 regex.h
 * Put gnuwin32 regex.h in <loadrunner_install_directory>/include
 * Put gnuwin32 regex2.dll in <loadrunner_install_directory>/bin
 */
#include "regex.h" 


// preprocessor macros giving names to regex response codes
#define MATCH 0
#define NOMATCH 1
#define ERROR   2


int dll_loaded = 0;


Action()
{
  regex_t pattern_buffer;


  // load gnuwin32 regular expression dll if not already loaded
  if (!dll_loaded) 
  {
 lr_load_dll("regex2.dll"); 
  }


  // Run tests  expected to match
  RunTest("test", ".est", MATCH);  // regex any char operator
  RunTest("test", "a*test", MATCH); // regex zero or more operator
  RunTest("test", "t+est", MATCH); // regex one or more operator
  RunTest("tttest", "t{3}est", MATCH); // regex interval operator
  RunTest("test", "t(ES|es)t", MATCH); // regex alternation operator
  RunTest("test", "t[^abcdfghijklmnopqruvwxyz]*t", MATCH); // regex list operator, with negation and repetition
  RunTest("test", "[t-z]est", MATCH);  // regex range operator
  RunTest("test", "(([u-z]est)|([t-z]es[t-z]))", MATCH);  // regex grouping operator
  RunTest("tests", "^te.*", MATCH);  // regex beginning of line operator
  RunTest("tests", ".*s$", MATCH);  // regex end of line operator
// 
  // Run tests  expected to not match
  RunTest("test", ".test", NOMATCH);  // regex any char operator
  RunTest("est", "t+est", NOMATCH); // regex one or more operator
  RunTest("test", "t(ES|Es)t", NOMATCH); // regex alternation operator
  RunTest("test", "t[^abcdefghijklmnopqrsuvwxyz]*t", NOMATCH); // regex list operator, with negation and repetition
  RunTest("tes2t", "tes[:alpha:]*t", NOMATCH); // regex character class operator: alpha   // 
  RunTest("testA", "test[:digit:]", NOMATCH); // regex character class operator: digits
  RunTest("test", "[u-z]est", NOMATCH);  // regex range operator
  RunTest("test", "(([u-z]est)|([a-s]es[t-z]))", NOMATCH);  // regex grouping operator
  RunTest("tests", "^es.*", NOMATCH);  // regex beginning of line operator
  RunTest("tests", ".*t$", NOMATCH);  // regex end of line operator


  return 0;
}


/*
 *  RunTest
 *  input: test string to match to regex pattern
 *  input: regex pattern
 *  input: expected result: match or no matc
 */
void RunTest(char *test_string, char *pattern, int expectedResult)
{
int status = TestMatch(test_string, pattern);


// Report whether or not test passed
lr_log_message("%s %s string \"%s\" to regex pattern \"%s\"",
  expectedResult==status ? "TEST SUCCESS: As expected," : "TEST FAILURE: Not expected,",
  status==MATCH ? "matched" : "failed to match",
  test_string,
  pattern);
}


/*
 *  TestMatch
 *  input: test string to match to regex pattern
 *  input: regex pattern
 *  output results of match attempt
 */
int TestMatch(char *test_string, char *pattern)
{
regex_t pattern_buffer;
int status;


if (regcomp(&pattern_buffer, pattern, REG_EXTENDED)) // REG_EXTENDED = POSIX Extended Regular Expression syntax
{
lr_error_message("Failed to parse regex pattern: %s", pattern);
return ERROR;
}


    status = regexec(&pattern_buffer, test_string, 0, NULL, 0);  // For meaning of last 3 params, see api doc: http://gnuwin32.sourceforge.net/packages/regex.htm
return status;
}



Console Output


Running Vuser...
Starting iteration 1.


Starting action Action.
TEST SUCCESS: As expected, matched string "test" to regex pattern ".est"
TEST SUCCESS: As expected, matched string "test" to regex pattern "a*test"
TEST SUCCESS: As expected, matched string "test" to regex pattern "t+est"
TEST SUCCESS: As expected, matched string "tttest" to regex pattern "t{3}est"
TEST SUCCESS: As expected, matched string "test" to regex pattern "t(ES|es)t"
TEST SUCCESS: As expected, matched string "test" to regex pattern "t[^abcdfghijklmnopqruvwxyz]*t"
TEST SUCCESS: As expected, matched string "test" to regex pattern "[t-z]est"
TEST SUCCESS: As expected, matched string "test" to regex pattern "(([u-z]est)|([t-z]es[t-z]))"
TEST SUCCESS: As expected, matched string "tests" to regex pattern "^te.*"
TEST SUCCESS: As expected, matched string "tests" to regex pattern ".*s$"
TEST SUCCESS: As expected, failed to match string "test" to regex pattern ".test"
TEST SUCCESS: As expected, failed to match string "est" to regex pattern "t+est"
TEST SUCCESS: As expected, failed to match string "test" to regex pattern "t(ES|Es)t"
TEST SUCCESS: As expected, failed to match string "test" to regex pattern "t[^abcdefghijklmnopqrsuvwxyz]*t"
TEST SUCCESS: As expected, failed to match string "tes2t" to regex pattern "tes[:alpha:]*t"
TEST SUCCESS: As expected, failed to match string "testA" to regex pattern "test[:digit:]"
TEST SUCCESS: As expected, failed to match string "test" to regex pattern "[u-z]est"
TEST SUCCESS: As expected, failed to match string "test" to regex pattern "(([u-z]est)|([a-s]es[t-z]))"
TEST SUCCESS: As expected, failed to match string "tests" to regex pattern "^es.*"
TEST SUCCESS: As expected, failed to match string "tests" to regex pattern ".*t$"
Ending action Action.
Ending iteration 1.
Ending Vuser...


No comments:

Post a Comment