001: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.engine;
003:
004: import java.util.Collections;
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: import org.apache.log4j.Logger;
009:
010: import com.canoo.webtest.ant.WebtestTask;
011: import com.canoo.webtest.boundary.ResetScriptRunner;
012: import com.canoo.webtest.engine.xpath.XPathHelper;
013: import com.canoo.webtest.steps.Step;
014: import com.gargoylesoftware.htmlunit.Page;
015: import com.gargoylesoftware.htmlunit.WebClient;
016: import com.gargoylesoftware.htmlunit.html.HtmlForm;
017: import com.gargoylesoftware.htmlunit.html.HtmlPage;
018:
019: /**
020: * Represents context information for a webtest.
021: *
022: * @author unknown
023: * @author Marc Guillemot
024: * @author Paul King
025: */
026: public class Context {
027: private static final Logger LOG = Logger.getLogger(Context.class);
028: /**
029: * The name identifiying the default {@link WebClientContext} that is the current one at test startup.
030: */
031: public static final String KEY_DEFAULT_WEBCLIENTCONTEXT = "default";
032:
033: private int fCurrentStepIndex;
034: private WebtestTask fWebtest;
035: private boolean fPrepared;
036: private Map fContextStore = new HashMap();
037: private ResetScriptRunner fSavedRunner;
038:
039: private final XPathHelper fXPathHelper = new XPathHelper();
040:
041: private WebClientContext fCurrentWebClientContext = new WebClientContext();
042: private final Map fWebClientContexts = new HashMap();
043:
044: public Context(final WebtestTask webtest) {
045: fWebtest = webtest;
046: fWebClientContexts.put(KEY_DEFAULT_WEBCLIENTCONTEXT,
047: fCurrentWebClientContext);
048: }
049:
050: public boolean containsKey(final String key) {
051: return fContextStore.containsKey(key);
052: }
053:
054: public Object get(final String key) {
055: return fContextStore.get(key);
056: }
057:
058: /**
059: * Shortcut method to get the configuration of the
060: *
061: * @return the configuration
062: * @see WebtestTask
063: */
064: public Configuration getConfig() {
065: return getWebtest().getConfig();
066: }
067:
068: /**
069: * Delegates to current {@link WebClientContext}
070: *
071: * @see #getCurrentWebClientContext()
072: * @see WebClientContext#getCurrentForm
073: */
074: public HtmlForm getCurrentForm() {
075: return getCurrentWebClientContext().getCurrentForm();
076: }
077:
078: /**
079: * /**
080: * Delegates to current {@link WebClientContext}
081: *
082: * @see #getCurrentWebClientContext()
083: * @see WebClientContext#getCurrentHtmlResponse(Step)
084: */
085: public HtmlPage getCurrentHtmlResponse(final Step step) {
086: return getCurrentWebClientContext()
087: .getCurrentHtmlResponse(step);
088: }
089:
090: /**
091: * Delegates to current {@link WebClientContext}
092: *
093: * @see #getCurrentWebClientContext()
094: * @see WebClientContext#getCurrentResponse()
095: */
096: public Page getCurrentResponse() {
097: return getCurrentWebClientContext().getCurrentResponse();
098: }
099:
100: /**
101: * Delegates to current {@link WebClientContext}
102: *
103: * @see #getCurrentResponseFile()
104: * @see WebClientContext#getCurrentResponseFile()
105: */
106: public String getCurrentResponseFile() {
107: return getCurrentWebClientContext().getCurrentResponseFile();
108: }
109:
110: public int getCurrentStepNumber() {
111: return fCurrentStepIndex + 1;
112: }
113:
114: /**
115: * Gets the currently active {@link WebClientContext}.
116: *
117: * @return the web client context
118: */
119: public WebClientContext getCurrentWebClientContext() {
120: return fCurrentWebClientContext;
121: }
122:
123: public int getNumberOfSteps() {
124: return getWebtest().getStepSequence().getSteps().size();
125: }
126:
127: /**
128: * Gets the currently available {@link WebClientContext}
129: *
130: * @return an unmodifiable Map of (key, {@link WebClientContext})
131: */
132: public Map getWebClientContexts() {
133: return Collections.unmodifiableMap(fWebClientContexts);
134: }
135:
136: /**
137: * Defines the current {@link WebClientContext} creating it if none was previously registered
138: * under this name.
139: *
140: * @param name the name of the {@link WebClientContext} to activate.
141: * The default {@link WebClientContext} is registered under the key {@link #KEY_DEFAULT_WEBCLIENTCONTEXT}.
142: * @return the new current context
143: */
144: public WebClientContext defineCurrentWebClientContext(
145: final String name) {
146: WebClientContext webClientContext = (WebClientContext) fWebClientContexts
147: .get(name);
148: if (webClientContext == null) {
149: webClientContext = new WebClientContext();
150: webClientContext
151: .setWebClient(getConfig().createWebClient());
152: fWebClientContexts.put(name, webClientContext);
153: LOG.info("Created new WebClientContext for \"" + name
154: + "\"");
155: }
156: fCurrentWebClientContext = webClientContext;
157: return webClientContext;
158: }
159:
160: /**
161: * Delegates to current {@link WebClientContext}
162: *
163: * @see #getCurrentWebClientContext()
164: * @see WebClientContext#getResponses()
165: */
166: public WebClientContext.StoredResponses getResponses() {
167: return getCurrentWebClientContext().getResponses();
168: }
169:
170: public ResetScriptRunner getRunner() {
171: return fSavedRunner;
172: }
173:
174: /**
175: * Delegates to current {@link WebClientContext}
176: *
177: * @see #getCurrentWebClientContext()
178: * @see WebClientContext#getSavedPassword()
179: */
180: public String getSavedPassword() {
181: return getCurrentWebClientContext().getSavedPassword();
182: }
183:
184: /**
185: * Delegates to current {@link WebClientContext}
186: *
187: * @see #getCurrentWebClientContext()
188: * @see WebClientContext#getSavedUserName()
189: */
190: public String getSavedUserName() {
191: return getCurrentWebClientContext().getSavedUserName();
192: }
193:
194: /**
195: * Delegates to current {@link WebClientContext}
196: *
197: * @see #getCurrentWebClientContext()
198: * @see WebClientContext#getWebClient()
199: */
200: public WebClient getWebClient() {
201: return getCurrentWebClientContext().getWebClient();
202: }
203:
204: /**
205: * Gets the <webtest> for this context.
206: *
207: * @return the task
208: */
209: public WebtestTask getWebtest() {
210: return fWebtest;
211: }
212:
213: /**
214: * Gets the XPath helper used for this test. It will be responsible to
215: * create the xpath objects with the right custom functions and variables.
216: *
217: * @return the helper
218: */
219: public XPathHelper getXPathHelper() {
220: return fXPathHelper;
221: }
222:
223: public void increaseStepNumber() {
224: fCurrentStepIndex += 1;
225: }
226:
227: public boolean isPrepared() {
228: return fPrepared;
229: }
230:
231: // generic mechanism to extend context information
232: public void put(final String key, final Object value) {
233: LOG.debug("put(" + key + ", " + value + ")");
234: fContextStore.put(key, value);
235: }
236:
237: public void remove(final String key) {
238: fContextStore.remove(key);
239: }
240:
241: /**
242: * Delegates to current {@link WebClientContext}
243: *
244: * @see #getCurrentWebClientContext()
245: * @see WebClientContext#restorePreviousResponse()
246: */
247: public void restorePreviousResponse() {
248: getCurrentWebClientContext().restorePreviousResponse();
249: }
250:
251: /**
252: * Delegates to current {@link WebClientContext}
253: *
254: * @see #getCurrentWebClientContext()
255: * @see WebClientContext#restoreResponses(com.canoo.webtest.engine.WebClientContext.StoredResponses)
256: */
257: public void restoreResponses(
258: final WebClientContext.StoredResponses savedResponses) {
259: getCurrentWebClientContext().restoreResponses(savedResponses);
260: }
261:
262: /**
263: * Delegates to current {@link WebClientContext}
264: *
265: * @see #getCurrentWebClientContext()
266: * @see WebClientContext#restoreWindowListener()
267: */
268: public void restoreWindowListener() {
269: getCurrentWebClientContext().restoreWindowListener();
270: }
271:
272: /**
273: * Delegates to current {@link WebClientContext}
274: *
275: * @see #getCurrentWebClientContext()
276: * @see WebClientContext#saveResponseAsCurrent(Page)
277: */
278: public void saveResponseAsCurrent(final Page page) {
279: getCurrentWebClientContext().saveResponseAsCurrent(page);
280: }
281:
282: /**
283: * Delegates to current {@link WebClientContext}
284: *
285: * @see #getCurrentWebClientContext()
286: * @see WebClientContext#setCurrentForm(HtmlForm)
287: */
288: public void setCurrentForm(final HtmlForm form) {
289: getCurrentWebClientContext().setCurrentForm(form);
290: }
291:
292: public void setPrepared(final boolean prepared) {
293: fPrepared = prepared;
294: }
295:
296: public void setRunner(final ResetScriptRunner sr) {
297: fSavedRunner = sr;
298: }
299:
300: /**
301: * Delegates to current {@link WebClientContext}
302: *
303: * @see #getCurrentWebClientContext()
304: * @see WebClientContext#setSavedPassword(String)
305: */
306: public void setSavedPassword(final String password) {
307: getCurrentWebClientContext().setSavedPassword(password);
308: }
309:
310: /**
311: * Delegates to current {@link WebClientContext}
312: *
313: * @see #getCurrentWebClientContext()
314: * @see WebClientContext#setSavedUserName(String)
315: */
316: public void setSavedUserName(final String userName) {
317: getCurrentWebClientContext().setSavedUserName(userName);
318: }
319:
320: /**
321: * Delegates to current {@link WebClientContext}
322: *
323: * @see #getCurrentWebClientContext()
324: * @see WebClientContext#setWebClient(WebClient)
325: */
326: public void setWebClient(final WebClient webClient) {
327: getCurrentWebClientContext().setWebClient(webClient);
328: }
329:
330: /**
331: * Delegates to current {@link WebClientContext}
332: *
333: * @see #getCurrentWebClientContext()
334: * @see WebClientContext#suspendWindowListener()
335: */
336: public void suspendWindowListener() {
337: getCurrentWebClientContext().suspendWindowListener();
338: }
339: }
|