001: // Copyright © 2005-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.steps.form;
003:
004: import com.canoo.webtest.steps.Step;
005: import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
006: import com.gargoylesoftware.htmlunit.html.HtmlPage;
007:
008: /**
009: * Test cases for {@link SetCheckbox}
010: * @author Paul King, ASERT
011: * @author Marc Guillemot
012: */
013: public class SetCheckBoxTest extends AbstractSetFieldStepTest {
014: private SetCheckbox fStep;
015:
016: protected void setUp() throws Exception {
017: super .setUp();
018: fStep = (SetCheckbox) getStep();
019: }
020:
021: protected Step createStep() {
022: return new SetCheckbox();
023: }
024:
025: // <setCheckbox name="nomatch" />
026: public void testNoMatch() {
027: final String htmlContent = wrapContent("<form name='testForm'>"
028: + "<input type='checkbox' name='bike'>I have a bike<br>"
029: + "<input type='checkbox' name='car'>I have a car"
030: + "</form>");
031: getContext().setDefaultResponse(htmlContent);
032:
033: fStep.setName("nomatch");
034: assertFailOnExecute(fStep,
035: "checkbox with this name should not be found",
036: "No suitable form found");
037: }
038:
039: // <setCheckbox name="car" value="I have a bike" />
040: public void testNameValueMismatch() {
041: final String htmlContent = wrapContent("<form name='testForm'>"
042: + "<input type='checkbox' name='bike'>I have a bike<br>"
043: + "<input type='checkbox' name='car'>I have a car"
044: + "</form>");
045:
046: getContext().setDefaultResponse(htmlContent);
047: fStep.setName("car");
048: fStep.setValue("I have a bike");
049: assertFailOnExecute(fStep,
050: "checkbox name should not match value",
051: "No suitable field(s) found");
052: }
053:
054: // <setCheckbox name="car" value="I have a Holden" />
055: public void testNoMatchByValue() {
056: final String htmlContent = wrapContent("<form name='testForm'>"
057: + "<input type='checkbox' name='car'>I have a Ford<br>"
058: + "<input type='hidden' name='car'>I have a Holden"
059: + "</form>");
060: getContext().setDefaultResponse(htmlContent);
061: fStep.setName("car");
062: fStep.setValue("I have a Holden");
063: assertFailOnExecute(fStep,
064: "name should match checkbox not other input field",
065: "No suitable field(s) found");
066: }
067:
068: // <setCheckbox />
069: public void testMandatoryAttributes() {
070: final Throwable thrown = assertErrorOnExecute(fStep, "", "");
071: assertTrue(thrown.getMessage().indexOf(
072: AbstractSetFieldStep.MESSAGE_ARGUMENT_MISSING) != -1);
073: }
074:
075: private static String wrapContent(final String content) {
076: return "<html><head><title>foo</title></head><body>" + content
077: + "</body></html>";
078: }
079:
080: public void testXPath() throws Exception {
081: final String htmlContent = wrapContent("<form name='testForm'>"
082: + "<input type='checkbox' name='car' value='car' id='it'>I have a Ford<br>"
083: + "<input type='hidden' name='car'>I have a Holden"
084: + "<input type='checkbox' name='car' value='bike'>I have a bike<br>"
085: + "</form>");
086: getContext().setDefaultResponse(htmlContent);
087:
088: final HtmlPage page = getContext()
089: .getCurrentHtmlResponse(fStep);
090: final HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) page
091: .getHtmlElementById("it");
092: assertFalse(checkbox.isChecked());
093:
094: fStep
095: .setXpath("//input[@type = 'checkbox' and @value = 'car']");
096: fStep.execute();
097:
098: assertTrue(checkbox.isChecked());
099:
100: fStep.setHtmlId("it");
101: fStep.setXpath(null);
102: fStep.setChecked("false");
103: fStep.execute();
104: assertFalse(checkbox.isChecked());
105: }
106:
107: }
|