View Javadoc

1   package com.flexiblewebsolutions.xdriveunit;
2   
3   import java.io.File;
4   
5   import org.apache.log4j.Logger;
6   
7   import com.flexiblewebsolutions.io.util.FileUtils;
8   
9   /***
10   * XML Test Case that can be used to Load Test Web applications.
11   * 
12   * @author Donavon Buss
13   */
14  public abstract class LoadWebXDriveTestCase extends WebXDriveTestCase {
15      static Logger logger = Logger.getLogger( LoadWebXDriveTestCase.class.getName() );
16  	/*** Measure the amount of time between button click and return page. */
17  	private boolean _TrackTime = false;
18  
19  	/*** Specify delay between page hits. */
20  	private DelayTimer _ClickTimer = null;
21  
22  	/*** Keeps track of page results */
23  	private StringBuffer _PerformanceStats = null;
24  
25  	/***
26  	 * Initialize the LoadWebXMLTestCase object with required parameters.
27  	 * 
28  	 * @see XDriveTestCase:XDriveTestCase(String pName, StringBuffer
29  	 *      pTestInput, StringBuffer pTestOptions, String pThreadName)
30  	 */
31  	public LoadWebXDriveTestCase(String pName, StringBuffer pTestInput,
32  			StringBuffer pTestOptions, String pThreadName) {
33  		super(pName, pTestInput, pTestOptions, pThreadName);
34  		initLoadWebTestOptions();
35  	}
36  
37  	private void setClickTimer( DelayTimer pTimer )
38  	{
39  	    _ClickTimer = pTimer;
40  	}
41  	
42  	private DelayTimer getClickTimer()
43  	{
44  	    if( _ClickTimer == null )
45  	    {
46  	        _ClickTimer = new DelayTimer();
47  	    }
48  	    return( _ClickTimer );
49  	}
50  	
51  	/***
52  	 * Setup properties that are defined as belonging to web load test
53  	 *  
54  	 */
55  	private void initLoadWebTestOptions() {
56  	    logger.debug( "initLoadWebTestOptions" );
57  		// handle click timer
58  		String clickTimerVal = (String) getTestOptions().get("clickDelay");
59  		if (clickTimerVal != null) {
60  		    LoadTestUtils lt = new LoadTestUtils();
61  			setClickTimer( lt.initClickTimer(clickTimerVal) );
62  			lt = null;
63  		}
64  		// handle stats
65  		_TrackTime = checkBooleanParameter("trackTime");
66  	}
67  
68  	
69  
70  	/***
71  	 * Appends start and end time of link to stored information.
72  	 * 
73  	 * @param pStart -
74  	 *            Before link was clicked
75  	 * @param pLinkName -
76  	 *            Link that was clicked
77  	 * @param pLinkDesc -
78  	 *            Description of link
79  	 */
80  	private void recordDuration(long pStart, String pLinkName, String pLinkDesc) {
81  		if (_TrackTime) {
82  			long endTime = System.currentTimeMillis();
83  			long duration = endTime - pStart;
84  
85  			_PerformanceStats.append("<click>\n");
86  			_PerformanceStats.append("<link>");
87  			_PerformanceStats.append(pLinkName);
88  			_PerformanceStats.append("</link>\n");
89  			_PerformanceStats.append("<pageDesc>");
90  			_PerformanceStats.append(pLinkDesc);
91  			_PerformanceStats.append("</pageDesc>\n");
92  			_PerformanceStats.append("<duration>");
93  			_PerformanceStats.append(duration);
94  			_PerformanceStats.append("</duration>\n");
95  			_PerformanceStats.append("</click>\n");
96  		}
97  	}
98  
99  	/***
100 	 * @see junit.framework.TestCase#setUp()
101 	 */
102 	protected void setUp() throws Exception {
103 		_PerformanceStats = new StringBuffer();
104 		_PerformanceStats.append("<clickTimes>\n");
105 		super.setUp();
106 	}
107 
108 	/***
109 	 * @see junit.framework.TestCase#tearDown()
110 	 */
111 	public void tearDown() throws Exception {
112 		super.tearDown();
113 		// Creates statistics file if option was set.
114 
115 		if (_TrackTime) {
116 			_PerformanceStats.append("</clickTimes>\n");
117 
118 			String methodName = getName();
119 			File outFile = new File(getTestTmpDirectory(), "STATS_"
120 					+ _ThreadName + methodName + ".xml");
121 			new FileUtils().writeFile(outFile, _PerformanceStats);
122 		}
123 	}
124 
125 	/***
126 	 * Clicks on a button after a set amount of time, records time taken for
127 	 * link to return
128 	 * 
129 	 * @param pButtonID -
130 	 *            ID of link
131 	 * @param pButtonDesc -
132 	 *            Text description of link
133 	 */
134 	protected void clickButton(String pButtonID, String pButtonDesc) {
135 	    logger.debug( "clickButton: " + pButtonID );
136 		getClickTimer().clickDelay();
137 		long startTime = System.currentTimeMillis();
138 
139 		super.clickButton(pButtonID, pButtonDesc);
140 		recordDuration(startTime, pButtonID, pButtonDesc);
141 	}
142 
143 	/***
144 	 * Clicks on a button after a set amount of time, records time taken for
145 	 * link to return
146 	 * 
147 	 * @see com.flexiblewebsolutions.xdriveunit.WebXDriveTestCase#clickLinkWithImage(java.lang.String,
148 	 *      java.lang.String)
149 	 */
150 	protected void clickLinkWithImage(String pLinkImg, String pLinkDesc) {
151 	    logger.debug( "clickLinkWithImage: " +pLinkImg );
152 		getClickTimer().clickDelay();
153 		long startTime = System.currentTimeMillis();
154 
155 		super.clickLinkWithImage(pLinkImg, pLinkDesc);
156 		recordDuration(startTime, pLinkImg, pLinkDesc);
157 	}
158 
159 	/***
160 	 * Clicks on a button after a set amount of time, records time taken for
161 	 * link to return
162 	 * 
163 	 * @see com.flexiblewebsolutions.xdriveunit.WebXDriveTestCase#clickLinkWithText(java.lang.String,
164 	 *      int, java.lang.String)
165 	 */
166 	protected void clickLinkWithText(String pLinkText, int pIndex,
167 			String pLinkDesc) {
168 	    logger.debug( "clickLinkWithText: " + pLinkText );
169 		getClickTimer().clickDelay();
170 		long startTime = System.currentTimeMillis();
171 
172 		super.clickLinkWithText(pLinkText, pIndex, pLinkDesc);
173 		recordDuration(startTime, pLinkText, pLinkDesc);
174 	}
175 
176 	/***
177 	 * Clicks on a button after a set amount of time, records time taken for
178 	 * link to return
179 	 * 
180 	 * @see com.flexiblewebsolutions.xdriveunit.WebXDriveTestCase#clickLinkWithText(java.lang.String,
181 	 *      java.lang.String)
182 	 */
183 	protected void clickLinkWithText(String pLinkText, String pLinkDesc) {
184 	    logger.debug( "clickLinkWithText: " + pLinkText );
185 		getClickTimer().clickDelay();
186 		long startTime = System.currentTimeMillis();
187 
188 		super.clickLinkWithText(pLinkText, pLinkDesc);
189 		recordDuration(startTime, pLinkText, pLinkDesc);
190 	}
191 
192 	/***
193 	 * Clicks on a button after a set amount of time, records time taken for
194 	 * link to return
195 	 * 
196 	 * @see com.flexiblewebsolutions.xdriveunit.WebXDriveTestCase#submit(java.lang.String,
197 	 *      java.lang.String)
198 	 */
199 	protected void submit(String pFormName, String pSubmitDesc) {
200 	    logger.debug( "submit: " + pFormName );
201 		getClickTimer().clickDelay();
202 		long startTime = System.currentTimeMillis();
203 
204 		super.submit(pFormName, pSubmitDesc);
205 		recordDuration(startTime, "SUBMIT - " + pFormName, pSubmitDesc);
206 	}
207 
208 	/***
209 	 * Clicks on a button after a set amount of time, records time taken for
210 	 * link to return
211 	 * 
212 	 * @see com.flexiblewebsolutions.xdriveunit.WebXDriveTestCase#submit(java.lang.String)
213 	 */
214 	protected void submit(String pSubmitDesc) {
215 	    logger.debug( "submit: " + pSubmitDesc );
216 		getClickTimer().clickDelay();
217 		long startTime = System.currentTimeMillis();
218 
219 		super.submit(pSubmitDesc);
220 		recordDuration(startTime, "SUBMIT", pSubmitDesc);
221 	}
222 }