Skip to main content

Advanced XDE Tester topics: leveraging the power of regular expressions

Bill Grant, Senior IT Specialist, IBM Rational Software
Bill Grant is a Senior IT Specialist for IBM Rational Software based in New York City. His primary focus is on the application of modern software engineering practices. Bill has worked with numerous software development teams in consulting, mentoring, and training capacities. His background includes leading development projects employing technologies including C++, Visual Basic, Delphi, and Java. His expertise extends to all aspects of the software development lifecycle -- change management, requirements management, object-oriented analysis and design, functional and performance testing, and project and process management. He can be reached by e-mail.

Summary:  One powerful facility in IBM Rational Functional Tester for Java and Web (XDE Tester) that enables testers to make their scripts more dynamic is regular expressions. This article describes a couple of different ways you can leverage the power of regular expressions in your test scripts.

Date:  30 Mar 2004
Level:  Introductory
Activity:  453 views

Editor's note: This article applies to IBM® Rational® XDE™ Tester version 2003, as well as IBM® Rational® Functional Tester for Java™ and Web version 2003.

IBM® Rational® XDE Tester is a script development environment that enables creation of scripts that are resilient to application changes. One powerful facility in XDE Tester (and in Java itself) that enables testers to make their scripts more dynamic is regular expressions. This article, one of a series covering advanced topics in XDE Tester, describes a couple of different ways you can leverage the power of regular expressions in your test scripts. By following along as we develop an example script, you'll learn how to create dynamic verification points for situations where the script accesses data that will vary from run to run, and also how to pull dynamic data out of a control.

First, a word about regular expressions

A regular expression is a formula for matching strings that adhere to a particular pattern. Using regular expressions enables you to search for more than one combination of characters at a time. For example, if I wanted to search for my last name, whether capitalized or not, I could search for [Gg]rant and the search would find Grant or grant but not gRant or granT.

Regular expressions can include both normal characters (A to Z, a to z, 0 to 9, and such) and metacharacters (characters that provide special meaning). One example of the latter is the brackets ([]) metacharacter, which roughly translates to "match any one of the characters between the brackets." Another example is the asterisk (*), which translates to "match zero or more occurrences of the character before the asterisk." For instance, the regular expression Flo*d would match to Fld, Flod, Flood, Floooooooood, and so forth.

This very simplified description of regular expressions is enough background for what I'm about to show you, but obviously there's much more to say. For a deeper understanding, see a book such as Mastering Regular Expressions by Jeffrey Friedl or visit one of any number of Web sites dedicated to regular expressions (see "References and Other Resources").


Setting up our example script

To demonstrate the use of regular expressions, we'll first record a script without them in this section, then in later sections add them in. We'll record our script against the sample ClassicsJavaA application (a Java-based store for purchasing classical music CDs) that's installed with XDE Tester. Our basic script will record placing an order for a piece of music and will add an object data verification point to verify the Message window that pops up when the order is placed. This window returns the order number to the customer, as shown in Figure 1.

Message window giving order number to customer
Figure 1: Message window giving order number to customer

Because this order number is bound to change, a typical verification point is prone to failure, as you'll see when you play back the script. We'll leverage regular expressions to build a script resilient enough to handle this dynamic behavior. Additionally, you may want to use this value for other validations or in other parts of the script. We'll modify the script with regular expressions to make this possible as well.

Prepare your environment

