001: package com.canoo.webtest.steps.request;
002:
003: import org.apache.commons.lang.StringUtils;
004: import org.apache.log4j.Logger;
005:
006: import com.canoo.webtest.boundary.HtmlUnitBoundary;
007: import com.canoo.webtest.engine.StepFailedException;
008: import com.canoo.webtest.steps.Step;
009: import com.gargoylesoftware.htmlunit.ElementNotFoundException;
010: import com.gargoylesoftware.htmlunit.html.HtmlElement;
011: import com.gargoylesoftware.htmlunit.html.HtmlPage;
012:
013: /**
014: * Drags an element to an other one
015: *
016: * @author Marc Guillemot
017: * @webtest.step category="Extension"
018: * name="dragAndDrop"
019: * description="Drags an element to an other one"
020: */
021: public class DragAndDrop extends Step {
022: private static final Logger LOG = Logger
023: .getLogger(DragAndDrop.class);
024: private String fFromXPath_;
025: private String fToXPath_;
026: private String fFromHtmlId_;
027: private String fToHtmlId_;
028:
029: /**
030: * @return the fFromXPath.
031: */
032: public String getFromXPath() {
033: return fFromXPath_;
034: }
035:
036: /**
037: * @param _fromXPath the fFromXPath to set
038: * @webtest.parameter
039: * required="yes/no"
040: * description="The xpath allowing to select the element to drag.
041: * One of fromXPath and fromHtmlId is required."
042: */
043: public void setFromXPath(final String _fromXPath) {
044: fFromXPath_ = _fromXPath;
045: }
046:
047: /**
048: * @return the fToXPath.
049: */
050: public String getToXPath() {
051: return fToXPath_;
052: }
053:
054: /**
055: * @param _toXPath the fToXPath to set
056: * @webtest.parameter
057: * required="yes/no"
058: * description="The xpath allowing to select the element to drop on.
059: * One of toXPath and toHtmlId is required."
060: */
061: public void setToXPath(final String _toXPath) {
062: fToXPath_ = _toXPath;
063: }
064:
065: /**
066: * @return the fFromHtmlId.
067: */
068: public String getFromHtmlId() {
069: return fFromHtmlId_;
070: }
071:
072: /**
073: * @param _fromHtmlId the fFromHtmlId to set
074: * @webtest.parameter
075: * required="yes/no"
076: * description="The id of the html element to drag.
077: * One of fromXPath and fromHtmlId is required."
078: */
079: public void setFromHtmlId(final String _fromHtmlId) {
080: fFromHtmlId_ = _fromHtmlId;
081: }
082:
083: /**
084: * @return the fToHtmlId.
085: */
086: public String getToHtmlId() {
087: return fToHtmlId_;
088: }
089:
090: /**
091: * @param _toHtmlId the fToHtmlId to set
092: * @webtest.parameter
093: * required="yes/no"
094: * description="The id of the html element to drop on.
095: * One of toXPath and toHtmlId is required."
096: */
097: public void setToHtmlId(final String _toHtmlId) {
098: fToHtmlId_ = _toHtmlId;
099: }
100:
101: public void doExecute() throws Exception {
102: final HtmlPage page = getContext().getCurrentHtmlResponse(this );
103: final HtmlElement from = getByXPathOrId("From element", page,
104: getFromXPath(), getFromHtmlId());
105: final HtmlElement to = getByXPathOrId("To element", page,
106: getToXPath(), getToHtmlId());
107:
108: from.mouseDown();
109: to.mouseMove();
110: to.mouseUp();
111: }
112:
113: private HtmlElement getByXPathOrId(final String description,
114: final HtmlPage page, final String xpath, String htmlId) {
115: final HtmlElement elt;
116: if (!StringUtils.isEmpty(htmlId)) {
117: try {
118: elt = page.getHtmlElementById(htmlId);
119: } catch (final ElementNotFoundException e) {
120: throw new StepFailedException(description
121: + ": no element found with id >" + htmlId + "<");
122: }
123: } else {
124: final Object node = HtmlUnitBoundary
125: .trySelectSingleNodeByXPath(xpath, page, this );
126: LOG.debug("XPath >" + xpath + "< evaluates to: " + node);
127: if (node == null) {
128: throw new StepFailedException(description
129: + ": no element found with xpath >" + xpath
130: + "<");
131: }
132: if (node instanceof HtmlElement) {
133: elt = (HtmlElement) node;
134: } else {
135: throw new StepFailedException(
136: "The xpath doesn't select an HTML element but a '"
137: + node.getClass() + "'");
138: }
139: }
140:
141: return elt;
142: }
143:
144: protected void verifyParameters() {
145: nullResponseCheck();
146: paramCheck(getFromHtmlId() == null && getFromXPath() == null,
147: "\"fromHtmlId\" or \"fromXPath\" must be set!");
148: paramCheck(getFromHtmlId() != null && getFromXPath() != null,
149: "Only one from \"fromHtmlId\" and \"fromXPath\" can be set!");
150:
151: paramCheck(getToHtmlId() == null && getToXPath() == null,
152: "\"toHtmlId\" or \"toXPath\" must be set!");
153: paramCheck(getToHtmlId() != null && getToXPath() != null,
154: "Only one from \"toHtmlId\" and \"toXPath\" can be set!");
155: }
156: }
|