01: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.steps.form;
03:
04: import java.io.IOException;
05:
06: import com.canoo.webtest.steps.BaseStepTestCase;
07: import com.canoo.webtest.steps.Step;
08: import com.gargoylesoftware.htmlunit.html.HtmlElement;
09: import com.gargoylesoftware.htmlunit.html.HtmlForm;
10: import com.gargoylesoftware.htmlunit.html.HtmlInput;
11: import com.gargoylesoftware.htmlunit.html.HtmlPage;
12: import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
13:
14: /**
15: * Test cases for {@link com.canoo.webtest.steps.form.SetInputField}
16: * @author Marc Guillemot
17: */
18: public class SetInputFieldTest extends BaseStepTestCase {
19: private SetInputField fStep;
20:
21: protected Step createStep() {
22: return new SetInputField();
23: }
24:
25: protected void setUp() throws Exception {
26: super .setUp();
27: fStep = (SetInputField) getStep();
28: }
29:
30: public void testDefineTextField() throws IOException {
31: final String htmlContent = "<html><head><title>foo</title></head>"
32: + "<body>"
33: + "<form name='testForm'>"
34: + "<input name='myInput'>"
35: + "</form>"
36: + "No access</body></html>";
37: final HtmlPage page = getDummyPage(htmlContent);
38: final HtmlForm form = page.getFormByName("testForm");
39: final SetInputField step = new SetInputField();
40:
41: // test with value
42: step.setName("myInput");
43: step.setValue("some value");
44: step.setField((HtmlElement) step.findFields(form).get(0));
45: final HtmlInput input = form.getInputByName("myInput");
46: assertEquals("some value", input.getValueAttribute());
47: }
48:
49: /**
50: * Test identification of text field with the label of an associated 'label' form field.<p>
51: */
52: public void testForLabel() {
53: final String html = "<html><body>"
54: + "<form name='testForm'>"
55: + "<label for='theFirstName'>First Name</label>: <input name='name' id='theFirstName' type='text'/>"
56: + "<label for='theAddress'>Address</label>: <input name='address' id='theAddress' type='text'/>"
57: + "</form>" + "</body></html>";
58: getContext().setDefaultResponse(html);
59:
60: final HtmlPage page = getContext()
61: .getCurrentHtmlResponse(fStep);
62: final HtmlTextInput inputFirstName = (HtmlTextInput) page
63: .getHtmlElementById("theFirstName");
64: final HtmlTextInput inputAddress = (HtmlTextInput) page
65: .getHtmlElementById("theAddress");
66:
67: fStep.setForLabel("First Name");
68: fStep.setValue("bla bla");
69: assertEquals("", inputFirstName.getValueAttribute());
70: fStep.execute();
71: assertEquals("bla bla", inputFirstName.getValueAttribute());
72:
73: fStep.setForLabel("Address");
74: fStep.setValue("11 Downing Street");
75: assertEquals("", inputAddress.getValueAttribute());
76: fStep.execute();
77: assertEquals("11 Downing Street", inputAddress
78: .getValueAttribute());
79: }
80: }
|