[Webtest] New step: "sleep"

John Willemin webtest@lists.canoo.com
Fri, 02 May 2003 16:41:16 -0400


Hey - Been using Canoo for about a week now and there was something I had 
to add in order to test our webapp. So in the spirit of open source, I'm 
throwing it out for criticism and in the hopes someone else will find it 
useful.

In our web application, a person runs a query, and these queries always 
take 2 minutes to complete. So rather than aimlessly refresh for 2 minutes, 
I just wanted to write a sleep task to have the tests wait until the query 
would be complete. So, here it is. It gets called like this:

                 <sleep stepid="wait for completion"
                     sleepTime="20000" />

Where sleepTime is the amount of time to sleep, in milliseconds. It 
probably makes more sense to have that in seconds, and sleepTime could be 
renamed "seconds" to make things more intuitive. That's easy to change, so 
if that's better, I can send out "version 2" :)

Tell me what you think. It might be that I'm the only person who finds this 
useful.

Thanks,
John

 >>>>>>>>>
Changes to TestStepSequence.java - I just had to add this:
 >>>>>>>>>

     public void addSleep(Sleep step) {
         step.setStepType("sleep");
         addStep(step);
     }


 >>>>>>>>>>>
com.canoo.webtest.steps.Sleep.java
 >>>>>>>>>>>


package com.canoo.webtest.steps;

import com.canoo.webtest.engine.Context;
import com.canoo.webtest.engine.StepExecutionException;

import java.util.HashMap;

/**
  *  The sleep task is pretty simple. All it does is call Thread.sleep() 
for a specified time so that your webapp
  *   can finish what it's doing.
  *
  * @author John Willemin
  */
public class Sleep
     extends Step
     {

     /**
      * This is how long we're sleeping for. Has to be an object, can't be 
a primitive, since it's getting put
      *  into a HashMap later.
      */
     private Integer fSleepTime = null;

     public void setSleepTime(String sleepTime)
     {
         fSleepTime = new Integer( Integer.parseInt( sleepTime ) );
     }

     public void doExecute(Context context)
         throws StepExecutionException
     {
         logText(context, " sleeping for " + fSleepTime.intValue() );

         verifyParameters();
         try
         {
             Thread.sleep( fSleepTime.longValue() );
         }
         catch( InterruptedException ie )
         {
             // can't do much here
         }
     }

     public void expandProperties()
     {
         super.expandProperties();
     }

     protected void verifyParameters()
     {
         if(fSleepTime == null)
         {
             throw new StepExecutionException("Attribute sleepTime must be 
set");
         }
     }

     /**
      * Collect parameters for reporting. Our our parameters to the ones 
obtained
      * from super.
      *
      * @return a HashMap containing paramter names (key) and their associated
      * values
      */
     public HashMap getParameterDictionary()
      {
          HashMap map = super.getParameterDictionary();
          if(fSleepTime != null)
          {
              map.put("sleepTime", fSleepTime);
          }
          return map;
      }
}