001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.store;
003:
004: import org.jaxen.JaxenException;
005:
006: import com.canoo.webtest.self.ThrowAssert;
007: import com.canoo.webtest.steps.BaseStepTestCase;
008: import com.canoo.webtest.steps.Step;
009:
010: /**
011: * Tests for {@link StoreXPath}.
012: * @author Walter Rumsby (wrumsby@netscape.net)
013: * @version 1.0, 29/10/2002
014: * @author Marc Guillemot
015: * @version 3.0, 11/08/2003
016: * @author Denis N. Antonioli
017: */
018: public class StoreXPathTest extends BaseStepTestCase {
019: private StoreXPath fStep;
020: public static final String PROPERTY_NAME = "result";
021:
022: protected void setUp() throws Exception {
023: super .setUp();
024:
025: fStep = (StoreXPath) getStep();
026: fStep.setProperty(PROPERTY_NAME);
027: }
028:
029: private static final String DOCUMENT = "<html><body><form><input type=\"hidden\" id=\"id\" value=\"1\"/><input/></form></body></html>";
030:
031: protected Step createStep() {
032: return new StoreXPath();
033: }
034:
035: public void testHandleHtmlPage() throws Exception {
036: String document = "<html><head></head><body><h1>hello</h1></body></html>";
037: getContext().setDefaultResponse(document);
038:
039: fStep.setXpath("/html/body/h1");
040: executeStep(fStep);
041: assertEquals("hello", fStep.getComputedValue());
042: }
043:
044: public void testHandleXmlPage() throws Exception {
045: getContext().setDefaultResponse(
046: "<xml><body><h1>hello</h1></body></xml>", "text/xml");
047:
048: fStep.setXpath("/xml/body/h1");
049: executeStep(fStep);
050: assertEquals("hello", fStep.getComputedValue());
051: }
052:
053: /**
054: * Tests on an XmlPage containing an badly formed xml document
055: * @throws Exception if the test fails
056: */
057: public void testHandleInvalidXmlPageIllegalChar() throws Exception {
058: getContext().setDefaultResponse("<xml type='foo & fii'></xml>",
059: "text/xml");
060: fStep.setXpath("/xml");
061: assertFailOnExecute(fStep, "", "");
062: }
063:
064: public void testHandleInvalidXmlPageNotWellFormed()
065: throws Exception {
066: getContext().setDefaultResponse("<xml></html>", "text/xml");
067: fStep.setXpath("/xml");
068: assertFailOnExecute(fStep, "", "");
069: }
070:
071: public void testHandleMissingPage() throws Exception {
072: getContext().fakeLastResponse(null);
073: fStep.setXpath("'valeur'='value'");
074: executeStep(fStep);
075: assertEquals("false", fStep.getComputedValue());
076:
077: fStep.setXpath("/not/here");
078: assertFailOnExecute(fStep, "", "");
079:
080: fStep.setXpath("/");
081: assertFailOnExecute(fStep, "", "");
082: }
083:
084: public void testHandleUnknownPage() throws JaxenException {
085: getContext().setDefaultResponse("hello", "text/plain");
086:
087: fStep.setXpath("/html/head/title");
088: assertFailOnExecute(fStep, "", "");
089: }
090:
091: public void testHandleMalformedXmlPage() throws JaxenException {
092: getContext().setDefaultResponse("hello", "text/xml");
093:
094: fStep.setXpath("/html/head/title");
095: assertFailOnExecute(fStep, "", "");
096: }
097:
098: public void testVerifyParameters() throws Exception {
099: fStep.setXpath("some xpath");
100: fStep.setProperty(null);
101: assertStepRejectsEmptyParam("property",
102: getExecuteStepTestBlock());
103:
104: fStep.setXpath(null);
105: fStep.setProperty(PROPERTY_NAME);
106: assertStepRejectsNullParam("xpath", getExecuteStepTestBlock());
107: }
108:
109: public void testExceptionIfNoMatch() throws Exception {
110: getContext().setDefaultResponse(DOCUMENT);
111:
112: fStep.setXpath("/html/foot");
113: assertFailOnExecute(fStep, "No match for xpath expression", "");
114:
115: // no exception if default value is provided
116: fStep.setDefault("bla");
117: fStep.execute();
118: }
119:
120: public void testStringExpressionInMatch() throws Exception {
121: getContext().setDefaultResponse(DOCUMENT);
122:
123: fStep.setXpath("count(/html)");
124:
125: ThrowAssert.assertPasses(
126: "xpath evaluates to string - not node",
127: getExecuteStepTestBlock());
128: assertEquals("1", fStep.getWebtestProperty(PROPERTY_NAME));
129: }
130:
131: /**
132: * This test proves that there is a bug now.
133: * It will fail after the installation of a newer, correct version of jaxen.
134: * @throws Exception if the test fails
135: */
136: public void testWT52() throws Exception {
137: String document = "<html><body>"
138: + "<p><a href='no'><img src='no.gif'></a></p>"
139: + "<p><a href='yes'><img src='yes.gif'></a></p>"
140: + "</body></html> ";
141: getContext().setDefaultResponse(document);
142:
143: fStep.setXpath("(//a/img[contains(@src,'gif')])[2]/../@href");
144: executeStep(fStep);
145: assertEquals("yes", fStep.getWebtestProperty(PROPERTY_NAME));
146: }
147:
148: /**
149: * This test proves that there is a bug now.
150: * It will fail after the installation of a newer, correct version of jaxen.
151: * @throws Exception if the test fails
152: */
153: public void testStoreEmtpyString() throws Exception {
154: String document = "<html><body>"
155: + "<form><select><option value=''></option>"
156: + "<option value='1'>first</option>"
157: + "<option value='2'>second</option>" + "</form>"
158: + "</body></html> ";
159: getContext().setDefaultResponse(document);
160:
161: fStep.setXpath("//select/option[1]");
162: executeStep(fStep);
163: assertEquals("", fStep.getWebtestProperty(PROPERTY_NAME));
164:
165: fStep.setXpath("//div[@id='notExisting']");
166: fStep.setDefault("bla bla");
167: executeStep(fStep);
168: assertEquals("bla bla", fStep.getWebtestProperty(PROPERTY_NAME));
169: }
170: }
|