001: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.form;
003:
004: import java.util.ArrayList;
005: import java.util.Collections;
006: import java.util.List;
007:
008: import com.canoo.webtest.engine.StepFailedException;
009: import com.canoo.webtest.self.TestBlock;
010: import com.canoo.webtest.self.ThrowAssert;
011: import com.canoo.webtest.steps.BaseStepTestCase;
012: import com.canoo.webtest.steps.Step;
013: import com.gargoylesoftware.htmlunit.AlertHandler;
014: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
015: import com.gargoylesoftware.htmlunit.WebClient;
016: import com.gargoylesoftware.htmlunit.html.HtmlForm;
017: import com.gargoylesoftware.htmlunit.html.HtmlOption;
018: import com.gargoylesoftware.htmlunit.html.HtmlPage;
019: import com.gargoylesoftware.htmlunit.html.HtmlSelect;
020:
021: /**
022: * Test cases for {@link com.canoo.webtest.steps.form.SetSelectField}.
023: * @author Marc Guillemot
024: */
025: public class SetSelectFieldTest extends BaseStepTestCase {
026: private static final String HTML_CONTENT = "<html><head><title>foo</title></head>"
027: + "<body>"
028: + "<form name='testForm'>"
029: + "<select name='mySelect' id='mySelectId'>"
030: + "<option value='0'>0</option>"
031: + "<option value='1'>first</option>"
032: + "<option value='2' id='opt2'>second</option>"
033: + "<option value='3'>third</option>"
034: + "<option>4</option>"
035: + "<option selected>5</option>"
036: + "</select>"
037: + "</form>"
038: + "No access</body></html>";
039: private SetSelectField fStep;
040:
041: protected Step createStep() {
042: return new SetSelectField();
043: }
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047: fStep = (SetSelectField) getStep();
048: }
049:
050: public void testSelectOption() {
051: final HtmlPage page = getDummyPage(HTML_CONTENT);
052: final HtmlForm form = page.getFormByName("testForm");
053: final HtmlSelect select = form.getSelectByName("mySelect");
054:
055: // test with value
056: fStep.setName("mySelect");
057: fStep.setValue("2");
058: HtmlOption option = fStep.findMatchingOption(select);
059: fStep.updateOption(select, option);
060:
061: assertEquals("second", option.asText());
062: assertTrue(option.isSelected());
063:
064: // test with value... for an option that has no value
065: fStep.setValue("4");
066: option = fStep.findMatchingOption(select);
067: assertEquals("4", option.asText());
068:
069: // test with index
070: fStep.setValue(null);
071: fStep.setOptionIndex("1");
072: option = fStep.findMatchingOption(select);
073: fStep.updateOption(select, option);
074: assertEquals("first", option.asText());
075: assertTrue(option.isSelected());
076:
077: // test with text
078: fStep.setOptionIndex(null);
079: fStep.setText("third");
080: option = fStep.findMatchingOption(select);
081: fStep.updateOption(select, option);
082: assertEquals("3", option.getValueAttribute());
083: assertTrue(option.isSelected());
084:
085: // check not finding option fails step
086: ThrowAssert.assertThrows(StepFailedException.class,
087: new TestBlock() {
088: public void call() throws Throwable {
089: fStep.updateOption(select, null);
090: }
091: });
092: }
093:
094: public void testFailureConditions() throws Exception {
095: fStep.setName("mySelect");
096:
097: fStep.setOptionIndex("1");
098: fStep.setValue("anything");
099: fStep.setText("some text");
100: assertErrorOnExecute(fStep, "multiple args not allowed",
101: SetSelectField.AT_MOST_ONE_VALUE_TEXT_OPTIONINDEX);
102:
103: fStep.setValue(null);
104: assertErrorOnExecute(fStep, "multiple args not allowed",
105: SetSelectField.AT_MOST_ONE_VALUE_TEXT_OPTIONINDEX);
106:
107: fStep.setOptionIndex(null);
108: fStep.verifyParameters();
109: assertFalse(fStep.isRegex());
110: assertFalse(fStep.isMultiSelect());
111: }
112:
113: /**
114: * Test identification of select and option with xpath
115: */
116: public void testXPath() {
117: getContext().setDefaultResponse(HTML_CONTENT);
118:
119: final HtmlPage page = getContext()
120: .getCurrentHtmlResponse(fStep);
121: final HtmlSelect select = page.getFormByName("testForm")
122: .getSelectByName("mySelect");
123: final HtmlOption option0 = select.getOption(0);
124: final HtmlOption option1 = select.getOption(1);
125: final HtmlOption option2 = select.getOption(2);
126: final HtmlOption option3 = select.getOption(3);
127:
128: // test with xpath for the option
129: fStep.setXpath("//option[@value='3']");
130: assertFalse(option3.isSelected());
131: fStep.execute();
132: assertTrue(option3.isSelected());
133:
134: // test with xpath for the select and text for the option
135: fStep.setXpath("//select");
136: fStep.setText("first");
137: fStep.execute();
138: assertTrue(option1.isSelected());
139:
140: // test with xpath for the select and text for the option
141: fStep.setXpath("//select");
142: fStep.setText(null);
143: fStep.setValue("0");
144: fStep.execute();
145: assertTrue(option0.isSelected());
146:
147: // test with xpath for the select and optionIndex for the option
148: fStep.setXpath("//select");
149: fStep.setText(null);
150: fStep.setValue(null);
151: fStep.setOptionIndex("2");
152: fStep.execute();
153: assertTrue(option2.isSelected());
154: }
155:
156: /**
157: * Test identification of select and option with id
158: */
159: public void testHtmlId() {
160: getContext().setDefaultResponse(HTML_CONTENT);
161:
162: final HtmlPage page = getContext()
163: .getCurrentHtmlResponse(fStep);
164: final HtmlSelect select = page.getFormByName("testForm")
165: .getSelectByName("mySelect");
166: final HtmlOption option1 = select.getOption(1);
167: final HtmlOption option2 = select.getOption(2);
168:
169: // test with id for the option
170: fStep.setHtmlId("opt2");
171: assertFalse(option2.isSelected());
172: fStep.execute();
173: assertTrue(option2.isSelected());
174:
175: // test with id for the select and text for the option
176: fStep.setHtmlId("mySelectId");
177: fStep.setText("first");
178: fStep.execute();
179: assertTrue(option1.isSelected());
180:
181: // test with id for the select and nothing for the option
182: fStep.setHtmlId("mySelectId");
183: fStep.setText(null);
184: assertErrorOnExecute(fStep, "",
185: SetSelectField.MESSAGE_MISSING_OPTION_IDENTIFIER);
186: }
187:
188: /**
189: * Regression test for bug WT-143: onchange was wrongly triggered twice
190: */
191: public void testOnchangeTriggeredTwice() {
192: // add onchange handler to standard code
193: final String html = HTML_CONTENT
194: .replaceFirst("id='mySelectId'",
195: "id='mySelectId' onchange='alert(this.selectedIndex)')");
196: getContext().setDefaultResponse(html);
197:
198: final HtmlPage page = getContext()
199: .getCurrentHtmlResponse(fStep);
200: final WebClient client = page.getWebClient();
201: final List collectedAlerts = new ArrayList();
202: final AlertHandler alertHandler = new CollectingAlertHandler(
203: collectedAlerts);
204: client.setAlertHandler(alertHandler);
205:
206: fStep.setName("mySelect");
207: fStep.setValue("3");
208: fStep.execute();
209:
210: final List expectedAlerts = Collections.singletonList("3");
211: assertEquals(expectedAlerts, collectedAlerts);
212: }
213: }
|