data driven testing: how to create a data driven …...know how to start access, create a new empty...

20
1 D D a a t t a a D D r r i i v v e e n n T T e e s s t t i i n n g g : : H H o o w w t t o o C C r r e e a a t t e e a a D D a a t t a a D D r r i i v v e e n n T T e e s s t t w w i i t t h h X X D D E E T T e e s s t t e e r r An IBM Rational Software TechNote

Upload: others

Post on 24-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

1

DDaattaa DDrriivveenn TTeessttiinngg:: HHooww ttoo CCrreeaattee aa DDaattaa DDrriivveenn TTeesstt wwiitthh XXDDEE TTeesstteerr

An IBM Rational Software TechNote

Page 2: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

Creating a Data-Driven Test with XDE Tester

By Dr. Gerd Weishaar

The samples provided in the advanced topic section of the XDE™ Tester online help illustrate simple data-driven testing, but these samples can’t be used by simply copying and pasting them from the online help to an XDE Tester script. This article gives you the information you need in order to replicate those samples in a working environment. If you follow this article step by step and create the artifa s it describes, you’ll be able to create a working demo of data-driven testing with XDE Te ter.

This article assumes hat you already know the basics of XDE Tester and can create an XDE Tester datastore, recIt also assumes that Tester script.

Basics of DaThe basic idea of dawhile plugging in diany way by using th

To create a data-driv

1. Create the basic

2. Identify the inpu

3. Create a data sou

4. Change the XDE

• Load the test

• Create a loop

• Replace the

• Restructure t

• Create error

cts

t

ord a script, play back a script, and create a datapool with custom data types. you have a basic understanding of the commands generated in an XDE

ta-Driven Testing ta-driven testing is to record a use case once and play it back several times fferent data records. We assume that the use case doesn’t change its flow in ese different records.

en test script with XDE Tester, we’ll follow these steps:

recording of a use case.

t fields that will be replaced by variables.

rce for the data-driven testing.

Tester script to implement data-driven testing.

data.

that iterates the use case for every existing data record.

input fields with variables.

he script (optional).

handling for failed records (optional).

2

Page 3: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

3

Create the Basic Recording of a Use Case The data-driven testing samples have one thing in common: they all employ the same use case, which is a very basic interaction with the sample application ClassicsJavaA. To use the sample code provided in this article, create an XDE Tester script following these steps:

1. Start the application ClassicsJavaA.

2. Expand the folder entitled “Schubert” and select “Die schone Mullerin, Op. 25.”

3. Click the Place Order button.

4. Log on as a member.

• Select the member name “Trent Culpito” even though it’s the default. This will ensure that the command to select a name is written to the script.

• Enter the password (we recorded “nix” — German for “don’t care”).

• Click the OK button.

5. Place an order.

• Change the quantity (we recorded 2).

• Enter the credit card number (we recorded 1234567890).

• Enter the expiration date (we recorded 1113).

• Click the Place Order button.

6. Verify the success of the transaction.

• Use the verification point on the dialog label (property verification point) and use a regular expression — for example, [0-9]{2}.

• Click the OK button.

7. Close the application.

Listing 1 is the resulting script. import resources.ClassicsJavaAHelper;

import com.rational.test.ft.*; import com.rational.test.ft.object.interfaces.*; import com.rational.test.ft.script.*; import com.rational.test.ft.value.*; import com.rational.test.ft.vp.*; /** * Description : XDE Tester Script * @author Administrator */ public class ClassicsJavaA extends ClassicsJavaAHelper { /** * Script Name : <b>ClassicsJavaA</b>

Page 4: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

4

* Generated : <b>Sep 2, 2002 4:09:14 PM</b> * Modified : <b>Sep 2, 2002 4:09:14 PM</b> * Description : XDE Tester Script * Original Host : WinNT Version 5.0 Build 2195 (Service Pack 2) * * @since 2002/09/02 * @author Administrator */ public void testMain (Object[] args) { startApp("ClassicsJavaA"); // Frame: ClassicsCD tree2Tree().drag(atPath("Composers->Schubert"), atPoint(8,8), atRow(atPath("Composers->Schubert")), atPoint(8,8)); tree2Tree().drag(atPath("Composers->Schubert->Die schone Mullerin, Op. 25")); placeOrderButton2Button().click(); // Frame: Member Logon nameComboComboBox().click(); nameComboComboBox().drag(atText("Trent Culpito")); _passText2Text().drag(atPoint(53,8), atPoint(52,8)); logFrame1Frame().inputKeys("nix"); okorderlogonButton().click(); // Frame: Place an Order _quantityFieldText().drag(atPoint(20,10), atPoint(1,7)); orderFormFrame().inputKeys("2"); _cardNumberFieldText().click(atPoint(15,7)); orderFormFrame().inputKeys("1234567890"); _expireFieldText().click(atPoint(24,11)); orderFormFrame().inputKeys("4711"); placeOrderButton().click(); Yourorderhasbeenr_standardVP().performTest(); // OKButton().click(); // Frame: ClassicsCD ClassicsJavaFrame2(ANY,MAY_EXIT).close(); } }

Listing 1: Our sample XDE Tester script

Identify the Input Fields That Will Be Replaced by Variables To identify the input fields that will be replaced by variables, we need to think about which interactions can be accomplished with varying data — for example, select the CD, log on as user, enter quantity, enter credit card number. To keep this example as simple as possible, we won’t

Page 5: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

5

vary the selection of the CD, but we’ll vary the user and all corresponding data values like the credit card number and quantity.

The selection of the CD can be varied by manipulating (replacing) the text values that describe the tree navigation (bold text): tree2Tree().drag(atPath("Composers->Schubert"), atPoint(8,8), atRow(atPath("Composers->Schubert")), atPoint(8,8)); tree2Tree().drag(atPath("Composers->Schubert->Die schone Mullerin, Op. 25"));

In this example we won’t change the tree navigation, but we’ll change all user-specific data, including the selection of the user name in the logon screen. These values will be replaced by varying data records:

User name: nameComboComboBox().drag(atText("Trent Culpito"));

User password: logFrame1Frame().inputKeys("nix");

CD quantity: orderFormFrame().inputKeys("2");

Credit card number: orderFormFrame().inputKeys("1234567890");

Expiration date: orderFormFrame().inputKeys("4711");

Create a Data Source for the Data-Driven Testing When we identified candidates for variable data above, we ended up with five fields:

User name: string

User password: string

CD quantity: integer

Credit card number: string

Expiration date: string

Values to plug into these fields can come from several different kinds of data sources — for example, CSV files, Access DBs, and datapools. For purposes of this article, choose one of these types of data sources and follow the steps outlined below.

Option 1: CSV File This is the simplest type of data source. We can create a CSV file by simply extracting data records from the sample database: C:\Program Files\Rational\XDE Tester\samples\classicsdata.mdb.

It’s important that you use the same data as the application does; otherwise the scripts will produce error messages during playback. From this sample database you can retrieve only customer names. It doesn’t contain passwords, credit card numbers, expiration dates, or quantities; those values have to be added manually.

Page 6: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

Note: If you decide to create a datapool as your data source, you can use the data-generation capabilities and simply copy the Customer.csv file from the TestManager datastore. C:\Classics\Projects\Classics Online\TestDatastore\DefaultTestScriptDatastore\TMS_Datapools\Customer.csv

Customer.csv

Option 2: Access DB The easiest and fastest way to create an Access DB is by importing the Customer.csv file. I won’t go into the details of how to create an Access DB here but will instead assume that you know how to start Access, create a new empty database, create a table, and import data from the CSV file.

customers.mdb

To use the database in your XDE Tester script, you’ll have to create an ODBC entry (for example, customerdb). Then you can use a JDBC-to-ODBC bridge to connect to the database from any Java program. Further details are given below when we talk about implementation.

Note that this is also a possible way to connect to an Excel spreadsheet or any other file that can be accessed via ODBC interface.

Option 3: Datapool If you choose to use a datapool as your data source, we recommend using the existing data-generation capabilities and a CSV file to specify the user names. You can use this list of customers:

Trent Culpito Susan Flontly Emma Trenchenza Claire Stratus Rick Tumbler Tony Miatta Jack Thompson Sonia Evans John Barrie Cal Maltais Ryan Rucker Sean Pilnuff Torrence Rein Bill Wu

6

Page 7: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

Karen Farrell Jill Sanford

Figure 1 shows a sample configuration of the datapool.

Figure 1: Sample configuration of Customer datapool

Change the XDE Tester Script to Implement Data-Driven Testing Now that we’ve done all the necessary data preparation, we can change the XDE Tester script to implement data-driven testing. Before we do that, though, I want to mention that for every data source except the datapool, we’ll need to create a container that will hold the data records during test execution. To address this need, I decided to implement a data-source–neutral version using the standard Java container class (ArrayList) and a class called Customer.java. This kind of implementation can be done in the same way for all data sources, including datapool. package resources; public class Customer { public String name; public String password; public String creditCardNr; public String expDate; public String quantity; }

This class can be created with a simple text editor, and you can save/copy the file as Customer.java into the resource directory of the XDE Tester datastore. For example: C:\Classics\Projects\Classics Online\XDE TesterDatastore\resources

Customer.java

(Note that whenever you copy a new Java source file to the Eclipse/XDE Tester directories, you can make it available in your project by switching to the Java perspective [choose Perspective > Open > Java], right-clicking the project folder, and selecting “Refresh from local.”)

7

Page 8: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

An alternative is to use the Eclipse class wizard to create a new Java class. For more information about Eclipse, see the online documentation (Java Development User Guide — “Create a Java Class”).

To use the new class Customer.java, you’ll have to add an import statement to the beginning of your XDE Tester script: import resources.Customer;

Load the Test Data Now we’ll look at the code for loading the test data from each of the three sources mentioned above. To use one of the code samples, paste it into your XDE Tester script right after these lines: public void testMain (Object[] args) {

After copying in the sample code, right-click the editor window and choose “Organize Imports.” When asked to choose an array list, select Java.util.ArrayList. After you save the file, all compilation errors should be gone.

These code samples contain System.out.println statements that are used for simple verification that the data was loaded. For a final version they can be removed or pasted into comments (//).

Option 1: CSV File

The basic functionality that we use to open/read the CSV file is taken from “Java in a Nutshell.” The code sample (Listing 2) assumes that the CSV file is stored in the currently logged-in user’s home directory — for example, C:\Documents and Settings\"TomS".

ClassicsJavaA_CSV.java

String filename = System.getProperty("user.home")+File.separator + "Customer.csv"; ArrayList customerList = new ArrayList(); String line; Customer customer; int startIndex; int endIndex; try{ BufferedReader in = new BufferedReader(new FileReader(filename)); while ((line=in.readLine()) != null){ customer = new Customer(); System.out.println(line); startIndex = 0; endIndex = line.indexOf(',',0); customer.name = line.substring(startIndex,endIndex);

8

Page 9: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

startIndex = endIndex + 1; endIndex = line.indexOf(',',startIndex); customer.password = line.substring(startIndex,endIndex); startIndex = endIndex + 1; endIndex = line.indexOf(',',startIndex); customer.creditCardNr = line.substring(startIndex,endIndex); startIndex = endIndex + 1; endIndex = line.indexOf(',',startIndex); customer.quantity = line.substring(startIndex,endIndex); startIndex = endIndex + 1; endIndex = line.indexOf(',',startIndex + 1); customer.expDate = line.substring(startIndex); customerList.add(customer); System.out.println(customer.name); System.out.println(customer.password); System.out.println(customer.creditCardNr); System.out.println(customer.quantity); System.out.println(customer.expDate); }; in.close(); } catch(IOException e){ logError("File Open Error!"); } logInfo("File-Data loaded!");

Listing 2: Loading test data from the CSV file

Option 2: Access DB

The basic functionality that we use to open/read the Access DB is taken from “Java in a Nutshell.” The code sample (Listing 3) assumes that an ODBC entry (customerdb) exists.

ClassicsJavaA_DB.java

ArrayList customerList = new ArrayList(); Customer customer; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException e){ logError("Error: Failed to load JDBC:ODBC Driver!"); } try{ Connection con = DriverManager.getConnection("jdbc:odbc:customerdb","","");

9

Page 10: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT NAME,PASSWORD,CREDITCARDNR,QUANTITY,EXPDATE FROM CUSTOMERDATA"); while (rs.next()){ customer = new Customer(); customer.name = rs.getString("NAME"); customer.password = rs.getString("PASSWORD"); customer.creditCardNr = rs.getString("CREDITCARDNR"); customer.quantity = rs.getString("QUANTITY"); customer.expDate = rs.getString("EXPDATE"); customerList.add(customer); System.out.println(customer.name); System.out.println(customer.password); System.out.println(customer.creditCardNr); System.out.println(customer.quantity); System.out.println(customer.expDate); }; rs.close(); stmt.close(); con.close(); } catch(SQLException sqle){ logError("Error: SQL Exception - " + sqle.getMessage()); sqle.printStackTrace(System.out); } logInfo("DB-Data loaded!");

Listing 3: Loading test data from the Access DB

Option 3: Datapool

The basic functionality that we use to open/read the datapool is taken from the documentation “Rational Test Script Services for Java.” The code sample (Listing 4) assumes that a datapool (Customer) exists.

ClassicsJavaA_DP.java

ArrayList customerList = new ArrayList(); Customer customer; try{ TSSDatapool dp = new TSSDatapool(); boolean fRetval = false; String sDPName = "Customer"; dp.open(sDPName); fRetval = dp.fetch();

10

Page 11: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

11

while (fRetval){ customer = new Customer(); customer.name = dp.value("Name").toString(); customer.password = dp.value("Password").toString(); customer.creditCardNr = dp.value("CreditCardNr").toString(); customer.quantity = dp.value("Quantity").toString(); customer.expDate = dp.value("expDate").toString(); customerList.add(customer); System.out.println(customer.name); System.out.println(customer.password); System.out.println(customer.creditCardNr); System.out.println(customer.quantity); System.out.println(customer.expDate); fRetval = dp.fetch(); }; } catch(Exception e){ logError("Error: Datapool - " + e.getMessage()); e.printStackTrace(System.out); }; logInfo("DataPool loaded!");

Listing 4: Loading test data from the datapool

Create a Loop That Iterates the Use Case for Every Existing Data Record At this point I assume that you’ve successfully loaded the test data from whatever source you’ve chosen. This means that the container ArrayList customerList now contains all data records for data-driven testing.

To execute the use case several times using each record in turn as input data, we have to create a loop that contains the user interaction for this use case. To do this, copy the following code marked in bold after the data loading and before the first user interaction, as shown: logInfo("DataPool loaded!");// This is the line you paste the new code after. int size = customerList.size();// Paste this line in. for (int i=0;i<size;i++){ // Paste this line in. startApp("ClassicsJava");// This is the line you paste the new code before.

At the end of the user interaction, close the loop. ClassicsJavaFrame(ANY,MAY_EXIT).close();// End after this line. };// Close the loop.

Replace the Input Fields with Variables Your script is looping but is still using the same recorded values. Now we’ll replace them with variables taken from the Customer class (as shown in the lines in boldface below), which will be a different set of data for each iteration. The following code sample works with any kind of data

Page 12: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

12

source. It’s useful to keep the original statement, so copy it and paste it into a comment, as shown. nameComboComboBox().click(); // nameComboComboBox().drag(atText("Trent Culpito")); nameComboComboBox().click(atText(((Customer)customerList.get(i)).name)); _passText2Text().drag(atPoint(53,8), atPoint(52,8)); // logFrame1Frame().inputKeys("nix"); logFrame1Frame().inputKeys(((Customer)customerList.get(i)).password); okorderlogonButton().click(); // Frame: Place an Order _quantityFieldText().drag(atPoint(20,10), atPoint(1,7)); // orderFormFrame().inputKeys("2"); orderFormFrame().inputKeys(((Customer)customerList.get(i)).quantity); _cardNumberFieldText().click(atPoint(15,7)); // orderFormFrame().inputKeys("1234567890"); orderFormFrame().inputKeys(((Customer)customerList.get(i)).creditCardNr); _expireFieldText().click(atPoint(24,11)); // orderFormFrame().inputKeys("4711"); orderFormFrame().inputKeys(((Customer)customerList.get(i)).expDate); placeOrderButton().click();

Your data-driven testing scripts are now ready to be executed. Run them until your system memory starts smoking and have fun!

Restructure the Script (Optional) After you’ve made the modifications to the XDE Test script described above, you may decide that the script could use a facelift to improve its readability and structure, and ultimately its maintainability. We can restructure the script (which will combine common tasks in a function or method) by using the Extract Method feature in Eclipse. Select the source code that should be restructured and right-click Extract Method (see Figure 2). The refactoring wizard will guide you through several steps.

Page 13: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

Figure 2: Using the refactoring wizard in Eclipse

We extract two new methods: • loadTestData: protected void loadTestData(ArrayList customerList)

• mainTC: protected void mainTC(ArrayList customerList,int i)

The refactored structure now looks the same for all three implementations: public void testMain (Object[] args) { ArrayList customerList = new ArrayList(); loadTestData(customerList); int size = customerList.size(); for (int i=0;i<size;i++){ mainTC(customerList,i); };// End for. }

Create Error Handling for Failed Records (Optional) When you change the input data — for example, when you add an invalid data record or the application-under-test changes — an iteration of the use case may fail to execute fully and

13

Page 14: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

14

correctly. When this happens, the application-under-test may remain in a state that’s not well defined or there may be no starting point for the next iteration, and thus it’s likely that the remaining script won’t execute properly. A possible solution is to add some error-handling code that can help the test continue to execute even after a failed iteration.

The first step is to add some code that logs every data record before it’s used as input data. This will help to identify possible reasons why the script failed. String logStr = new String(); logStr = ((Customer)customerList.get(i)).name + ", "; logStr += ((Customer)customerList.get(i)).password + ", "; logStr += ((Customer)customerList.get(i)).creditCardNr + ", "; logStr += ((Customer)customerList.get(i)).expDate + ", "; logStr += ((Customer)customerList.get(i)).quantity; logInfo(logStr);

The special error handling will vary for each individual test case, but for the sample scripts we just want to kill the application when, for example, a playback failure leaves some windows or dialogs open/pending. Therefore, we have to obtain the process information for the application that we start. We do this by first declaring the object that will contain the process information: ProcessTestObject p; TestObjectReference testObjectRef; OSProcessInformation osProcessInfo; public void testMain (Object[] args) // Add declarations before this line.

We then change the loop so that we can catch any exception and do the error handling on our own, by wrapping the call to mainTC in a try-catch block. In the catch part of this statement we add some code to print out the error message and the stack trace, because it’s useful to find out what caused the exception. After printing the information, we kill the application if it’s available (and assume that it already got removed if it’s not available). for (int i=0;i<size;i++){ try{ mainTC(customerList,i); } catch (Exception e){ logError("Error: " + e.getMessage()); e.printStackTrace(System.out); if (!osProcessInfo.processNotAvailable) osProcessInfo.kill(); }; }// End for.

To verify that this code really does what we want, we can add a new user who doesn’t exist to the data records. You’ll see that the error gets reported in the log file, as shown in Figure 3; the application will be removed and restarted for the next iteration.

Page 15: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

Figure 3: Log results of a failed data-driven test script

Finally, the Data-Driven Test Script If you’ve followed all of the directions above, you have an XDE Tester script that you can run to demonstrate data-driven testing. Listing 5 is the final script after refactoring and with error handling added. This script loads test data from the three different kinds of data sources we discussed. import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.sql.Connection;

15

Page 16: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

16

import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import resources.ClassicsJavaA_CSV_DB_DPHelper; import resources.Customer; import com.rational.test.ft.object.TestObjectReference; import com.rational.test.ft.object.interfaces.ProcessTestObject; import com.rational.test.ft.sys.OSProcessInformation; import com.rational.test.tss.TSSDatapool; /** * Description : XDE Tester Script * @author Administrator */ public class ClassicsJavaA_CSV_DB_DP extends ClassicsJavaA_CSV_DB_DPHelper { /** * Script Name : <b>ClassicsJavaA</b> * Generated : <b>Sep 2, 2002 4:09:14 PM</b> * Modified : <b>Sep 2, 2002 4:09:14 PM</b> * Description : XDE Tester Script * Original Host : WinNT Version 5.0 Build 2195 (Service Pack 2) * * @since 2002/09/02 * @author Administrator */ ProcessTestObject p; TestObjectReference testObjectRef; OSProcessInformation osProcessInfo; public void testMain (Object[] args) { ArrayList customerList = new ArrayList(); // loadTestDataDB(customerList); // loadTestDataCSV(customerList); loadTestDataDP(customerList); int size = customerList.size(); for (int i=0;i<size;i++){ try{ mainTC(customerList,i); } catch (Exception e){ logError("Error: " + e.getMessage()); e.printStackTrace(System.out); if (!osProcessInfo.processNotAvailable) osProcessInfo.kill(); };

Page 17: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

17

}// End for. } protected void mainTC(ArrayList customerList,int i) { String logStr = new String(); logStr = ((Customer)customerList.get(i)).name + ", "; logStr += ((Customer)customerList.get(i)).password + ", "; logStr += ((Customer)customerList.get(i)).creditCardNr + ", "; logStr += ((Customer)customerList.get(i)).expDate + ", "; logStr += ((Customer)customerList.get(i)).quantity; logInfo(logStr); p = startApp("ClassicsJavaA"); testObjectRef = p.getObjectReference(); osProcessInfo = testObjectRef.getProcess(); // Frame: ClassicsCD tree2Tree().drag(atPath("Composers->Schubert"), atPoint(8,8), atRow(atPath("Composers->Schubert")), atPoint(8,8)); tree2Tree().drag(atPath("Composers->Schubert->Die schone Mullerin, Op. 25")); placeOrderButton2Button().click(); // Frame: Member Logon nameComboComboBox().click(); // nameComboComboBox().drag(atText("Trent Culpito")); nameComboComboBox().click(atText(((Customer)customerList.get(i)).name)); _passText2Text().drag(atPoint(53,8), atPoint(52,8)); // logFrame1Frame().inputKeys("nix"); logFrame1Frame().inputKeys(((Customer)customerList.get(i)).password); okorderlogonButton().click(); // Frame: Place an Order _quantityFieldText().drag(atPoint(20,10), atPoint(1,7)); // orderFormFrame().inputKeys("2"); orderFormFrame().inputKeys(((Customer)customerList.get(i)).quantity); _cardNumberFieldText().click(atPoint(15,7)); // orderFormFrame().inputKeys("1234567890"); orderFormFrame().inputKeys(((Customer)customerList.get(i)).creditCardNr); _expireFieldText().click(atPoint(24,11)); // orderFormFrame().inputKeys("4711"); orderFormFrame().inputKeys(((Customer)customerList.get(i)).expDate); placeOrderButton().click(); Yourorderhasbeenr_standardVP().performTest(); // OKButton().click(); // Frame: ClassicsCD ClassicsJavaFrame2(ANY,MAY_EXIT).close(); }

Page 18: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

18

protected void loadTestDataDP(ArrayList customerList) { Customer customer; try{ TSSDatapool dp = new TSSDatapool(); boolean fRetval = false; String sDPName = "Customer"; dp.open(sDPName); fRetval = dp.fetch(); while (fRetval){ customer = new Customer(); customer.name = dp.value("Name").toString(); customer.password = dp.value("Password").toString(); customer.creditCardNr = dp.value("CreditCardNr").toString(); customer.quantity = dp.value("Quantity").toString(); customer.expDate = dp.value("expDate").toString(); customerList.add(customer); System.out.println(customer.name); System.out.println(customer.password); System.out.println(customer.creditCardNr); System.out.println(customer.quantity); System.out.println(customer.expDate); fRetval = dp.fetch(); }; } catch(Exception e){ logError("Error: Datapool - " + e.getMessage()); e.printStackTrace(System.out); }; logInfo("DataPool loaded!"); } protected void loadTestDataCSV(ArrayList customerList) { String filename = System.getProperty("user.home")+File.separator + "Customer.csv"; String line; Customer customer; int startIndex; int endIndex; try{ BufferedReader in = new BufferedReader(new FileReader(filename)); while ((line=in.readLine()) != null){ customer = new Customer(); System.out.println(line); startIndex = 0; endIndex = line.indexOf(',',0); customer.name = line.substring(startIndex,endIndex);

Page 19: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

19

startIndex = endIndex + 1; endIndex = line.indexOf(',',startIndex); customer.password = line.substring(startIndex,endIndex); startIndex = endIndex + 1; endIndex = line.indexOf(',',startIndex); customer.creditCardNr = line.substring(startIndex,endIndex); startIndex = endIndex + 1; endIndex = line.indexOf(',',startIndex); customer.quantity = line.substring(startIndex,endIndex); startIndex = endIndex + 1; endIndex = line.indexOf(',',startIndex + 1); customer.expDate = line.substring(startIndex); customerList.add(customer); System.out.println(customer.name); System.out.println(customer.password); System.out.println(customer.creditCardNr); System.out.println(customer.quantity); System.out.println(customer.expDate); }; in.close(); } catch(IOException e){ logError("File Open Error!"); } logInfo("File-Data loaded!"); } protected void loadTestDataDB(ArrayList customerList) { Customer customer; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException e){ logError("Error: Failed to load JDBC:ODBC Driver!"); } try{ Connection con = DriverManager.getConnection("jdbc:odbc:customerdb","",""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT NAME,PASSWORD,CREDITCARDNR,QUANTITY,EXPDATE FROM CUSTOMERDATA"); while (rs.next()){ customer = new Customer(); customer.name = rs.getString("NAME"); customer.password = rs.getString("PASSWORD"); customer.creditCardNr = rs.getString("CREDITCARDNR"); customer.quantity = rs.getString("QUANTITY");

Page 20: Data Driven Testing: How to Create a Data Driven …...know how to start Access, create a new empty database, create a table, and import data from the CSV file. customers.mdb To use

20

customer.expDate = rs.getString("EXPDATE"); customerList.add(customer); System.out.println(customer.name); System.out.println(customer.password); System.out.println(customer.creditCardNr); System.out.println(customer.quantity); System.out.println(customer.expDate); }; rs.close(); stmt.close(); con.close(); } catch(SQLException sqle){ logError("Error: SQL Exception - " + sqle.getMessage()); sqle.printStackTrace(System.out); } logInfo("DB-Data loaded!"); } }

Listing 5: Our sample XDE Tester script, revised to implement data-driven testing

About the Author Dr. Gerd Weishaar is a technology evangelist for the automated quality assurance group (ASQG) of IBM Rational Software. He works for worldwide technical marketing and is responsible for communication and information exchange among product managers, product marketing managers, field sales staff, and customers. Before joining Rational in 1999, he worked for a software engineering research laboratory at Kepler University in Linz, Austria. During this time he conducted several software projects for the steel industry and for a vendor of load-testing tools. Dr. Weishaar welcomes feedback on this article and can be reached by e-mail.