001: // Copyright © 2004-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.extension.dialogs;
003:
004: import org.apache.log4j.Logger;
005:
006: import com.canoo.webtest.engine.Context;
007: import com.canoo.webtest.steps.Step;
008: import com.gargoylesoftware.htmlunit.AlertHandler;
009: import com.gargoylesoftware.htmlunit.ConfirmHandler;
010: import com.gargoylesoftware.htmlunit.PromptHandler;
011: import com.gargoylesoftware.htmlunit.WebClient;
012:
013: /**
014: * Saves away a JavaScript dialog response in preparation for a subsequent automatic user response.<p>
015: *
016: * @author Paul King, ASERT
017: * @webtest.step category="Extension"
018: * name="expectDialog"
019: * alias="prepareDialogResponse"
020: * description="Provides the ability to set expectations in relation to user responses to <key>javascript</key> Dialog boxes (Alert, Confirm and Prompt dialogs)."
021: */
022: public class ExpectDialog extends Step {
023: private String fSaveProperty;
024: private String fSavePropertyType;
025: private String fResponse;
026: private String fDialogType;
027: private static final Logger LOG = Logger
028: .getLogger(ExpectDialog.class);
029:
030: /**
031: * The name of the property used to save dialog text
032: *
033: * @param value The new saveProperty value
034: * @webtest.parameter required="no"
035: * description="The name of the property in which to store the dialog text for later checking with \"verifyProperty\"."
036: */
037: public void setSaveProperty(final String value) {
038: fSaveProperty = value;
039: }
040:
041: public String getSaveProperty() {
042: return fSaveProperty;
043: }
044:
045: /**
046: * The type of the property used to save dialog text (ant or dynamic)
047: *
048: * @param value The new savePropertyType value
049: * @webtest.parameter required="no"
050: * description="The type of the property in which to store the dialog text for later checking. Either \"ant\" or \"dynamic\"."
051: * default="the \"defaultPropertyType\" as specified in the \"config\" element is used."
052: */
053: public void setSavePropertyType(final String value) {
054: fSavePropertyType = value;
055: }
056:
057: public String getSavePropertyType() {
058: return fSavePropertyType;
059: }
060:
061: /**
062: * The type of JavaScript dialog (alert, confirm or prompt)
063: *
064: * @param value The new dialogType value
065: * @webtest.parameter required="no"
066: * default="\"alert\""
067: * description="One of \"alert\", \"confirm\" or \"prompt\"."
068: */
069: public void setDialogType(final String value) {
070: fDialogType = value;
071: }
072:
073: public String getDialogType() {
074: return fDialogType;
075: }
076:
077: /**
078: * The response value returned to the JavaScript
079: *
080: * @param value The new response value
081: * @webtest.parameter required="no"
082: * description="simulate user response: ignored for alerts, converted to boolean for confirms - \"OK\" (true) or \"Cancel\" (false), contains typed text for prompts."
083: * default="true"
084: */
085: public void setResponse(final String value) {
086: fResponse = value;
087: }
088:
089: public String getResponse() {
090: return fResponse;
091: }
092:
093: public void doExecute() {
094: final Context context = getContext();
095: final WebClient wc = context.getWebClient();
096:
097: final AbstractDialogStep dialogStep;
098: if ("confirm".equalsIgnoreCase(getDialogType())) {
099: dialogStep = new ConfirmDialogStep("true"
100: .equals(getResponse()), null, null,
101: getSaveProperty(), getSavePropertyType());
102: } else if ("prompt".equalsIgnoreCase(getDialogType())) {
103: dialogStep = new PromptDialogStep(getResponse(), null,
104: null, getSaveProperty(), getSavePropertyType());
105: } else { // alert is default
106: dialogStep = new AlertDialogStep(null, null,
107: getSaveProperty(), getSavePropertyType());
108: }
109: DialogHelper.addExpectedDialog(context, dialogStep);
110: final Object handler = new ExpectDialogs.CheckingDialogHandler(
111: context, this );
112:
113: wc.setAlertHandler((AlertHandler) handler);
114: wc.setConfirmHandler((ConfirmHandler) handler);
115: wc.setPromptHandler((PromptHandler) handler);
116: LOG.debug("Dialog expectation saved - now expect "
117: + DialogHelper.getExpectedDialogsCount(context)
118: + " dialog(s).");
119:
120: }
121:
122: }
|