Control loops - PlantsByWebSphere example

 

+

Search Tips   |   Advanced Search

 

This example demonstrates extending test execution by using custom code to control loops.

This example uses a tree purchase transaction using IBM's PlantsByWebSphere application. The test begins with a recording of a tree purchase transaction, using datapool substitution for the login IDs.

The pages are wrapped in a five-iteration loop...

The first piece of custom code, InitializeBuyTest, contains...

// InitializeBuyTest

package test;

import java.util.Random;

import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.services.IVirtualUserInfo;

public class InitializeBuyTest 
    implements com.ibm.rational.test.lt.kernel.custom.ICustomCode2 
{

  public InitializeBuyTest() 
  {
  }

  public String exec(ITestExecutionServices tes, String[] args) 
  {
    // Get the test's data area and set a flag indicating that nothing has failed yet. 
    // 
    // This flag will be used later to break out of the schedule loop as soon as a 
    // failure is encountered.

    IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
    dataArea.put("failedYet", "false");

    // Get the virtual users's data area
    IDataArea vda = tes.findDataArea(IDataArea.VIRTUALUSER);
    
    // Randomly select a tree to purchase from the set of T0001 to T0005

    IVirtualUserInfo vuInfo = (IVirtualUserInfo) vda.get(IVirtualUserInfo.KEY);
    Random rand = vuInfo.getRandom();
    String tree = "T000" + Integer.toString(rand.nextInt(5));

    // Persist the name of the tree in the virtual user's data area.
    vda.put("myTree", tree);

    return tree;
  }
}

First, the data area for the test is acquired to store a flag value, in this case a string of text, to be used later to stop the test loop when an error is discovered. Data stored in this way can be persisted across tests.

Then a randomly generated tree string is created. The value is stored as the variable tree, and is passed back as the return value for the method. This return value is used as a substitute in a request later...

The highlighted item uses a substitution (s%3A716), which is the value returned by the InitializeBuyTest custom code item. We are using custom code to drive the direction of our test.

The next lines of code in InitializeBuyTest use the Virtual User data area to store the name of the tree for later reference. Again, data stored in this way can persist across tests.

The second piece of custom code is called CheckTree. Its contents are as follows (listing only the exec() method this time):

package test;

import org.eclipse.hyades.test.common.event.VerdictEvent;

import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.services.ITestInfo;
import com.ibm.rational.test.lt.kernel.services.ITestLogManager;

public class CheckTree 
    implements com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {


  public CheckTree() {
  }

  public String exec(ITestExecutionServices tes, String[] args) 
  {

      // Get the actual and requested tree purchased.
    
      String actualTree = args[0];
      String requestedTree = args[1];

      // Optional: Print arguments to file
      for (int i = 0; i < args.length; i++)
          outFile.write("Argument " + i + " is: " + args[i] + "\n");

      // Set the log level to ALL.
      IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
      ITestInfo testInfo = (ITestInfo)dataArea.get(ITestInfo.KEY);
      testInfo.setTestLogLevel(ITestLogManager.ALL);

      // Report the actual and requested tree purchased.
      ITestLogManager testLogManager = tes.getTestLogManager();

      if (testLogManager.wouldReport(ITestLogManager.ALL)) 
      {
         testLogManager.reportMessage("Actual tree purchased: "
                                     + actualTree 
                                     + ". Requested tree: " 
                                     + requestedTree
                                     + ".");
      }      

      // If the actual and requested tree don't match, submit a FAIL verdict.
      if (testLogManager.wouldReport(ITestLogManager.ALL)) 
      { 
        if (!actualTree.equalsIgnoreCase(requestedTree)) 
        {
            testLogManager.reportVerdict("Actual and requested purchase tree do not match.",
                                          VerdictEvent.VERDICT_FAIL);

          // Use the test's data area to record the fact that an error has occurred.
          dataArea.put("failedYet", "true");
        }     
      }

      return null;
  }
}

Use a reference created in response of the original recording.

The reference is passed into CheckTree as an argument. String manipulation is needed parse the text of interest, in this case, the name of the tree that was actually purchased.

Select CheckTree code, and then in the Arguments section, select Add.

Check boxes to enable new reference and InitializeBuyTest custom code as arguments.

Press Select.

The CheckTree custom code item uses these values to verify that the randomly chosen tree generated by InitializeBuyTest is actually purchased during the execution of the test.

CheckTree then sets the test log level, reports the actual and requested tree purchase, and raises a FAIL verdict if they do not match. CheckTree also stores a true value associated with the tag failedYet in the test's data area.

The final piece of code uses the test's data area to determine the user-defined value associated with the tag failedYet. If failedYet is true, StopLoopCheck breaks out of the test loop.

package test;

import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.services.ITestLogManager;

public class StopLoopCheck implements com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

  public StopLoopCheck() {
  }

  public String exec(ITestExecutionServices tes, String[] args) 
  {

      // Get the test log manager

      ITestLogManager testLogManager = tes.getTestLogManager();
      
      // Get success-failure flag from data area. 
      // If anything has failed, stop the loop.

      IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
      String failedYet = (String) dataArea.get("failedYet");

      // Break out of the loop if an error has been encountered.

      if (failedYet.equalsIgnoreCase("true")) 
      {
        tes.getLoopControl().breakLoop();

        if (testLogManager.wouldReport(ITestLogManager.ALL)) 
        {
          testLogManager.reportMessage("Loop stopped.");
        }
      }

      return null;
  }
    }
}