001: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.request;
003:
004: import java.io.File;
005:
006: import com.canoo.webtest.engine.StepFailedException;
007: import com.canoo.webtest.engine.WebTestException;
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.MockWebConnection;
013:
014: /**
015: * Unit tests for {@link InvokePage}.
016: * @author Unknown
017: * @author Marc Guillemot
018: */
019: public class InvokePageTest extends BaseStepTestCase {
020: protected Step createStep() {
021: return new InvokePage();
022: }
023:
024: public void testExecuteFailsWithoutUrl() {
025: assertErrorOnExecute(getStep(), "no Url", "");
026: }
027:
028: public void testExecuteFailsWithTooManyParams() {
029: InvokePage step = (InvokePage) getStep();
030: step.setMethod("POST");
031: assertErrorOnExecute(step,
032: "post with no content or contentFile", "");
033: step.setContent("dummy");
034: step.setContentFile(new File("dummy"));
035: assertErrorOnExecute(step, "both content and contentFile", "");
036: }
037:
038: /**
039: * Tests that javascript errors are caught and correctly reported
040: * @throws Exception if the test fails
041: */
042: public void testCatchRuntimeScriptError() throws Exception {
043: final String url = "http://myhost.mydomain/myPage.html";
044: final String html = "<html><head><script>notExisting()</script></head></html>";
045: final String message = checkCodeWithScriptError(url, html)
046: .getMessage();
047: assertTrue("message doesn't contain page url: " + message,
048: message.indexOf(url) != -1);
049: assertTrue(
050: "message doesn't contain information about failing function: "
051: + message, message.indexOf("notExisting") != -1);
052: }
053:
054: /**
055: * Tests that javascript parsing errors are caught and correctly reported
056: * @throws Exception if the test fails
057: */
058: public void testCatchParseScriptError() throws Exception {
059: final String url = "http://myhost.mydomain/myPage.html";
060: final String html = "<html><head><script>var a = 1;\nnotExisting(;</script></head></html>";
061: final WebTestException t = (WebTestException) checkCodeWithScriptError(
062: url, html);
063: assertTrue("message doesn't contain page url: "
064: + t.getMessage(), t.getMessage().indexOf(url) != -1);
065: assertTrue(
066: "message doesn't contain information about non parseable code: "
067: + t.getDetails(), t.getDetails().toString()
068: .indexOf("notExisting(;") != -1);
069: }
070:
071: /**
072: * Loads the given html as answer from the url,
073: * checks that a StepFailedException is thrown and return the error message
074: * @return the error message
075: */
076: private Throwable checkCodeWithScriptError(final String url,
077: final String html) {
078: final InvokePage step = (InvokePage) getStep();
079: ((MockWebConnection) getContext().getWebClient()
080: .getWebConnection()).setDefaultResponse(html);
081:
082: step.setUrl(url);
083: final TestBlock block = new TestBlock() {
084: public void call() throws Throwable {
085: step.execute();
086: }
087: };
088: return ThrowAssert.assertThrows("", StepFailedException.class,
089: block);
090: }
091:
092: public void testNestedText() throws Exception {
093: InvokePage invokeStep = (InvokePage) getStep();
094: testNestedTextEquivalent(invokeStep, "url");
095:
096: invokeStep = (InvokePage) createAndConfigureStep();
097: invokeStep.setUrl("http://my.web.site/foo");
098: testNestedTextEquivalent(invokeStep, "content");
099:
100: }
101: }
|