001: // Copyright © 2004-2005 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.extension;
003:
004: import org.apache.log4j.Logger;
005:
006: import com.canoo.webtest.engine.StepExecutionException;
007: import com.canoo.webtest.engine.StepFailedException;
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.Page;
013:
014: /**
015: * Test class for {@link StoreElementAttribute}.
016: * @author Paul King
017: */
018: public class StoreElementAttributeTest extends BaseStepTestCase {
019: private static final Logger LOG = Logger
020: .getLogger(StoreElementAttributeTest.class);
021:
022: private StoreElementAttribute fStep;
023:
024: protected Step createStep() {
025: return new StoreElementAttribute();
026: }
027:
028: protected void setUp() throws Exception {
029: super .setUp();
030: fStep = (StoreElementAttribute) getStep();
031: }
032:
033: public void testVerifyParameterUsage() {
034: // <storeElementAttribute />
035: String message = ThrowAssert
036: .assertThrows(StepExecutionException.class,
037: getExecuteStepTestBlock());
038: assertEquals("\"htmlId\" or \"xPath\" must be set!", message);
039:
040: // <storeElementAttribute htmlId="X" xPath="Y" />
041: fStep.setHtmlId("X");
042: fStep.setXpath("Y");
043: message = ThrowAssert
044: .assertThrows(StepExecutionException.class,
045: getExecuteStepTestBlock());
046: assertEquals(
047: "Only one from \"htmlId\" and \"xPath\" can be set!",
048: message);
049:
050: fStep.setXpath(null);
051: assertStepRejectsNullParam("property",
052: getExecuteStepTestBlock());
053: fStep.setProperty("someName");
054: assertStepRejectsNullParam("attributeName",
055: getExecuteStepTestBlock());
056: }
057:
058: public void testVerifyParametersWithoutPreviousPage() {
059: fStep.setHtmlId("someId");
060: fStep.setProperty("someName");
061: fStep.setAttributeName("someName");
062: assertStepRejectsNullResponse(fStep);
063: }
064:
065: /**
066: * Test deprecated methods
067: * @deprecated
068: */
069: public void testDeprecatedAttributes() {
070: assertNull(fStep.getProperty());
071: fStep.setPropertyName("foo");
072: assertEquals("foo", fStep.getProperty());
073: }
074:
075: public void testNonHtmlUsingHtmlId() {
076: getContext().setDefaultResponse("", "text/plain");
077: fStep.setHtmlId("someId");
078: fStep.setProperty("someName");
079: fStep.setAttributeName("someName");
080: ThrowAssert.assertThrows(StepExecutionException.class,
081: getExecuteStepTestBlock());
082: }
083:
084: public void testFindElement() {
085: Page page = getDummyPage("<html><body><img id='image'><p></body></html>");
086: assertEquals("img", StoreElementAttribute.findElement(page,
087: "image", null, LOG, fStep).getNodeName());
088: assertEquals("img", StoreElementAttribute.findElement(page,
089: "image", "//p", LOG, fStep).getNodeName());
090: assertEquals("p", StoreElementAttribute.findElement(page, null,
091: "//p", LOG, fStep).getNodeName());
092: }
093:
094: public void testFindElementByXpath() {
095: final Page page = getDummyPage("<html><body><img id='image'></body></html>");
096: assertEquals("img", StoreElementAttribute.findElementByXpath(
097: page, "//img", LOG, fStep).getNodeName());
098: ThrowAssert.assertThrows(StepFailedException.class,
099: new TestBlock() {
100: public void call() throws Exception {
101: StoreElementAttribute.findElementByXpath(page,
102: "//p", LOG, fStep);
103: }
104: });
105: ThrowAssert.assertThrows(StepFailedException.class,
106: new TestBlock() {
107: public void call() throws Exception {
108: StoreElementAttribute.findElementByXpath(page,
109: "//img/@id", LOG, fStep);
110: }
111: });
112: }
113:
114: public void testFindElementById() {
115: final Page page = getDummyPage("<html><body><img id='image'></body></html>");
116: assertEquals("img", StoreElementAttribute.findElementById(page,
117: "image", LOG, fStep).getNodeName());
118: ThrowAssert.assertThrows(StepFailedException.class,
119: new TestBlock() {
120: public void call() throws Exception {
121: StoreElementAttribute.findElementById(page,
122: "pasImage", LOG, fStep);
123: }
124: });
125: ThrowAssert.assertThrows(StepExecutionException.class,
126: new TestBlock() {
127: public void call() throws Exception {
128: StoreElementAttribute.findElementById(
129: getDummyPage("hello", "text/plain"),
130: "pasImportant", LOG, fStep);
131: }
132: });
133: }
134: }
|