001: // Copyright © 2004-2005 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.steps.request;
003:
004: import java.net.URL;
005: import java.util.Iterator;
006:
007: import com.canoo.webtest.engine.StepExecutionException;
008: import com.canoo.webtest.self.TestBlock;
009: import com.canoo.webtest.self.ThrowAssert;
010: import com.canoo.webtest.steps.BaseStepTestCase;
011: import com.canoo.webtest.steps.Step;
012: import com.gargoylesoftware.htmlunit.MockWebConnection;
013: import com.gargoylesoftware.htmlunit.WebClient;
014: import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
015: import com.gargoylesoftware.htmlunit.html.HtmlPage;
016:
017: /**
018: * Test class for {@link SelectWindow}.<p>
019: *
020: * @author Paul King
021: * @author Marc Guillemot
022: */
023: public class SelectWindowTest extends BaseStepTestCase {
024: protected Step createStep() {
025: return new SelectWindow();
026: }
027:
028: public void testVerifyIndexParameterInvalid() {
029: SelectWindow step = new SelectWindow();
030:
031: step.setIndex("x");
032: assertVerificationFails("1) index must be integer", step);
033:
034: step.setIndex("-1");
035: assertVerificationFails("2) index must be positive", step);
036: }
037:
038: public void testVerifyIndexParameterValid() {
039: SelectWindow step = new SelectWindow();
040:
041: step.setIndex("0");
042: assertVerificationOk("1) index is 0", step);
043:
044: step.setIndex("1");
045: assertVerificationOk("2) index is positive integer", step);
046: }
047:
048: public void testVerifyNameParameterValid() {
049: SelectWindow step = new SelectWindow();
050:
051: step.setName("");
052: assertVerificationOk("1) name is empty", step);
053:
054: step.setName("a");
055: assertVerificationOk("2) name is set", step);
056:
057: }
058:
059: public void testParameterCombination() {
060: SelectWindow step = new SelectWindow();
061:
062: step.setIndex("");
063: step.setName("a");
064: assertVerificationOk("1) index is empty and name is set", step);
065:
066: step = new SelectWindow();
067: step.setName("1");
068: step.setIndex("1");
069: assertVerificationOk("2) name is empty and index is set", step);
070:
071: step = new SelectWindow();
072: step.setName("");
073: step.setIndex("");
074: assertVerificationOk("3) both name and index are empty", step);
075:
076: step = new SelectWindow();
077: step.setName(null);
078: step.setIndex("");
079: assertVerificationFails("4) name is null and index is empty",
080: step);
081:
082: step = new SelectWindow();
083: step.setName("");
084: step.setIndex(null);
085: assertVerificationOk("5) name is empty and index is null", step);
086:
087: step = new SelectWindow();
088: step.setName(null);
089: step.setIndex(null);
090: assertVerificationFails("6) both name and index are null", step);
091: }
092:
093: private void assertVerificationFails(final String message,
094: final SelectWindow step) {
095: ThrowAssert.assertThrows(message, StepExecutionException.class,
096: new TestBlock() {
097: public void call() throws Exception {
098: step.verifyParameters();
099: }
100: });
101: }
102:
103: private void assertVerificationOk(final String message,
104: final SelectWindow step) {
105: configureStep(step);
106: ThrowAssert.assertPasses(message, new TestBlock() {
107: public void call() throws Exception {
108: step.verifyParameters();
109: }
110: });
111: }
112:
113: public void testWindowNotFoundMessage() throws Exception {
114: final WebClient webClient = new WebClient();
115: final MockWebConnection connection = new MockWebConnection(
116: webClient);
117: webClient.setWebConnection(connection);
118:
119: final String contentMain = "<html><head><title>Main</title></head>"
120: + "<body>"
121: + "<a href='2.html' target='foo'>2</a>"
122: + "<a href='3.txt' target='_blank'>3</a>"
123: + "<iframe src='about:blank'></iframe>"
124: + "</body></html>";
125:
126: final URL urlBase = new URL("http://foo");
127: connection.setResponse(urlBase, contentMain);
128: connection.setResponse(new URL(urlBase, "2.html"),
129: "<html><head><title>page 2</title></head></html>");
130: connection.setResponse(new URL(urlBase, "3.txt"), "bla",
131: "text/plain");
132:
133: final HtmlPage page = (HtmlPage) webClient.getPage(urlBase);
134: for (Iterator iter = page.getAnchors().iterator(); iter
135: .hasNext();) {
136: final HtmlAnchor link = (HtmlAnchor) iter.next();
137: link.click();
138: }
139:
140: final String expectedMsg = "index: 0, name: ><, title: >Main<, url: http://foo\n"
141: + "index: 1, name: >foo<, title: >page 2<, url: http://foo/2.html\n"
142: + "index: 2, name: ><, text/plain, url: http://foo/3.txt";
143: assertEquals(expectedMsg, SelectWindow
144: .getAvailableWindowsMessage(webClient));
145: }
146: }
|