001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.request;
003:
004: import java.util.ArrayList;
005: import java.util.Collections;
006: import java.util.List;
007:
008: import com.canoo.webtest.engine.StepExecutionException;
009: import com.canoo.webtest.engine.StepFailedException;
010: import com.canoo.webtest.self.TestBlock;
011: import com.canoo.webtest.self.ThrowAssert;
012: import com.canoo.webtest.steps.BaseStepTestCase;
013: import com.canoo.webtest.steps.Step;
014: import com.gargoylesoftware.htmlunit.AlertHandler;
015: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
016: import com.gargoylesoftware.htmlunit.WebClient;
017: import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
018: import com.gargoylesoftware.htmlunit.html.HtmlPage;
019:
020: /**
021: * Test cases for {@link ClickLink}.
022: *
023: * @author unknown
024: * @author Marc Guillemot
025: * @author Paul King, ASERT
026: * @author Denis N. Antonioli
027: */
028: public class ClickLinkTest extends BaseStepTestCase {
029: private static final String NBSP = "\u00a0";
030: private ClickLink fStep;
031:
032: protected Step createStep() {
033: return new ClickLink();
034: }
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038: fStep = (ClickLink) getStep();
039: }
040:
041: public static void testNbsp() {
042: ClickLink step = new ClickLink();
043:
044: step.setLabel("a" + NBSP + "b");
045: // TODO migration: why should this nbsp be transformed to a normal space?
046: // assertEquals("a b", step.getLabel());
047: }
048:
049: // <clickLink htmlId="notExisting" />
050: public void testClickNonExistingLink() throws Exception {
051: final String htmlContent = wrapContent("No link");
052: getContext().setDefaultResponse(htmlContent);
053: fStep.setHtmlId("notExisting");
054: final HtmlPage page = getDummyPage(htmlContent);
055: assertNull(fStep.findClickableElement(page));
056: }
057:
058: // <clickLink htmlId="dummyLink" />
059: public void testLocateWebLinkById() throws Exception {
060: final String htmlContent = wrapContent("<a id='dummyLink' href='dummy.html'>dummy</a>");
061: fStep.setHtmlId("dummyLink");
062: final HtmlPage page = getDummyPage(htmlContent);
063: assertNotNull(fStep.findClickableElement(page));
064: }
065:
066: // <clickLink htmlId="idNotAnchor" />
067: public void testLocateNonLinkFails() throws Exception {
068: final String htmlContent = wrapContent("<span id='idNotAnchor'>dummy</span>");
069: final HtmlPage page = getDummyPage(htmlContent);
070: fStep.setHtmlId("idNotAnchor");
071: String msg = ThrowAssert.assertThrows(
072: StepFailedException.class, new TestBlock() {
073: public void call() throws Exception {
074: fStep.findClickableElement(page);
075: }
076: });
077: assertTrue(msg.indexOf("not a link") != -1);
078: }
079:
080: // <clickLink label="dummy" />
081: public void testLocateWebLinkByLabel() throws Exception {
082: final String htmlContent = wrapContent("<a id='dummyLink' href='dummy.html'>dummy</a>");
083:
084: final HtmlPage page = getDummyPage(htmlContent);
085: fStep.setLabel("dummy");
086: assertNotNull(fStep.findClickableElement(page));
087: }
088:
089: // <clickLink label="dummy" href="foo"/>
090: public void testLocateWebLinkByLabelAndHref() throws Exception {
091: final String htmlContent = wrapContent("<a href='foo.html'>dummy</a>, <a href='foo2.html'>foo</a>");
092: final HtmlPage page = getDummyPage(htmlContent);
093:
094: final ClickLink step = (ClickLink) getStep();
095:
096: step.setLabel("dummy");
097: step.setHref("foo");
098: assertEquals("foo.html", ((HtmlAnchor) step
099: .findClickableElement(page)).getHrefAttribute());
100: step.setLabel("foo");
101: step.setHref("foo");
102: assertEquals("foo2.html", ((HtmlAnchor) step
103: .findClickableElement(page)).getHrefAttribute());
104: }
105:
106: // <clickLink label="dummyalt" />
107: public void testLocateWebLinkByMatchingImageAlt() throws Exception {
108: final String htmlContent = wrapContent("<img src='dummy.gif' alt='dummyalt'/>"
109: + "<table><tr><td><img src='dummy.gif' alt='dummyalt'/></td></tr></table>"
110: + "<a href='dummy.html'><img src='dummy.gif' alt='dummyalt'/></a>");
111: final HtmlPage page = getDummyPage(htmlContent);
112: fStep.setLabel("dummyalt");
113: assertNotNull(fStep.findClickableElement(page));
114: }
115:
116: // <clickLink href="dummy.html" />
117: public void testLocateWebLinkByHref() throws Exception {
118: final String htmlContent = wrapContent("<a id='dummyLink' href='dummy.html'>dummy</a>");
119: final HtmlPage page = getDummyPage(htmlContent);
120: fStep.setHref("dummy.html");
121: assertNotNull(fStep.findClickableElement(page));
122: }
123:
124: // <clickLink href="param=value" />
125: public void testLocateWebLinkByHrefParams() throws Exception {
126: final String htmlContent = wrapContent("<a id='dummyLink' href='dummy.jsp?param=value&this=that'>dummy</a>");
127: fStep.setHref("param=value");
128: final HtmlPage page = getDummyPage(htmlContent);
129: assertNotNull(fStep.findClickableElement(page));
130: }
131:
132: // <clickLink xpath="//a[@class = 'foo']" />
133: public void testLocateWebLinkByXPath() throws Exception {
134: final String htmlContent = wrapContent("<a href='dummy.jsp'>dummy1</a> <a href='dummy.jsp' class='foo'>dummy2</a>");
135: fStep.setXpath("//a[@class = 'foo']");
136: final HtmlPage page = getDummyPage(htmlContent);
137: assertEquals("dummy2", fStep.findClickableElement(page)
138: .asText());
139: }
140:
141: // <clickLink xpath="//a[@class = 'foo']" />
142: public void testExecuteXPath() throws Exception {
143: final String htmlContent = wrapContent("<a href='dummy.jsp'>dummy1</a> <a href='dummy.jsp' class='foo' onclick='alert(\"foo\")'>dummy2</a>");
144: fStep.setXpath("//a[@class = 'foo']");
145: final HtmlPage page = getDummyPage(htmlContent);
146: final WebClient client = page.getWebClient();
147: final List collectedAlerts = new ArrayList();
148: final AlertHandler alertHandler = new CollectingAlertHandler(
149: collectedAlerts);
150: client.setAlertHandler(alertHandler);
151:
152: fStep.execute();
153:
154: final List expectedAlerts = Collections.singletonList("foo");
155: assertEquals(expectedAlerts, collectedAlerts);
156:
157: }
158:
159: // <clickLink />
160: public void testNoAttributes() throws Exception {
161: String msg = ThrowAssert.assertThrows(
162: StepExecutionException.class, new TestBlock() {
163: public void call() throws Exception {
164: fStep.verifyParameters();
165: }
166: });
167:
168: assertEquals(
169: "\"htmlId\" or \"xpath\" or \"label\" or \"href\" must be set!",
170: msg);
171: }
172:
173: /**
174: * Tests that an exception is thrown when label and/or href is used combined with htmlid
175: */
176: public void testVerifyParameterHtmlIdNotCombined() {
177: fStep.setHtmlId("idNotAnchor");
178: fStep.setLabel("foo");
179:
180: TestBlock block = new TestBlock() {
181: public void call() throws Exception {
182: fStep.verifyParameters();
183: }
184: };
185:
186: // with htmlid and label
187: ThrowAssert.assertThrows(StepExecutionException.class, block);
188:
189: // with htmlid, label and href
190: fStep.setHref("foo");
191: ThrowAssert.assertThrows(StepExecutionException.class, block);
192:
193: // with htmlid and href
194: fStep.setLabel(null);
195: ThrowAssert.assertThrows(StepExecutionException.class, block);
196:
197: // with htmlid and xpath
198: fStep.setHref(null);
199: fStep.setXpath("//a");
200: ThrowAssert.assertThrows(StepExecutionException.class, block);
201: }
202:
203: /**
204: * Tests that an exception is thrown when label and/or href is used combined with xpath
205: */
206: public void testVerifyParametersXPath() {
207: fStep.setXpath("//a");
208:
209: TestBlock block = new TestBlock() {
210: public void call() throws Exception {
211: fStep.verifyParameters();
212: }
213: };
214:
215: // with xpath and label
216: fStep.setLabel("foo");
217: ThrowAssert.assertThrows(StepExecutionException.class, block);
218:
219: // with xpath, label and href
220: fStep.setHref("foo");
221: ThrowAssert.assertThrows(StepExecutionException.class, block);
222:
223: // with xpath and href
224: fStep.setLabel(null);
225: ThrowAssert.assertThrows(StepExecutionException.class, block);
226: }
227:
228: public void testErrorWithXmlPageUnknownLabel() throws Exception {
229: fStep.setLabel("foo");
230: assertErrorOnExecuteIfCurrentPageIsXml(fStep);
231: }
232:
233: public void testErrorWithXmlPageInvalidXPath() throws Exception {
234: final String htmlContent = wrapContent("<span>dummy</span>");
235: final HtmlPage page = getDummyPage(htmlContent);
236: fStep.setXpath("//span");
237: String msg = ThrowAssert.assertThrows(
238: StepFailedException.class, new TestBlock() {
239: public void call() throws Exception {
240: fStep.findClickableElement(page);
241: }
242: });
243: assertTrue(msg.indexOf("not a link") != -1);
244: assertTrue(msg.indexOf("span tag") != -1);
245: }
246:
247: private static String wrapContent(String content) {
248: return "<html><head><title>foo</title></head><body>" + content
249: + "</body></html>";
250: }
251:
252: public void testNestedText() throws Exception {
253: testNestedTextEquivalent(getStep(), "label");
254: }
255: }
|