Start XDE Tester and open a project (or create one if you haven't already done so). You'll also need to be sure you've done the following:

  • enabled the Java environments for testing
  • configured the ClassicsJavaA application for testing

If you haven't enabled your Java environments for testing, click Configure > Enable Environments for Testing. Click the Java Environments tab and you should see something like Figure 2.

Enable Environments window, Java Environments tab
Figure 2: Enable Environments window, Java Environments tab

To make sure you have all of your environments enabled, click the Search button and choose the Quick search. This is usually sufficient. Once you're done you can close the Enable Environments window.

If you haven't already configured the ClassicsJavaA application for testing, start by clicking Configure > Configure Applications for Testing. You'll see something like Figure 3.

Application Configuration Tool window
Figure 3: Application Configuration Tool window

If you don't already see ClassicsJavaA present, do the following:

  1. Click Add.
  2. Select Java Application and click Next.
  3. Browse to <XDETester Location>\samples\ClassicsJavaA.jar where <XDETester Location> is c:\Program Files\Rational\XDETester by default.
  4. Click Finish.

You can click Run if you want to test whether the application is configured properly.

Record the basic script

Now we're ready to record our basic script. Proceed as follows:

  1. Click the Record New XDE Tester Script button on the toolbar.
  2. Name the script Classics_One, then click Next.
  3. Choose to use a private Test Object Map, then click Finish. You'll see the Recording bar (Figure 4).
The Recording bar
Figure 4: The Recording bar
  1. To start the application, click the fourth button on the bar, Start Application. Select ClassicsJavaA -- java as the application and click OK.
  2. In the tree view, open the Haydn section, click Violin Concertos, and click the Place Order button.
  3. Leave everything on the Member Logon page the way it is and click OK.
  4. On the Place An Order screen, click the Card Number field and type 1111222233334444, then click the Expiration Date field and fill in 12/05.
  5. Click the Place Order button.

The Message window that pops up is what we want to verify. Create an object data verification point on the label by doing the following:

  1. Click the third button on the Recording bar, Verification Point and Action Wizard.
  2. Drag the hand on top of the javax.swing.JLabel that begins with "Your order has been received!"
  3. Leave the defaults (the first tab on this screen is Object Data VP) and click Next.
  4. Click Finish.
  5. Click OK and close the ClassicsJavaA application.
  6. Stop recording with XDE Tester by clicking the first button on the Recording bar, Stop Recording.

Your script should have a testMain function that looks something like this:

public void testMain (Object[] args) 
{
 startApp("ClassicsJavaA");

 // Frame: ClassicsCD
 tree().click(atPath("Composers->Haydn->Location(PLUS_MINUS)"));
 tree().click(atPath("Composers->Haydn->Violin Concertos"));
 PlaceOrder().click();

 // Frame: Member Logon
 OK().click();

 // Frame: Place an Order
 text().click(atPoint(22,13));
 PlaceanOrder().inputChars("1111222233334444");
 text2().click(atPoint(4,4));
 PlaceanOrder().inputChars("12/05");
 PlaceOrder2().click();
 YourorderhasbeenreceivedYouVP().performTest();

 // 
 OK2().click();

 // Frame: ClassicsCD
 ClassicsJava2(ANY,MAY_EXIT).close();
}

Your verification points and test objects should look like those shown in Figure 5.

Listing of verification points and test objects for the basic script
Figure 5: Listing of verification points and test objects for the basic script

Play back your script

Now try running your script by clicking the Run XDE Tester Script button on the toolbar. Depending on the log type you're using, the format may differ, but the result will be the same -- you'll see that the verification point YourorderhasbeenreceivedYou failed. If you choose to use either TestManager or HTML for your format, you can see the details as something resembling Figure 6.

Details of how the verification point failed
Figure 6: Details of how the verification point failed
(click here to enlarge)

It's clear that the verification point isn't resilient enough to encompass changing order numbers. To make it so, let's modify the verification point by converting it to a regular expression.


Making our verification point dynamic

If you have TestManager, the HTML Log, or the Verification Point Comparator still open, close them now so we can go back and make changes to our basic script. We'll use a regular expression to make the verification point flexible enough to handle any digits after the : (colon and space) and before the . (period).

Back in the XDE Tester Script Explorer window, double-click the verification point YourorderhasbeenreceivedYou, shown in Figure 7.

Verification point in XDE Tester Script Explorer
Figure 7: Verification point in XDE Tester Script Explorer

Now convert the verification point into a regular expression by clicking the RE: button. Once you've done that, you'll notice the format changes a little: the : (colon) and . (period) are now preceded by a \ (backslash). Let me explain what's happened here.

The : and the . are both regular expression metacharacters. That means that when found on their own, the colon and the period each have a special meaning, just like the brackets and asterisk do. If we want to treat those characters as "normal" characters, we must escape them (or put a backslash in front of them). (Note: There are some situations where these characters don't need to be escaped, but this is beyond the scope of this article. For more information, refer to "References and Other Resources.")

If we did nothing else, our verification point would work the same as it did before. We need to replace the number (in our example, 32) with the three characters \d+. This can be interpreted as meaning "as many digits as possible, at least one." Be sure not to add any other characters or replace any other existing characters (for example, the space after the colon or the period at the end), as this will cause the regular expression to fail. The window should look like Figure 8.

Verification point converted to a regular expression
Figure 8: Verification point converted to a regular expression

Save and close the verification point. Then try playing back the script again. It shouldn't fail at that verification point anymore.


Pulling dynamic data out of a control

While making the value of the order ID dynamic is useful, there may also be times when we actually need this value. It may be useful if we want to validate directly that the order is in the database, or we may need it to look up the order later in other test scripts. We can use the power of regular expressions to parse the value of the digits into an object, which we can then plug in where we need it.

We first must import XDE Tester's Regex package to make the necessary Java classes available to our script, since we're going to be using regular expressions as part of the Java code. Add the following line near the other import lines at the top of the script:

import com.rational.test.util.regex.Regex;

Next we need to add some code to get the value of the order ID and send it to the log. Rather than using the verification point YourorderhasbeenreceivedYou, we'll be interacting directly with the test object, YourorderhasbeenreceivedYouror. (See Figure 5 above.) After the line

YourorderhasbeenreceivedYouVP().performTest();

add the following code:

// Code to pull out the order id
String theOrderLbl =
 YourorderhasbeenreceivedYouror().getTestData("text").toString();
String theRegexStr = "is\\: (\\d+)\\.";
Regex theRegex = new Regex(theRegexStr);
if (theRegex.matches(theOrderLbl))
{
 String theOrder = theRegex.getMatch(1).toString().trim();
 logInfo("The order is " + theOrder);
} else
{
 logInfo("No match, value is: " + theOrderLbl);
}

You'll see our regular expression in this line of code:

String theRegexStr = "is\\: (\\d+)\\.";

Let's break it down:

  • is\\: are taken as string literals. Note that there's a space after the colon.
  • (\\d+) means "one or more digits." The parentheses indicate that this value should be placed in a holding area where we can later access it.
  • . is taken as a string literal.

Note that because we're entering this code in Java, we need to escape characters like the backslash. In places where we already needed a backslash (such as before the colon and before the d), we end up with two backslashes in a row.

After checking to see if we found a match, the line

String theOrder = theRegex.getMatch(1).toString().trim();

puts the value of our order (the digits) into a variable that's added into the log in the very next line.

If you play back the script again, your results in the log should show a new string with the order number -- for example:

The order is 45


Wrap-up

Our example XDE Tester script has demonstrated how to convert verification points to regular expressions in order to make these points dynamic, and how to use regular expressions to pull dynamic values into an object we can reuse in other parts of the script. You'll want to make verification points dynamic whenever your script accesses data that will vary from run to run. And you'll want to use the power of regular expressions to pull dynamic data from a test object if you'll need to access it later.


References and other resources

  • "A Tao of Regular Expressions" by Steve Mansour. This Web page offers a pretty good first look at regular expressions.
  • Mastering Regular Expressions by Jeffrey E. F. Friedl (O'Reilly, 2002). This book thoroughly covers the topic of regular expressions and gives excellent examples in Java.

About the author

Bill Grant is a Senior IT Specialist for IBM Rational Software based in New York City. His primary focus is on the application of modern software engineering practices. Bill has worked with numerous software development teams in consulting, mentoring, and training capacities. His background includes leading development projects employing technologies including C++, Visual Basic, Delphi, and Java. His expertise extends to all aspects of the software development lifecycle -- change management, requirements management, object-oriented analysis and design, functional and performance testing, and project and process management. He can be reached by e-mail.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational
ArticleID=4058
ArticleTitle=Advanced XDE Tester topics: leveraging the power of regular expressions
publish-date=03302004
author1-email=billgrant@us.ibm.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers