0001: /******************************************************************************
0002: * jWebUnit project (http://jwebunit.sourceforge.net) *
0003: * Distributed open-source, see full license under LICENCE.txt *
0004: ******************************************************************************/package net.sourceforge.jwebunit.junit;
0005:
0006: import java.awt.Image;
0007: import java.awt.image.BufferedImage;
0008: import java.io.File;
0009: import java.io.FileOutputStream;
0010: import java.io.IOException;
0011: import java.io.InputStream;
0012: import java.io.PrintStream;
0013: import java.net.MalformedURLException;
0014: import java.net.URL;
0015: import java.text.MessageFormat;
0016: import java.util.Iterator;
0017: import java.util.List;
0018: import java.util.Locale;
0019: import java.util.ResourceBundle;
0020:
0021: import javax.imageio.ImageIO;
0022: import javax.servlet.http.Cookie;
0023:
0024: import junit.framework.Assert;
0025: import junit.framework.AssertionFailedError;
0026: import net.sourceforge.jwebunit.api.IJWebUnitDialog;
0027: import net.sourceforge.jwebunit.exception.ExpectedJavascriptAlertException;
0028: import net.sourceforge.jwebunit.exception.ExpectedJavascriptConfirmException;
0029: import net.sourceforge.jwebunit.exception.ExpectedJavascriptPromptException;
0030: import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
0031: import net.sourceforge.jwebunit.exception.UnableToSetFormException;
0032: import net.sourceforge.jwebunit.html.Table;
0033: import net.sourceforge.jwebunit.javascript.JavascriptAlert;
0034: import net.sourceforge.jwebunit.javascript.JavascriptConfirm;
0035: import net.sourceforge.jwebunit.javascript.JavascriptPrompt;
0036: import net.sourceforge.jwebunit.util.TestContext;
0037: import net.sourceforge.jwebunit.util.TestingEngineRegistry;
0038:
0039: import org.apache.regexp.RE;
0040: import org.apache.regexp.RESyntaxException;
0041:
0042: /**
0043: * Provides a high-level API for basic web application navigation and validation by providing
0044: * JUnit assertions. It supports use of a property file for web resources (a la Struts), though a resource file for the
0045: * app is not required.
0046: *
0047: * @author Julien Henry
0048: * @author Jim Weaver
0049: * @author Wilkes Joiner
0050: */
0051: public class WebTester {
0052: private IJWebUnitDialog dialog = null;
0053:
0054: private TestContext testContext = null;
0055:
0056: /**
0057: * This is the testing engine key that the webtester will use to find the correct testing engine from the registry.
0058: */
0059: private String testingEngineKey = null;
0060:
0061: /**
0062: * Provides access to the testing engine for subclasses - in case functionality not yet wrappered required by test.
0063: *
0064: * If the dialog is not explicitly set the jWebUnit framework will default to using the orignal testing engine,
0065: * which is, htmlunit.
0066: *
0067: * @return IJWebUnitDialog instance used to wrapper htmlunit conversation.
0068: * @deprecated You should not use plugin specific fonctionality. Please ask for a new core feature instead.
0069: */
0070: public IJWebUnitDialog getDialog() {
0071: return getTestingEngine();
0072: }
0073:
0074: /**
0075: * Protected version of deprecated getDialog(). Not deprecated for internal use.
0076: *
0077: * @return IJWebUnitDialog instance.
0078: */
0079: protected IJWebUnitDialog getTestingEngine() {
0080: if (dialog == null) {
0081: // defaulting to the HtmlUnitDialog implementation.
0082: dialog = initializeDialog();
0083: }
0084: return dialog;
0085: }
0086:
0087: /**
0088: * Initializes the IJWebUnitDialog when the dialog is null. This will construct a new instance of the dialog based
0089: * on the specified testing engine key.
0090: */
0091: protected IJWebUnitDialog initializeDialog() {
0092: IJWebUnitDialog theIJWebUnitDialog = null;
0093: String theTestingEngineKey = getTestingEngineKey();
0094: Class theClass;
0095: try {
0096: theClass = TestingEngineRegistry
0097: .getTestingEngineClass(theTestingEngineKey);
0098: } catch (ClassNotFoundException e1) {
0099: throw new RuntimeException(e1);
0100: }
0101: try {
0102: theIJWebUnitDialog = (IJWebUnitDialog) theClass
0103: .newInstance();
0104: } catch (InstantiationException e) {
0105: e.printStackTrace();
0106: throw new RuntimeException(
0107: "Can't Instantiate Testing Engine with class ["
0108: + theClass + "] with key ["
0109: + theTestingEngineKey + "].", e);
0110: } catch (IllegalAccessException e) {
0111: e.printStackTrace();
0112: throw new RuntimeException(
0113: "IllegalAccessException using class [" + theClass
0114: + "] with key [" + theTestingEngineKey
0115: + "].", e);
0116: }
0117:
0118: return theIJWebUnitDialog;
0119: }
0120:
0121: /**
0122: * Close the current conversation.
0123: */
0124: public void closeBrowser() {
0125: try {
0126: getTestingEngine().closeBrowser();
0127: } catch (ExpectedJavascriptAlertException e) {
0128: Assert.fail("You previously tell that alert with message ["
0129: + e.getAlertMessage()
0130: + "] was expected, but nothing appeared.");
0131: } catch (ExpectedJavascriptConfirmException e) {
0132: Assert
0133: .fail("You previously tell that confirm with message ["
0134: + e.getConfirmMessage()
0135: + "] was expected, but nothing appeared.");
0136: } catch (ExpectedJavascriptPromptException e) {
0137: Assert
0138: .fail("You previously tell that prompt with message ["
0139: + e.getPromptMessage()
0140: + "] was expected, but nothing appeared.");
0141: }
0142: }
0143:
0144: /**
0145: * Close the current window.
0146: */
0147: public void closeWindow() {
0148: getTestingEngine().closeWindow();
0149: }
0150:
0151: /**
0152: * Set the testing engine.
0153: *
0154: * @param aIJWebUnitDialog Testing engine.
0155: */
0156: public void setDialog(IJWebUnitDialog aIJWebUnitDialog) {
0157: dialog = aIJWebUnitDialog;
0158: }
0159:
0160: /**
0161: * Provide access to test testContext.
0162: *
0163: * @return TestContext
0164: */
0165: public TestContext getTestContext() {
0166: if (testContext == null) {
0167: // defaulting to the original implementation.
0168: testContext = new TestContext();
0169: }
0170: return testContext;
0171: }
0172:
0173: /**
0174: * Allows setting an external test testContext class that might be extended from TestContext. Example:
0175: * setTestContext(new CompanyATestContext());
0176: *
0177: * CompanyATestContext extends TestContext.
0178: *
0179: * @param aTestContext
0180: */
0181: public void setTestContext(TestContext aTestContext) {
0182: testContext = aTestContext;
0183: }
0184:
0185: /**
0186: * Begin conversation at a URL absolute or relative to base URL. Use
0187: * {@link TestContext#setBaseUrl(String) getTestContext().setBaseUrl(String)} to define base URL. Absolute URL
0188: * should start with "http://", "https://" or "www.".
0189: *
0190: * @param url absolute or relative URL (relative to base URL).
0191: * @throws TestingEngineResponseException If something bad happend (404)
0192: */
0193: public void beginAt(String aRelativeURL)
0194: throws TestingEngineResponseException {
0195: try {
0196: getTestingEngine().beginAt(
0197: createUrl(aRelativeURL, getTestContext()
0198: .getBaseUrl()), testContext);
0199: } catch (MalformedURLException e) {
0200: Assert.fail(e.getLocalizedMessage());
0201: }
0202:
0203: }
0204:
0205: private URL createUrl(String url, URL baseURL)
0206: throws MalformedURLException {
0207: if (url.startsWith("http://") || url.startsWith("https://")
0208: || url.startsWith("file://")) {
0209: return new URL(url);
0210: } else if (url.startsWith("www.")) {
0211: return new URL("http://" + url);
0212: } else {
0213: url = url.startsWith("/") ? url.substring(1) : url;
0214: return new URL(baseURL, url);
0215: }
0216: }
0217:
0218: /**
0219: * Return the value of a web resource based on its key. This translates to a property file lookup with the locale
0220: * based on the current TestContext.
0221: *
0222: * @param key name of the web resource.
0223: * @return value of the web resource, encoded according to TestContext.
0224: */
0225: public String getMessage(String key) {
0226: String message = "";
0227: Locale locale = testContext.getLocale();
0228: try {
0229: message = ResourceBundle.getBundle(
0230: getTestContext().getResourceBundleName(), locale)
0231: .getString(key);
0232: } catch (Exception e) {
0233: e.printStackTrace();
0234: throw new RuntimeException("No message found for key ["
0235: + key + "].", e);
0236: }
0237: return message;
0238: }
0239:
0240: /**
0241: * Return the value of a web resource based on its key, using MessageFormat
0242: * to perform parametric substitution with formatting.
0243: *
0244: * @see MessageFormat
0245: * @param key
0246: * name of the web resource.
0247: * @param args
0248: * array of arguments to be formatted into message
0249: * @return value of the web resource after formatting
0250: */
0251: public String getMessage(String key, Object[] args) {
0252: String message = getMessage(key);
0253:
0254: // TODO: Struts escapes single quotes... maybe this should too
0255:
0256: MessageFormat format = new MessageFormat(message,
0257: getTestContext().getLocale());
0258:
0259: return format.format(args);
0260: }
0261:
0262: // Assertions
0263:
0264: /**
0265: * Assert title of current html page in conversation matches an expected
0266: * value.
0267: *
0268: * @param title
0269: * expected title value
0270: */
0271: public void assertTitleEquals(String title) {
0272: Assert.assertEquals(title, getTestingEngine().getPageTitle());
0273: }
0274:
0275: /**
0276: * Assert title of current html page in conversation matches an expected regexp.
0277: *
0278: * @param regexp expected title regexp
0279: */
0280: public void assertTitleMatch(String regexp) {
0281: RE re = null;
0282: try {
0283: re = new RE(regexp, RE.MATCH_SINGLELINE);
0284: } catch (RESyntaxException e) {
0285: Assert.fail(e.toString());
0286: }
0287: Assert.assertTrue("Unable to match [" + regexp + "] in title",
0288: re.match(getTestingEngine().getPageTitle()));
0289: }
0290:
0291: /**
0292: * Assert title of current html page matches the value of a specified web
0293: * resource.
0294: *
0295: * @param titleKey
0296: * web resource key for title
0297: */
0298: public void assertTitleEqualsKey(String titleKey) {
0299: Assert.assertEquals(getMessage(titleKey), getTestingEngine()
0300: .getPageTitle());
0301: }
0302:
0303: /**
0304: * Assert title of current page matches formatted message resource
0305: *
0306: * @param titleKey
0307: * @param args
0308: */
0309: public void assertTitleEqualsKey(String titleKey, Object[] args) {
0310: Assert.assertEquals(getMessage(titleKey, args),
0311: getTestingEngine().getPageTitle());
0312: }
0313:
0314: /**
0315: * Assert that a web resource's value is present.
0316: *
0317: * @param key
0318: * web resource name
0319: */
0320: public void assertKeyPresent(String key) {
0321: assertTextPresent(getMessage(key));
0322: }
0323:
0324: /**
0325: * Assert that a web resource's value (with formatting) is present
0326: *
0327: * @param key
0328: * @param args
0329: */
0330: public void assertKeyPresent(String key, Object[] args) {
0331: assertTextPresent(getMessage(key, args));
0332: }
0333:
0334: /**
0335: * Assert that supplied text is present.
0336: *
0337: * @param text
0338: */
0339: public void assertTextPresent(String text) {
0340: if (!(getTestingEngine().getPageText().indexOf(text) >= 0))
0341: Assert.fail("Expected text not found in current page: ["
0342: + text + "]\n Page content was: ["
0343: + getTestingEngine().getPageText() + "]");
0344: }
0345:
0346: /**
0347: * Assert that supplied regexp is matched.
0348: *
0349: * @param regexp
0350: */
0351: public void assertMatch(String regexp) {
0352: RE re = getRE(regexp);
0353: if (!re.match(getTestingEngine().getPageText()))
0354: Assert.fail("Expected rexexp not matched in response: ["
0355: + regexp + "]");
0356: }
0357:
0358: /**
0359: * Assert that a web resource's value is not present.
0360: *
0361: * @param key web resource name
0362: */
0363: public void assertKeyNotPresent(String key) {
0364: assertTextNotPresent(getMessage(key));
0365: }
0366:
0367: /**
0368: * Assert that a web resource's formatted value is not present.
0369: *
0370: * @param key
0371: * web resource name
0372: */
0373: public void assertKeyNotPresent(String key, Object[] args) {
0374: assertTextNotPresent(getMessage(key, args));
0375: }
0376:
0377: /**
0378: * Assert that supplied text is not present.
0379: *
0380: * @param text
0381: */
0382: public void assertTextNotPresent(String text) {
0383: if (getTestingEngine().getPageText().indexOf(text) >= 0)
0384: Assert.fail("Text found in response when not expected: ["
0385: + text + "]");
0386: }
0387:
0388: /**
0389: * Assert that supplied regexp is not present.
0390: *
0391: * @param regexp
0392: */
0393: public void assertNoMatch(String regexp) {
0394: RE re = getRE(regexp);
0395: if (re.match(getTestingEngine().getPageText()))
0396: Assert
0397: .fail("Regexp matched in response when not expected: ["
0398: + regexp + "]");
0399: }
0400:
0401: /**
0402: *
0403: * @param tableSummaryNameOrId
0404: * @return Object that represent a html table in a way independent from plugin.
0405: */
0406: public Table getTable(String tableSummaryNameOrId) {
0407: return getTestingEngine().getTable(tableSummaryNameOrId);
0408: }
0409:
0410: /**
0411: * Assert that a table with a given summary or id value is present.
0412: *
0413: * @param tableSummaryNameOrId summary, name or id attribute value of table
0414: */
0415: public void assertTablePresent(String tableSummaryNameOrId) {
0416: if (!getTestingEngine().hasTable(tableSummaryNameOrId))
0417: Assert.fail("Unable to locate table \""
0418: + tableSummaryNameOrId + "\"");
0419: }
0420:
0421: /**
0422: * Assert that a table with a given summary or id value is not present.
0423: *
0424: * @param tableSummaryNameOrId summary, name or id attribute value of table
0425: */
0426: public void assertTableNotPresent(String tableSummaryNameOrId) {
0427: if (getTestingEngine().hasTable(tableSummaryNameOrId))
0428: Assert.fail("Located table \"" + tableSummaryNameOrId
0429: + "\"");
0430: }
0431:
0432: /**
0433: * Assert that the value of a given web resource is present in a specific table.
0434: *
0435: * @param tableSummaryOrId summary or id attribute value of table
0436: * @param key web resource name
0437: */
0438: public void assertKeyInTable(String tableSummaryOrId, String key) {
0439: assertTextInTable(tableSummaryOrId, getMessage(key));
0440: }
0441:
0442: /**
0443: * Assert that the value of a given web resource is present in a specific
0444: * table.
0445: *
0446: * @param tableSummaryOrId
0447: * summary or id attribute value of table
0448: * @param key
0449: * web resource name
0450: */
0451: public void assertKeyInTable(String tableSummaryOrId, String key,
0452: Object[] args) {
0453: assertTextInTable(tableSummaryOrId, getMessage(key, args));
0454: }
0455:
0456: /**
0457: * Assert that supplied text is present in a specific table.
0458: *
0459: * @param tableSummaryNameOrId
0460: * summary, name or id attribute value of table
0461: * @param text
0462: */
0463: public void assertTextInTable(String tableSummaryNameOrId,
0464: String text) {
0465: assertTablePresent(tableSummaryNameOrId);
0466: Assert.assertTrue("Could not find: [" + text + "]"
0467: + "in table [" + tableSummaryNameOrId + "]",
0468: getTestingEngine().getTable(tableSummaryNameOrId)
0469: .hasText(text));
0470: }
0471:
0472: /**
0473: * Assert that supplied regexp is matched in a specific table.
0474: *
0475: * @param tableSummaryNameOrId summary, name or id attribute value of table
0476: * @param regexp
0477: */
0478: public void assertMatchInTable(String tableSummaryNameOrId,
0479: String regexp) {
0480: assertTablePresent(tableSummaryNameOrId);
0481: Assert.assertTrue("Could not match: [" + regexp + "]"
0482: + "in table [" + tableSummaryNameOrId + "]",
0483: getTestingEngine().getTable(tableSummaryNameOrId)
0484: .hasMatch(regexp));
0485: }
0486:
0487: /**
0488: * Assert that the values of a set of web resources are all present in a specific table.
0489: *
0490: * @param tableSummaryOrId summary, name or id attribute value of table
0491: * @param keys Array of web resource names.
0492: */
0493: public void assertKeysInTable(String tableSummaryOrId, String[] keys) {
0494: for (int i = 0; i < keys.length; i++) {
0495: assertKeyInTable(tableSummaryOrId, keys[i]);
0496: }
0497: }
0498:
0499: /**
0500: * Assert that the values of a set of web resources are all present in a
0501: * specific table.
0502: *
0503: * @param tableSummaryOrId
0504: * summary or id attribute value of table
0505: * @param keys
0506: * Array of web resource names.
0507: */
0508: public void assertKeysInTable(String tableSummaryOrId,
0509: String[] keys, Object[][] args) {
0510: for (int i = 0; i < keys.length; i++) {
0511: assertKeyInTable(tableSummaryOrId, keys[i], args[i]);
0512: }
0513: }
0514:
0515: /**
0516: * Assert that a set of text values are all present in a specific table.
0517: *
0518: * @param tableSummaryOrId
0519: * summary, name or id attribute value of table
0520: * @param text
0521: * Array of expected text values.
0522: */
0523: public void assertTextInTable(String tableSummaryOrId, String[] text) {
0524: for (int i = 0; i < text.length; i++) {
0525: assertTextInTable(tableSummaryOrId, text[i]);
0526: }
0527: }
0528:
0529: /**
0530: * Assert that a set of regexp values are all matched in a specific table.
0531: *
0532: * @param tableSummaryOrId summary, name or id attribute value of table
0533: * @param text Array of expected regexps to match.
0534: */
0535: public void assertMatchInTable(String tableSummaryOrId,
0536: String[] regexp) {
0537: for (int i = 0; i < regexp.length; i++) {
0538: assertMatchInTable(tableSummaryOrId, regexp[i]);
0539: }
0540: }
0541:
0542: /**
0543: * Assert that the value of a given web resource is not present in a specific table.
0544: *
0545: * @param tableSummaryOrId summary, name or id attribute value of table
0546: * @param key web resource name
0547: */
0548: public void assertKeyNotInTable(String tableSummaryOrId, String key) {
0549: assertTextNotInTable(tableSummaryOrId, getMessage(key));
0550: }
0551:
0552: /**
0553: * Assert that supplied text is not present in a specific table.
0554: *
0555: * @param tableSummaryNameOrId summary, name or id attribute value of table
0556: * @param text
0557: */
0558: public void assertTextNotInTable(String tableSummaryNameOrId,
0559: String text) {
0560: assertTablePresent(tableSummaryNameOrId);
0561: Assert.assertTrue("Found text: [" + text + "] in table ["
0562: + tableSummaryNameOrId + "]", !getTestingEngine()
0563: .getTable(tableSummaryNameOrId).hasText(text));
0564: }
0565:
0566: /**
0567: * Assert that none of a set of text values are present in a specific table.
0568: *
0569: * @param tableSummaryNameOrId summary, name or id attribute value of table
0570: * @param text Array of text values
0571: */
0572: public void assertTextNotInTable(String tableSummaryNameOrId,
0573: String[] text) {
0574: for (int i = 0; i < text.length; i++) {
0575: assertTextNotInTable(tableSummaryNameOrId, text[i]);
0576: }
0577: }
0578:
0579: /**
0580: * Assert that supplied regexp is not present in a specific table.
0581: *
0582: * @param tableSummaryNameOrId summary, name or id attribute value of table
0583: * @param text
0584: */
0585: public void assertNoMatchInTable(String tableSummaryNameOrId,
0586: String regexp) {
0587: assertTablePresent(tableSummaryNameOrId);
0588: Assert.assertTrue("Found regexp: [" + regexp + "] in table ["
0589: + tableSummaryNameOrId + "]", !getTestingEngine()
0590: .getTable(tableSummaryNameOrId).hasMatch(regexp));
0591: }
0592:
0593: /**
0594: * Assert that none of a set of regexp values are present in a specific table.
0595: *
0596: * @param tableSummaryNameOrId summary, name or id attribute value of table
0597: * @param text Array of text values
0598: */
0599: public void assertNoMatchInTable(String tableSummaryNameOrId,
0600: String[] regexp) {
0601: for (int i = 0; i < regexp.length; i++) {
0602: assertNoMatchInTable(tableSummaryNameOrId, regexp[i]);
0603: }
0604: }
0605:
0606: /**
0607: * Assert that a specific table matches an ExpectedTable.
0608: *
0609: * @param tableSummaryNameOrId summary, name or id attribute value of table
0610: * @param expectedTable represents expected values (colspan supported).
0611: */
0612: public void assertTableEquals(String tableSummaryNameOrId,
0613: Table expectedTable) {
0614: getTestingEngine().getTable(tableSummaryNameOrId).assertEquals(
0615: expectedTable);
0616: }
0617:
0618: /**
0619: * Assert that a specific table matches a matrix of supplied text values.
0620: *
0621: * @param tableSummaryNameOrId summary, name or id attribute value of table
0622: * @param expectedCellValues double dimensional array of expected values
0623: */
0624: public void assertTableEquals(String tableSummaryNameOrId,
0625: String[][] expectedCellValues) {
0626: getTestingEngine().getTable(tableSummaryNameOrId).assertEquals(
0627: new Table(expectedCellValues));
0628: }
0629:
0630: /**
0631: * Assert that a range of rows for a specific table matches a matrix of supplied text values.
0632: *
0633: * @param tableSummaryNameOrId summary, name or id attribute value of table
0634: * @param startRow index of start row for comparison
0635: * @param expectedTable represents expected values (colspan and rowspan supported).
0636: */
0637: public void assertTableRowsEqual(String tableSummaryNameOrId,
0638: int startRow, Table expectedTable) {
0639: getTestingEngine().getTable(tableSummaryNameOrId)
0640: .assertSubTableEquals(startRow, expectedTable);
0641: }
0642:
0643: /**
0644: * Assert that a range of rows for a specific table matches a matrix of supplied text values.
0645: *
0646: * @param tableSummaryNameOrId summary, name or id attribute value of table
0647: * @param startRow index of start row for comparison
0648: * @param expectedTable represents expected values (colspan and rowspan supported).
0649: */
0650: public void assertTableRowsEqual(String tableSummaryNameOrId,
0651: int startRow, String[][] expectedTable) {
0652: getTestingEngine().getTable(tableSummaryNameOrId)
0653: .assertSubTableEquals(startRow,
0654: new Table(expectedTable));
0655: }
0656:
0657: /**
0658: * Assert that the number of rows for a specific table equals expected value.
0659: *
0660: * @param tableSummaryNameOrId summary, name or id attribute value of table
0661: * @param expectedRowCount expected row count.
0662: */
0663: public void assertTableRowCountEquals(String tableSummaryNameOrId,
0664: int expectedRowCount) {
0665: assertTablePresent(tableSummaryNameOrId);
0666: int actualRowCount = getTestingEngine().getTable(
0667: tableSummaryNameOrId).getRowCount();
0668: Assert.assertTrue("Expected row count was " + expectedRowCount
0669: + " but actual row count is " + actualRowCount,
0670: actualRowCount == expectedRowCount);
0671: }
0672:
0673: /**
0674: * Assert that a specific table matches an ExpectedTable.
0675: *
0676: * @param tableSummaryOrId summary or id attribute value of table
0677: * @param expectedTable represents expected regexps (colspan supported).
0678: */
0679: public void assertTableMatch(String tableSummaryOrId,
0680: Table expectedTable) {
0681: getTestingEngine().getTable(tableSummaryOrId).assertMatch(
0682: expectedTable);
0683: }
0684:
0685: /**
0686: * Assert that a specific table matches a matrix of supplied regexps.
0687: *
0688: * @param tableSummaryOrId summary or id attribute value of table
0689: * @param expectedCellValues double dimensional array of expected regexps
0690: */
0691: public void assertTableMatch(String tableSummaryOrId,
0692: String[][] expectedCellValues) {
0693: getTestingEngine().getTable(tableSummaryOrId).assertMatch(
0694: new Table(expectedCellValues));
0695: }
0696:
0697: /**
0698: * Assert that a range of rows for a specific table matches a matrix of supplied regexps.
0699: *
0700: * @param tableSummaryOrId summary or id attribute value of table
0701: * @param startRow index of start row for comparison
0702: * @param expectedTable represents expected regexps (colspan and rowspan supported).
0703: */
0704: public void assertTableRowsMatch(String tableSummaryOrId,
0705: int startRow, Table expectedTable) {
0706: getTestingEngine().getTable(tableSummaryOrId)
0707: .assertSubTableMatch(startRow, expectedTable);
0708: }
0709:
0710: /**
0711: * Assert that a range of rows for a specific table matches a matrix of supplied regexps.
0712: *
0713: * @param tableSummaryOrId summary or id attribute value of table
0714: * @param startRow index of start row for comparison
0715: * @param expectedTable represents expected regexps (colspan and rowspan not supported).
0716: */
0717: public void assertTableRowsMatch(String tableSummaryOrId,
0718: int startRow, String[][] expectedTable) {
0719: getTestingEngine()
0720: .getTable(tableSummaryOrId)
0721: .assertSubTableMatch(startRow, new Table(expectedTable));
0722: }
0723:
0724: /**
0725: * Assert that a form input element with a given name is present.
0726: *
0727: * @param formElementName
0728: */
0729: public void assertFormElementPresent(String formElementName) {
0730: assertFormPresent();
0731: Assert.assertTrue("Did not find form element with name ["
0732: + formElementName + "].", getTestingEngine()
0733: .hasFormParameterNamed(formElementName));
0734: }
0735:
0736: /**
0737: * Assert that a form input element with a given name is not present.
0738: *
0739: * @param formElementName
0740: */
0741: public void assertFormElementNotPresent(String formElementName) {
0742: assertFormPresent();
0743: try {
0744: Assert
0745: .assertTrue("Found form element with name ["
0746: + formElementName + "] when not expected.",
0747: !getDialog().hasFormParameterNamed(
0748: formElementName));
0749: } catch (UnableToSetFormException e) {
0750: // assertFormControlNotPresent
0751: }
0752: }
0753:
0754: /**
0755: * Assert that a form checkbox with a given name is present.
0756: *
0757: * @param checkboxName checkbox name.
0758: */
0759: public void assertCheckboxPresent(String checkboxName) {
0760: assertFormPresent();
0761: Assert.assertTrue("Did not find form checkbox with name ["
0762: + checkboxName + "].", getTestingEngine()
0763: .hasElementByXPath(
0764: "//input[@type='checkbox' and @name='"
0765: + checkboxName + "']"));
0766: }
0767:
0768: /**
0769: * Assert that a given checkbox is present.
0770: *
0771: * @param checkboxName checkbox name attribut.
0772: * @param checkboxValue checkbox value attribut.
0773: */
0774: public void assertCheckboxPresent(String checkboxName,
0775: String checkboxValue) {
0776: assertFormPresent();
0777: Assert.assertTrue(
0778: "Did not find form checkbox with name [" + checkboxName
0779: + "] and value [" + checkboxValue + "].",
0780: getTestingEngine().hasElementByXPath(
0781: "//input[@type='checkbox' and @name='"
0782: + checkboxName + "' and @value='"
0783: + checkboxValue + "']"));
0784: }
0785:
0786: /**
0787: * Assert that a form checkbox with a given name is not present.
0788: *
0789: * @param checkboxName checkbox name.
0790: */
0791: public void assertCheckboxNotPresent(String checkboxName) {
0792: assertFormPresent();
0793: Assert.assertFalse("Found form checkbox with name ["
0794: + checkboxName + "] when not expected.",
0795: getTestingEngine().hasElementByXPath(
0796: "//input[@type='checkbox' and @name='"
0797: + checkboxName + "']"));
0798: }
0799:
0800: /**
0801: * Assert that a given checkbox is not present.
0802: *
0803: * @param checkboxName checkbox name.
0804: * @param checkboxValue checkbox value attribut.
0805: */
0806: public void assertCheckboxNotPresent(String checkboxName,
0807: String checkboxValue) {
0808: assertFormPresent();
0809: Assert.assertFalse("Found form checkbox with name ["
0810: + checkboxName + "] and value [" + checkboxValue
0811: + "] when not expected.", getTestingEngine()
0812: .hasElementByXPath(
0813: "//input[@type='checkbox' and @name='"
0814: + checkboxName + "' and @value='"
0815: + checkboxValue + "']"));
0816: }
0817:
0818: /**
0819: * Assert that there is a form present.
0820: *
0821: */
0822: public void assertFormPresent() {
0823: Assert.assertTrue("No form present", getTestingEngine()
0824: .hasForm());
0825: }
0826:
0827: /**
0828: * Assert that there is a form with the specified name or id present.
0829: *
0830: * @param nameOrID
0831: */
0832: public void assertFormPresent(String nameOrID) {
0833: Assert.assertTrue("No form present with name or id ["
0834: + nameOrID + "]", getTestingEngine().hasForm(nameOrID));
0835: }
0836:
0837: /**
0838: * Assert that there is not a form present.
0839: *
0840: */
0841: public void assertFormNotPresent() {
0842: Assert.assertFalse("A form is present", getTestingEngine()
0843: .hasForm());
0844: }
0845:
0846: /**
0847: * Assert that there is not a form with the specified name or id present.
0848: *
0849: * @param nameOrID
0850: */
0851: public void assertFormNotPresent(String nameOrID) {
0852: Assert.assertFalse("Form present with name or id [" + nameOrID
0853: + "]", getTestingEngine().hasForm(nameOrID));
0854: }
0855:
0856: /**
0857: * Assert that a specific form element has an expected value. Can be used to check hidden input.
0858: *
0859: * @param formElementName
0860: * @param expectedValue
0861: * @deprecated
0862: */
0863: public void assertFormElementEquals(String formElementName,
0864: String expectedValue) {
0865: assertFormElementPresent(formElementName);
0866: Assert.assertEquals(expectedValue, getDialog()
0867: .getElementAttributByXPath(
0868: "//input[@name='" + formElementName + "']",
0869: "value"));
0870: }
0871:
0872: /**
0873: * Assert that a specific form element matches an expected regexp.
0874: *
0875: * @param formElementName
0876: * @param regexp
0877: * @deprecated
0878: */
0879: public void assertFormElementMatch(String formElementName,
0880: String regexp) {
0881: assertFormElementPresent(formElementName);
0882: RE re = null;
0883: try {
0884: re = new RE(regexp, RE.MATCH_SINGLELINE);
0885: } catch (RESyntaxException e) {
0886: Assert.fail(e.toString());
0887: }
0888: Assert.assertTrue("Unable to match [" + regexp
0889: + "] in form element \"" + formElementName + "\"", re
0890: .match(getDialog().getElementAttributByXPath(
0891: "//input[@name='" + formElementName + "']",
0892: "value")));
0893: }
0894:
0895: /**
0896: * Assert that a form element had no value / is empty.
0897: *
0898: * @param formElementName
0899: * @deprecated
0900: */
0901: public void assertFormElementEmpty(String formElementName) {
0902: assertFormElementPresent(formElementName);
0903: Assert.assertEquals("", getTestingEngine()
0904: .getElementAttributByXPath(
0905: "//input[@name='" + formElementName + "']",
0906: "value"));
0907: }
0908:
0909: /**
0910: * Assert that an input text element with name <code>formElementName</code> has the <code>expectedValue</code>
0911: * value.
0912: *
0913: * @param formElementName the value of the name attribute of the element
0914: * @param expectedValue the expected value of the given input element
0915: */
0916: public void assertTextFieldEquals(String formElementName,
0917: String expectedValue) {
0918: assertFormElementPresent(formElementName);
0919: Assert.assertEquals(expectedValue, getTestingEngine()
0920: .getTextFieldValue(formElementName));
0921: }
0922:
0923: /**
0924: * Assert that an input hidden element with name <code>formElementName</code> has the <code>expectedValue</code>
0925: * value.
0926: *
0927: * @param formElementName the value of the name attribute of the element
0928: * @param expectedValue the expected value of the given input element
0929: */
0930: public void assertHiddenFieldPresent(String formElementName,
0931: String expectedValue) {
0932: assertFormElementPresent(formElementName);
0933: Assert.assertEquals(expectedValue, getTestingEngine()
0934: .getHiddenFieldValue(formElementName));
0935: }
0936:
0937: /**
0938: * Assert that a specific checkbox is selected.
0939: *
0940: * @param checkBoxName
0941: */
0942: public void assertCheckboxSelected(String checkBoxName) {
0943: assertCheckboxPresent(checkBoxName);
0944: if (!getTestingEngine().isCheckboxSelected(checkBoxName)) {
0945: Assert.fail("Checkbox with name [" + checkBoxName
0946: + "] was not found selected.");
0947: }
0948: }
0949:
0950: /**
0951: * Assert that a specific checkbox is selected.
0952: *
0953: * @param checkBoxName
0954: * @param checkBoxValue
0955: */
0956: public void assertCheckboxSelected(String checkBoxName,
0957: String checkBoxValue) {
0958: assertCheckboxPresent(checkBoxName, checkBoxValue);
0959: if (!getTestingEngine().isCheckboxSelected(checkBoxName,
0960: checkBoxValue)) {
0961: Assert.fail("Checkbox with name [" + checkBoxName
0962: + "] and value [" + checkBoxValue
0963: + "] was not found selected.");
0964: }
0965: }
0966:
0967: /**
0968: * Assert that a specific checkbox is not selected.
0969: *
0970: * @param checkBoxName
0971: */
0972: public void assertCheckboxNotSelected(String checkBoxName) {
0973: assertCheckboxPresent(checkBoxName);
0974: if (getTestingEngine().isCheckboxSelected(checkBoxName)) {
0975: Assert.fail("Checkbox with name [" + checkBoxName
0976: + "] was found selected.");
0977: }
0978: }
0979:
0980: /**
0981: * Assert that a specific checkbox is not selected.
0982: *
0983: * @param checkBoxName
0984: * @param checkBoxValue
0985: */
0986: public void assertCheckboxNotSelected(String checkBoxName,
0987: String checkBoxValue) {
0988: assertCheckboxPresent(checkBoxName, checkBoxValue);
0989: if (getTestingEngine().isCheckboxSelected(checkBoxName,
0990: checkBoxValue)) {
0991: Assert.fail("Checkbox with name [" + checkBoxName
0992: + "] and value [" + checkBoxValue
0993: + "] was found selected.");
0994: }
0995: }
0996:
0997: /**
0998: * Assert that a specific option is present in a radio group.
0999: *
1000: * @param name radio group name.
1001: * @param radioOption option to test for.
1002: */
1003: public void assertRadioOptionPresent(String name, String radioOption) {
1004: assertFormElementPresent(name);
1005: if (!getTestingEngine().hasRadioOption(name, radioOption)) {
1006: Assert.fail("Unable to find option [" + radioOption
1007: + "] in radio group [" + name + "]");
1008: }
1009: }
1010:
1011: /**
1012: * Assert that a specific option is not present in a radio group.
1013: *
1014: * @param name radio group name.
1015: * @param radioOption option to test for.
1016: */
1017: public void assertRadioOptionNotPresent(String name,
1018: String radioOption) {
1019: assertFormElementPresent(name);
1020: if (getTestingEngine().hasRadioOption(name, radioOption))
1021: Assert.fail("Found option [" + radioOption
1022: + "] in radio group [" + name + "]");
1023: }
1024:
1025: /**
1026: * Assert that a specific option is selected in a radio group.
1027: *
1028: * @param name radio group name.
1029: * @param radioOption option to test for selection.
1030: */
1031: public void assertRadioOptionSelected(String name,
1032: String radioOption) {
1033: assertRadioOptionPresent(name, radioOption);
1034: Assert.assertEquals(radioOption, getTestingEngine()
1035: .getSelectedRadio(name));
1036: }
1037:
1038: /**
1039: * Assert that a specific option is not selected in a radio group.
1040: *
1041: * @param name radio group name.
1042: * @param radioOption option to test for selection.
1043: */
1044: public void assertRadioOptionNotSelected(String name,
1045: String radioOption) {
1046: assertRadioOptionPresent(name, radioOption);
1047: Assert.assertFalse("Radio option [" + radioOption
1048: + "] is selected.", radioOption
1049: .equals(getTestingEngine().getSelectedRadio(name)));
1050: }
1051:
1052: /**
1053: * Assert that given options are present in a select box (by label).
1054: *
1055: * @param selectName name of the select element.
1056: * @param optionLabels option labels.
1057: */
1058: public void assertSelectOptionsPresent(String selectName,
1059: String[] optionLabels) {
1060: assertFormElementPresent(selectName);
1061: for (int i = 0; i < optionLabels.length; i++)
1062: Assert.assertTrue("Option [" + optionLabels[i]
1063: + "] not found in select element " + selectName,
1064: getTestingEngine().hasSelectOption(selectName,
1065: optionLabels[i]));
1066: }
1067:
1068: /**
1069: * Assert that a specific option is present in a select box (by label).
1070: *
1071: * @param selectName name of the select element.
1072: * @param optionLabel option label.
1073: */
1074: public void assertSelectOptionPresent(String selectName,
1075: String optionLabel) {
1076: assertSelectOptionsPresent(selectName,
1077: new String[] { optionLabel });
1078: }
1079:
1080: /**
1081: * Assert that given options are present in a select box (by value).
1082: *
1083: * @param selectName name of the select element.
1084: * @param optionValues option labels.
1085: */
1086: public void assertSelectOptionValuesPresent(String selectName,
1087: String[] optionValues) {
1088: assertFormElementPresent(selectName);
1089: for (int i = 0; i < optionValues.length; i++)
1090: Assert.assertTrue("Option [" + optionValues[i]
1091: + "] not found in select element " + selectName,
1092: getTestingEngine().hasSelectOptionValue(selectName,
1093: optionValues[i]));
1094: }
1095:
1096: /**
1097: * Assert that a specific option is present in a select box (by value).
1098: *
1099: * @param selectName name of the select element.
1100: * @param optionValue option value.
1101: */
1102: public void assertSelectOptionValuePresent(String selectName,
1103: String optionValue) {
1104: assertSelectOptionValuesPresent(selectName,
1105: new String[] { optionValue });
1106: }
1107:
1108: public void assertSelectOptionValueNotPresent(String selectName,
1109: String optionValue) {
1110: try {
1111: assertSelectOptionValuePresent(selectName, optionValue);
1112: } catch (AssertionFailedError e) {
1113: return;
1114: }
1115: Assert.fail("Option value" + optionValue
1116: + " found in select element " + selectName
1117: + " when not expected.");
1118: }
1119:
1120: /**
1121: * Assert that a specific option is not present in a select box.
1122: *
1123: * @param selectName name of the select element.
1124: * @param expectedOption option label.
1125: */
1126: public void assertSelectOptionNotPresent(String selectName,
1127: String optionLabel) {
1128: try {
1129: assertSelectOptionPresent(selectName, optionLabel);
1130: } catch (AssertionFailedError e) {
1131: return;
1132: }
1133: Assert.fail("Option " + optionLabel
1134: + " found in select element " + selectName
1135: + " when not expected.");
1136: }
1137:
1138: /**
1139: * Assert that the display values of a select element's options match a given array of strings.
1140: *
1141: * @param selectName name of the select element.
1142: * @param expectedOptions expected labels for the select box.
1143: */
1144: public void assertSelectOptionsEqual(String selectName,
1145: String[] expectedOptions) {
1146: assertFormElementPresent(selectName);
1147: assertArraysEqual(expectedOptions, getOptionsFor(selectName));
1148: }
1149:
1150: /**
1151: * Assert that the display values of a select element's options do not match a given array of strings.
1152: *
1153: * @param selectName name of the select element.
1154: * @param expectedOptions expected display values for the select box.
1155: */
1156: public void assertSelectOptionsNotEqual(String selectName,
1157: String[] expectedOptions) {
1158: assertFormElementPresent(selectName);
1159: try {
1160: assertSelectOptionsEqual(selectName, expectedOptions);
1161: } catch (AssertionFailedError e) {
1162: return;
1163: }
1164: Assert.fail("Options not expected to be equal");
1165: }
1166:
1167: /**
1168: * Assert that the values of a select element's options match a given array of strings.
1169: *
1170: * @param selectName name of the select element.
1171: * @param expectedValues expected values for the select box.
1172: */
1173: public void assertSelectOptionValuesEqual(String selectName,
1174: String[] expectedValues) {
1175: assertFormElementPresent(selectName);
1176: assertArraysEqual(expectedValues, getTestingEngine()
1177: .getSelectOptionValues(selectName));
1178:
1179: }
1180:
1181: /**
1182: * Assert that the values of a select element's options do not match a given array of strings.
1183: *
1184: * @param selectName name of the select element.
1185: * @param optionValues expected values for the select box.
1186: */
1187: public void assertSelectOptionValuesNotEqual(String selectName,
1188: String[] optionValues) {
1189: assertFormElementPresent(selectName);
1190: try {
1191: assertSelectOptionValuesEqual(selectName, optionValues);
1192: } catch (AssertionFailedError e) {
1193: return;
1194: }
1195: Assert.fail("Values not expected to be equal");
1196: }
1197:
1198: /**
1199: * Assert that the currently selected display label(s) of a select box matches given label(s).
1200: *
1201: * @param selectName name of the select element.
1202: * @param labels expected display label(s) of the selected option.
1203: */
1204: public void assertSelectedOptionsEqual(String selectName,
1205: String[] labels) {
1206: assertFormElementPresent(selectName);
1207: Assert.assertEquals(labels.length, getTestingEngine()
1208: .getSelectedOptions(selectName).length);
1209: for (int i = 0; i < labels.length; i++)
1210: Assert.assertEquals(labels[i], getDialog()
1211: .getSelectOptionLabelForValue(
1212: selectName,
1213: getTestingEngine().getSelectedOptions(
1214: selectName)[i]));
1215: }
1216:
1217: public void assertSelectedOptionEquals(String selectName,
1218: String option) {
1219: assertSelectedOptionsEqual(selectName, new String[] { option });
1220: }
1221:
1222: /**
1223: * Assert that the currently selected value(s) of a select box matches given value(s).
1224: *
1225: * @param selectName name of the select element.
1226: * @param values expected value(s) of the selected option.
1227: */
1228: public void assertSelectedOptionValuesEqual(String selectName,
1229: String[] values) {
1230: assertFormElementPresent(selectName);
1231: Assert.assertEquals(values.length, getTestingEngine()
1232: .getSelectedOptions(selectName).length);
1233: for (int i = 0; i < values.length; i++)
1234: Assert.assertEquals(values[i], getTestingEngine()
1235: .getSelectedOptions(selectName)[i]);
1236: }
1237:
1238: /**
1239: * Assert that the currently selected value of a select box matches given value.
1240: *
1241: * @param selectName name of the select element.
1242: * @param value expected value of the selected option.
1243: */
1244: public void assertSelectedOptionValueEquals(String selectName,
1245: String value) {
1246: assertSelectedOptionValuesEqual(selectName,
1247: new String[] { value });
1248: }
1249:
1250: /**
1251: * Assert that the currently selected display value(s) of a select box matches a given value(s).
1252: *
1253: * @param selectName name of the select element.
1254: * @param regexps expected display value of the selected option.
1255: */
1256: public void assertSelectedOptionsMatch(String selectName,
1257: String[] regexps) {
1258: assertFormElementPresent(selectName);
1259: Assert.assertEquals(regexps.length, getTestingEngine()
1260: .getSelectedOptions(selectName).length);
1261: for (int i = 0; i < regexps.length; i++) {
1262: RE re = getRE(regexps[i]);
1263: Assert
1264: .assertTrue("Unable to match ["
1265: + regexps[i]
1266: + "] in option \""
1267: + getTestingEngine().getSelectedOptions(
1268: selectName)[i] + "\"", re
1269: .match(getTestingEngine()
1270: .getSelectedOptions(selectName)[i]));
1271: }
1272: }
1273:
1274: public void assertSelectedOptionMatches(String selectName,
1275: String regexp) {
1276: assertSelectedOptionsMatch(selectName, new String[] { regexp });
1277: }
1278:
1279: /**
1280: * Assert that a submit button is present. <br/> A submit button can be the following HTML elements:
1281: * <ul>
1282: * <li>submit input
1283: * <li>image input
1284: * <li>submit button
1285: * </ul>
1286: *
1287: */
1288: public void assertSubmitButtonPresent() {
1289: assertFormPresent();
1290: Assert.assertTrue("no submit button found.", getDialog()
1291: .hasSubmitButton());
1292: }
1293:
1294: /**
1295: * Assert that a submit button with a given name is present. <br/> A submit button can be the following HTML
1296: * elements:
1297: * <ul>
1298: * <li>submit input
1299: * <li>image input
1300: * <li>submit button
1301: * </ul>
1302: *
1303: * @param buttonName
1304: */
1305: public void assertSubmitButtonPresent(String buttonName) {
1306: assertFormPresent();
1307: Assert.assertTrue("Submit Button [" + buttonName
1308: + "] not found.", getTestingEngine().hasSubmitButton(
1309: buttonName));
1310: }
1311:
1312: /**
1313: * Assert that no submit button is present in the current form. <br/> A submit button can be the following HTML
1314: * elements:
1315: * <ul>
1316: * <li>submit input
1317: * <li>image input
1318: * <li>submit button
1319: * </ul>
1320: *
1321: * @param buttonName
1322: */
1323: public void assertSubmitButtonNotPresent() {
1324: assertFormPresent();
1325: Assert.assertFalse("Submit Button found.", getDialog()
1326: .hasSubmitButton());
1327: }
1328:
1329: /**
1330: * Assert that a submit button with a given name is not present. <br/> A submit button can be the following HTML
1331: * elements:
1332: * <ul>
1333: * <li>submit input
1334: * <li>image input
1335: * <li>submit button
1336: * </ul>
1337: *
1338: * @param buttonName
1339: */
1340: public void assertSubmitButtonNotPresent(String buttonName) {
1341: assertFormPresent();
1342: Assert.assertFalse("Submit Button [" + buttonName + "] found.",
1343: getTestingEngine().hasSubmitButton(buttonName));
1344: }
1345:
1346: /**
1347: * Assert that a submit button with a given name and value is present. <br/> A submit button can be the following
1348: * HTML elements:
1349: * <ul>
1350: * <li>submit input
1351: * <li>image input
1352: * <li>submit button
1353: * </ul>
1354: *
1355: * @param buttonName
1356: * @param buttonValue
1357: */
1358: public void assertSubmitButtonPresent(String buttonName,
1359: String buttonValue) {
1360: assertFormPresent();
1361: Assert.assertTrue("Submit Button [" + buttonName
1362: + "] with value [" + buttonValue + "] not found.",
1363: getTestingEngine().hasSubmitButton(buttonName,
1364: buttonValue));
1365: }
1366:
1367: /**
1368: * Assert that a reset button is present. <br/> A reset button can be the following HTML elements:
1369: * <ul>
1370: * <li>reset input
1371: * <li>reset button
1372: * </ul>
1373: *
1374: */
1375: public void assertResetButtonPresent() {
1376: assertFormPresent();
1377: Assert.assertTrue("no reset button found.", getDialog()
1378: .hasResetButton());
1379: }
1380:
1381: /**
1382: * Assert that a reset button with a given name is present.<br/> A reset button can be the following HTML elements:
1383: * <ul>
1384: * <li>reset input
1385: * <li>reset button
1386: * </ul>
1387: *
1388: * @param buttonName
1389: */
1390: public void assertResetButtonPresent(String buttonName) {
1391: assertFormPresent();
1392: Assert.assertTrue("Reset Button [" + buttonName
1393: + "] not found.", getTestingEngine().hasResetButton(
1394: buttonName));
1395: }
1396:
1397: /**
1398: * Assert that no reset button is present in the current form.<br/> A reset button can be the following HTML
1399: * elements:
1400: * <ul>
1401: * <li>reset input
1402: * <li>reset button
1403: * </ul>
1404: *
1405: * @param buttonName
1406: */
1407: public void assertResetButtonNotPresent() {
1408: assertFormPresent();
1409: Assert.assertFalse("Reset Button found.", getTestingEngine()
1410: .hasResetButton());
1411: }
1412:
1413: /**
1414: * Assert that a reset button with a given name is not present.<br/> A reset button can be the following HTML
1415: * elements:
1416: * <ul>
1417: * <li>reset input
1418: * <li>reset button
1419: * </ul>
1420: *
1421: * @param buttonName
1422: */
1423: public void assertResetButtonNotPresent(String buttonName) {
1424: assertFormPresent();
1425: Assert.assertFalse("Reset Button [" + buttonName + "] found.",
1426: getTestingEngine().hasResetButton(buttonName));
1427: }
1428:
1429: /**
1430: * Assert that a button with a given id is present in the current window.<br/> A button can be the following HTML
1431: * elements:
1432: * <ul>
1433: * <li>button input
1434: * <li>button button
1435: * </ul>
1436: *
1437: * @param buttonId
1438: */
1439: public void assertButtonPresent(String buttonId) {
1440: assertFormPresent();
1441: Assert.assertTrue("Button [" + buttonId + "] not found.",
1442: getDialog().hasButton(buttonId));
1443: }
1444:
1445: /**
1446: * Assert that a button with a given text is present in the current window.
1447: *
1448: * @param text Text representation of button content.
1449: */
1450: public void assertButtonPresentWithText(String text) {
1451: Assert.assertTrue("Did not find button with text [" + text
1452: + "].", getTestingEngine().hasButtonWithText(text));
1453: }
1454:
1455: /**
1456: * Assert that a button with a given text is not present in the current window.
1457: *
1458: * @param text Text representation of button content.
1459: */
1460: public void assertButtonNotPresentWithText(String text) {
1461: Assert.assertFalse("Found button with text [" + text + "].",
1462: getTestingEngine().hasButtonWithText(text));
1463: }
1464:
1465: /**
1466: * Assert that a button with a given id is not present in the current window.
1467: *
1468: * @param buttonId
1469: */
1470: public void assertButtonNotPresent(String buttonId) {
1471: assertFormPresent();
1472: Assert.assertFalse("Button [" + buttonId
1473: + "] found when not expected.", getTestingEngine()
1474: .hasButton(buttonId));
1475: }
1476:
1477: /**
1478: * Assert that a link with a given id is present in the response.
1479: *
1480: * @param linkId
1481: */
1482: public void assertLinkPresent(String linkId) {
1483: Assert.assertTrue("Unable to find link with id [" + linkId
1484: + "]", getTestingEngine().hasLink(linkId));
1485: }
1486:
1487: /**
1488: * Assert that no link with the given id is present in the response.
1489: *
1490: * @param linkId
1491: */
1492: public void assertLinkNotPresent(String linkId) {
1493: Assert.assertTrue("link with id [" + linkId
1494: + "] found in response", !getTestingEngine().hasLink(
1495: linkId));
1496: }
1497:
1498: /**
1499: * Assert that a link containing the supplied text is present.
1500: *
1501: * @param linkText
1502: */
1503: public void assertLinkPresentWithText(String linkText) {
1504: Assert.assertTrue("Link with text [" + linkText
1505: + "] not found in response.", getTestingEngine()
1506: .hasLinkWithText(linkText, 0));
1507: }
1508:
1509: /**
1510: * Assert that no link containing the supplied text is present.
1511: *
1512: * @param linkText
1513: */
1514: public void assertLinkNotPresentWithText(String linkText) {
1515: Assert.assertTrue("Link with text [" + linkText
1516: + "] found in response.", !getTestingEngine()
1517: .hasLinkWithText(linkText, 0));
1518: }
1519:
1520: /**
1521: * Assert that a link containing the supplied text is present.
1522: *
1523: * @param linkText
1524: * @param index The 0-based index, when more than one link with the same text is expected.
1525: */
1526: public void assertLinkPresentWithText(String linkText, int index) {
1527: Assert.assertTrue("Link with text [" + linkText
1528: + "] and index [" + index + "] not found in response.",
1529: getDialog().hasLinkWithText(linkText, index));
1530: }
1531:
1532: /**
1533: * Assert that no link containing the supplied text is present.
1534: *
1535: * @param linkText
1536: * @param index The 0-based index, when more than one link with the same text is expected.
1537: */
1538: public void assertLinkNotPresentWithText(String linkText, int index) {
1539: Assert.assertTrue("Link with text [" + linkText
1540: + "] and index " + index + " found in response.",
1541: !getTestingEngine().hasLinkWithText(linkText, index));
1542: }
1543:
1544: // BEGIN RFE 996031...
1545:
1546: /**
1547: * Assert that a link containing the Exact text is present.
1548: *
1549: * @param linkText
1550: */
1551: public void assertLinkPresentWithExactText(String linkText) {
1552: Assert.assertTrue("Link with Exact text [" + linkText
1553: + "] not found in response.", getTestingEngine()
1554: .hasLinkWithExactText(linkText, 0));
1555: }
1556:
1557: /**
1558: * Assert that no link containing the Exact text is present.
1559: *
1560: * @param linkText
1561: */
1562: public void assertLinkNotPresentWithExactText(String linkText) {
1563: Assert.assertTrue("Link with Exact text [" + linkText
1564: + "] found in response.", !getTestingEngine()
1565: .hasLinkWithExactText(linkText, 0));
1566: }
1567:
1568: /**
1569: * Assert that a link containing the Exact text is present.
1570: *
1571: * @param linkText
1572: * @param index The 0-based index, when more than one link with the same text is expected.
1573: */
1574: public void assertLinkPresentWithExactText(String linkText,
1575: int index) {
1576: Assert.assertTrue("Link with Exact text [" + linkText
1577: + "] and index [" + index + "] not found in response.",
1578: getDialog().hasLinkWithExactText(linkText, index));
1579: }
1580:
1581: /**
1582: * Assert that no link containing the Exact text is present.
1583: *
1584: * @param linkText
1585: * @param index The 0-based index, when more than one link with the same text is expected.
1586: */
1587: public void assertLinkNotPresentWithExactText(String linkText,
1588: int index) {
1589: Assert.assertTrue("Link with Exact text [" + linkText
1590: + "] and index " + index + " found in response.",
1591: !getDialog().hasLinkWithExactText(linkText, index));
1592: }
1593:
1594: // END RFE 996031...
1595:
1596: /**
1597: * Assert that a link containing a specified image is present.
1598: *
1599: * @param imageFileName A suffix of the image's filename; for example, to match <tt>"images/my_icon.png"</tt>,
1600: * you could just pass in <tt>"my_icon.png"</tt>.
1601: */
1602: public void assertLinkPresentWithImage(String imageFileName) {
1603: Assert.assertTrue("Link with image file [" + imageFileName
1604: + "] not found in response.", getTestingEngine()
1605: .hasLinkWithImage(imageFileName, 0));
1606: }
1607:
1608: /**
1609: * Assert that a link containing a specified image is not present.
1610: *
1611: * @param imageFileName A suffix of the image's filename; for example, to match <tt>"images/my_icon.png"</tt>,
1612: * you could just pass in <tt>"my_icon.png"</tt>.
1613: */
1614: public void assertLinkNotPresentWithImage(String imageFileName) {
1615: Assert.assertTrue("Link with image file [" + imageFileName
1616: + "] found in response.", !getTestingEngine()
1617: .hasLinkWithImage(imageFileName, 0));
1618: }
1619:
1620: /**
1621: * Assert that an element with a given id is present.
1622: *
1623: * @param anID element id to test for.
1624: */
1625: public void assertElementPresent(String anID) {
1626: Assert.assertTrue("Unable to locate element with id \"" + anID
1627: + "\"", getTestingEngine().hasElement(anID));
1628: }
1629:
1630: /**
1631: * Assert that an element with a given id is not present.
1632: *
1633: * @param anID element id to test for.
1634: */
1635: public void assertElementNotPresent(String anID) {
1636: Assert.assertFalse("Located element with id \"" + anID + "\"",
1637: getTestingEngine().hasElement(anID));
1638: }
1639:
1640: /**
1641: * Assert that an element with a given xpath is present.
1642: *
1643: * @param xpath element xpath to test for.
1644: */
1645: public void assertElementPresentByXPath(String xpath) {
1646: Assert.assertTrue("Unable to locate element with xpath \""
1647: + xpath + "\"", getTestingEngine().hasElementByXPath(
1648: xpath));
1649: }
1650:
1651: /**
1652: * Assert that an element with a given xpath is not present.
1653: *
1654: * @param xpath element xpath to test for.
1655: */
1656: public void assertElementNotPresentByXPath(String xpath) {
1657: Assert.assertFalse("Located element with xpath \"" + xpath
1658: + "\"", getTestingEngine().hasElementByXPath(xpath));
1659: }
1660:
1661: /**
1662: * Assert that a given element contains specific text.
1663: *
1664: * @param elementID id of element to be inspected.
1665: * @param text to check for.
1666: */
1667: public void assertTextInElement(String elementID, String text) {
1668: Assert.assertTrue("Unable to locate element with id \""
1669: + elementID + "\"", getTestingEngine().hasElement(
1670: elementID));
1671: Assert.assertTrue("Unable to locate [" + text
1672: + "] in element \"" + elementID + "\"", getDialog()
1673: .isTextInElement(elementID, text));
1674: }
1675:
1676: public void assertTextNotInElement(String elementID, String text) {
1677: assertElementPresent(elementID);
1678: Assert.assertTrue("Unable to locate element with id \""
1679: + elementID + "\"", getTestingEngine().hasElement(
1680: elementID));
1681: Assert.assertFalse("Text [" + text + "] found in element ["
1682: + elementID + "] when not expected", getTestingEngine()
1683: .isTextInElement(elementID, text));
1684: }
1685:
1686: /**
1687: * Assert that a given element matches a specific regexp.
1688: *
1689: * @param elementID id of element to be inspected.
1690: * @param regexp to match.
1691: */
1692: public void assertMatchInElement(String elementID, String regexp) {
1693: Assert.assertTrue("Unable to locate element with id \""
1694: + elementID + "\"", getTestingEngine().hasElement(
1695: elementID));
1696: Assert.assertTrue("Unable to match [" + regexp
1697: + "] in element \"" + elementID + "\"",
1698: getTestingEngine().isMatchInElement(elementID, regexp));
1699: }
1700:
1701: /**
1702: * Assert that a given element does not match a specific regexp.
1703: *
1704: * @param elementID id of element to be inspected.
1705: * @param regexp to match.
1706: */
1707: public void assertNoMatchInElement(String elementID, String regexp) {
1708: assertElementPresent(elementID);
1709: Assert.assertTrue("Unable to locate element with id \""
1710: + elementID + "\"", getTestingEngine().hasElement(
1711: elementID));
1712: Assert.assertFalse("Regexp [" + regexp
1713: + "] matched in element [" + elementID
1714: + "] when not expected", getDialog().isMatchInElement(
1715: elementID, regexp));
1716: }
1717:
1718: /**
1719: * Assert that a window with the given name is open.
1720: *
1721: * @param windowName
1722: */
1723: public void assertWindowPresent(String windowName) {
1724: Assert.assertTrue("Unable to locate window [" + windowName
1725: + "].", getTestingEngine().hasWindow(windowName));
1726: }
1727:
1728: /**
1729: * Assert that a window with the given ID is open.
1730: *
1731: * @param windowID Javascript window ID.
1732: */
1733: public void assertWindowPresent(int windowID) {
1734: Assert.assertTrue("There is no window with index [" + windowID
1735: + "].", getTestingEngine().getWindowCount() > windowID);
1736: }
1737:
1738: /**
1739: * Assert that at least one window with the given title is open.
1740: *
1741: * @param title
1742: */
1743: public void assertWindowPresentWithTitle(String title) {
1744: Assert.assertTrue("Unable to locate window with title ["
1745: + title + "].", getTestingEngine().hasWindowByTitle(
1746: title));
1747: }
1748:
1749: /**
1750: * Assert that the number of opened windows equals given value.
1751: *
1752: * @param windowCount Window count
1753: */
1754: public void assertWindowCountEquals(int windowCount) {
1755: Assert.assertTrue("Window count is "
1756: + getTestingEngine().getWindowCount() + " but "
1757: + windowCount + " was expected.", getDialog()
1758: .getWindowCount() == windowCount);
1759: }
1760:
1761: /**
1762: * Assert that a frame with the given name or ID is present.
1763: *
1764: * @param frameNameOrId Name or ID of the frame. ID is checked first.
1765: */
1766: public void assertFramePresent(String frameNameOrId) {
1767: Assert.assertTrue("Unable to locate frame with name or ID ["
1768: + frameNameOrId + "].", getTestingEngine().hasFrame(
1769: frameNameOrId));
1770: }
1771:
1772: /**
1773: * Checks to see if a cookie is present in the response.
1774: *
1775: * @param cookieName The cookie name
1776: */
1777: public void assertCookiePresent(String cookieName) {
1778: List cookies = getTestingEngine().getCookies();
1779: for (Iterator i = cookies.iterator(); i.hasNext();) {
1780: if (((Cookie) i.next()).getName().equals(cookieName)) {
1781: return;
1782: }
1783: }
1784: Assert.fail("Could not find Cookie with name [" + cookieName
1785: + "]");
1786: }
1787:
1788: /**
1789: * Check to see if a cookie has the given value.
1790: *
1791: * @param cookieName The cookie name
1792: * @param expectedValue The cookie value
1793: */
1794: public void assertCookieValueEquals(String cookieName,
1795: String expectedValue) {
1796: assertCookiePresent(cookieName);
1797: List cookies = getTestingEngine().getCookies();
1798: for (Iterator i = cookies.iterator(); i.hasNext();) {
1799: Cookie c = (Cookie) i.next();
1800: if (c.getName().equals(cookieName)) {
1801: Assert.assertEquals(expectedValue, c.getValue());
1802: return;
1803: }
1804: }
1805: Assert.fail("Should not be reached");
1806: }
1807:
1808: /**
1809: * Check to see if a cookie value match the given regexp.
1810: *
1811: * @param cookieName The cookie name
1812: * @param regexp The regexp
1813: */
1814: public void assertCookieValueMatch(String cookieName, String regexp) {
1815: assertCookiePresent(cookieName);
1816: RE re = null;
1817: try {
1818: re = new RE(regexp, RE.MATCH_SINGLELINE);
1819: } catch (RESyntaxException e) {
1820: Assert.fail(e.toString());
1821: }
1822: List cookies = getTestingEngine().getCookies();
1823: for (Iterator i = cookies.iterator(); i.hasNext();) {
1824: Cookie c = (Cookie) i.next();
1825: if (c.getName().equals(cookieName)) {
1826: Assert.assertTrue("Unable to match [" + regexp
1827: + "] in cookie \"" + cookieName + "\"", re
1828: .match(c.getValue()));
1829: return;
1830: }
1831: }
1832: Assert.fail("Should not be reached");
1833: }
1834:
1835: // Form interaction methods
1836:
1837: /**
1838: * @deprecated
1839: */
1840: public String getFormElementValue(String formElementName) {
1841: assertFormPresent();
1842: assertFormElementPresent(formElementName);
1843: return getTestingEngine().getElementAttributByXPath(
1844: "//input[@name='" + formElementName + "']", "value");
1845: }
1846:
1847: /**
1848: * Begin interaction with a specified form. If form interaction methods are called without explicitly calling this
1849: * method first, jWebUnit will attempt to determine itself which form is being manipulated.
1850: *
1851: * It is not necessary to call this method if their is only one form on the current page.
1852: *
1853: * @param index 0-based index of the form to work with.
1854: */
1855: public void setWorkingForm(int index) {
1856: getTestingEngine().setWorkingForm(index);
1857: }
1858:
1859: /**
1860: * Begin interaction with a specified form. If form interaction methods are called without explicitly calling this
1861: * method first, jWebUnit will attempt to determine itself which form is being manipulated.
1862: *
1863: * It is not necessary to call this method if their is only one form on the current page.
1864: *
1865: * @param nameOrId name or id of the form to work with.
1866: */
1867: public void setWorkingForm(String nameOrId) {
1868: getTestingEngine().setWorkingForm(nameOrId, 0);
1869: }
1870:
1871: /**
1872: * Begin interaction with a specified form. If form interaction methods are called without explicitly calling this
1873: * method first, jWebUnit will attempt to determine itself which form is being manipulated.
1874: *
1875: * It is not necessary to call this method if their is only one form on the current page.
1876: *
1877: * @param nameOrId name or id of the form to work with.
1878: * @param index The 0-based index, when more than one form with the same name is expected.
1879: */
1880: public void setWorkingForm(String nameOrId, int index) {
1881: getTestingEngine().setWorkingForm(nameOrId, index);
1882: }
1883:
1884: /**
1885: * Set the value of a text or password input field.
1886: *
1887: * @param inputName name of form element.
1888: * @param value value to set.
1889: */
1890: public void setTextField(String inputName, String value) {
1891: assertFormPresent();
1892: assertFormElementPresent(inputName);
1893: getTestingEngine().setTextField(inputName, value);
1894: }
1895:
1896: /**
1897: * Select a specified checkbox. If the checkbox is already checked then the checkbox will stay checked.
1898: *
1899: * @param checkBoxName name of checkbox to be selected.
1900: */
1901: public void checkCheckbox(String checkBoxName) {
1902: assertCheckboxPresent(checkBoxName);
1903: getTestingEngine().checkCheckbox(checkBoxName);
1904: }
1905:
1906: /**
1907: * Select a specified checkbox. If the checkbox is already checked then the checkbox will stay checked.
1908: *
1909: * @param checkBoxName name of checkbox to be selected.
1910: * @param value value of checkbox to be selected.
1911: */
1912: public void checkCheckbox(String checkBoxName, String value) {
1913: assertCheckboxPresent(checkBoxName);
1914: getTestingEngine().checkCheckbox(checkBoxName, value);
1915: }
1916:
1917: /**
1918: * Deselect a specified checkbox. If the checkbox is already unchecked then the checkbox will stay unchecked.
1919: *
1920: * @param checkBoxName name of checkbox to be deselected.
1921: */
1922: public void uncheckCheckbox(String checkBoxName) {
1923: assertFormElementPresent(checkBoxName);
1924: getTestingEngine().uncheckCheckbox(checkBoxName);
1925: }
1926:
1927: /**
1928: * Deselect a specified checkbox. If the checkbox is already unchecked then the checkbox will stay unchecked.
1929: *
1930: * @param checkBoxName name of checkbox to be deselected.
1931: * @param value value of checkbox to be deselected.
1932: */
1933: public void uncheckCheckbox(String checkBoxName, String value) {
1934: assertFormElementPresent(checkBoxName);
1935: getTestingEngine().uncheckCheckbox(checkBoxName, value);
1936: }
1937:
1938: /**
1939: * Select options with given display labels in a select element.
1940: *
1941: * @param selectName name of select element.
1942: * @param labels labels of options to be selected.
1943: */
1944: public void selectOptions(String selectName, String[] labels) {
1945: assertSelectOptionsPresent(selectName, labels);
1946: selectOptionsByLabel(selectName, labels);
1947: }
1948:
1949: /**
1950: * Select an option with a given display label in a select element.
1951: *
1952: * @param selectName name of select element.
1953: * @param label label of option to be selected.
1954: */
1955: public void selectOption(String selectName, String label) {
1956: selectOptions(selectName, new String[] { label });
1957: }
1958:
1959: /**
1960: * Select options with given values in a select element.
1961: *
1962: * @param selectName name of select element.
1963: * @param values values of options to be selected.
1964: */
1965: public void selectOptionsByValues(String selectName, String[] values) {
1966: assertSelectOptionValuesPresent(selectName, values);
1967: getTestingEngine().selectOptions(selectName, values);
1968: }
1969:
1970: /**
1971: * Select an option with a given value in a select element.
1972: *
1973: * @param selectName name of select element.
1974: * @param values values of options to be selected.
1975: */
1976: public void selectOptionByValue(String selectName, String value) {
1977: selectOptionsByValues(selectName, new String[] { value });
1978: }
1979:
1980: // Form submission and link navigation methods
1981:
1982: /**
1983: * Submit form - default submit button will be used (unnamed submit button, or named button if there is only one on
1984: * the form.
1985: */
1986: public void submit() {
1987: assertSubmitButtonPresent();
1988: getTestingEngine().submit();
1989: }
1990:
1991: /**
1992: * Submit form by pressing named button.
1993: *
1994: * @param buttonName Submit button name attribut value.
1995: */
1996: public void submit(String buttonName) {
1997: assertSubmitButtonPresent(buttonName);
1998: getTestingEngine().submit(buttonName);
1999: }
2000:
2001: /**
2002: * Submit the form by pressing the named button with the given value (label). Useful if you have more than one
2003: * submit button with same name.
2004: *
2005: * @param buttonName Submit button name attribut value.
2006: * @param buttonValue Submit button value attribut value.
2007: */
2008: public void submit(String buttonName, String buttonValue) {
2009: assertSubmitButtonPresent(buttonName, buttonValue);
2010: getTestingEngine().submit(buttonName, buttonValue);
2011: }
2012:
2013: /**
2014: * Reset the current form using the default reset button. See {@link #getForm}for an explanation of how the current
2015: * form is established.
2016: */
2017: public void reset() {
2018: assertResetButtonPresent();
2019: getTestingEngine().reset();
2020: }
2021:
2022: /**
2023: * Navigate by selection of a link containing given text.
2024: *
2025: * @param linkText Text in the link.
2026: */
2027: public void clickLinkWithText(String linkText) {
2028: assertLinkPresentWithText(linkText);
2029: getTestingEngine().clickLinkWithText(linkText, 0);
2030: }
2031:
2032: /**
2033: * Navigate by selecting Nth link containing given text.
2034: *
2035: * @param linkText Text in the link.
2036: * @param index The 0-based index, when more than one link with the same text is expected.
2037: */
2038: public void clickLinkWithText(String linkText, int index) {
2039: assertLinkPresentWithText(linkText, index);
2040: getTestingEngine().clickLinkWithText(linkText, index);
2041: }
2042:
2043: /**
2044: * Navigate by selection of a link with the exact given text.
2045: *
2046: * @param linkText Text of the link.
2047: */
2048: public void clickLinkWithExactText(String linkText) {
2049: assertLinkPresentWithExactText(linkText);
2050: getTestingEngine().clickLinkWithExactText(linkText, 0);
2051: }
2052:
2053: /**
2054: * Navigate by selecting Nth link with the exact given text.
2055: *
2056: * @param linkText Text of the link.
2057: * @param index The 0-based index, when more than one link with the same text is expected.
2058: */
2059: public void clickLinkWithExactText(String linkText, int index) {
2060: assertLinkPresentWithExactText(linkText, index);
2061: getTestingEngine().clickLinkWithExactText(linkText, index);
2062: }
2063:
2064: /**
2065: * Click the button with the given id.
2066: *
2067: * @param buttonId Button ID attribut value.
2068: */
2069: public void clickButton(String buttonId) {
2070: assertButtonPresent(buttonId);
2071: getTestingEngine().clickButton(buttonId);
2072: }
2073:
2074: /**
2075: * Clicks a button with <code>text</code> of the value attribute.
2076: *
2077: * @param buttonValueText The text of the button (contents of the value attribute).
2078: */
2079: public void clickButtonWithText(String buttonValueText) {
2080: assertButtonPresentWithText(buttonValueText);
2081: getTestingEngine().clickButtonWithText(buttonValueText);
2082: }
2083:
2084: /**
2085: * Navigate by selection of a link with a given image.
2086: *
2087: * @param imageFileName A suffix of the image's filename; for example, to match <tt>"images/my_icon.png"</tt>,
2088: * you could just pass in <tt>"my_icon.png"</tt>.
2089: */
2090: public void clickLinkWithImage(String imageFileName) {
2091: assertLinkPresentWithImage(imageFileName);
2092: getTestingEngine().clickLinkWithImage(imageFileName, 0);
2093: }
2094:
2095: /**
2096: * Navigate by selection of a link with given id.
2097: *
2098: * @param linkId id of link
2099: */
2100: public void clickLink(String linkId) {
2101: assertLinkPresent(linkId);
2102: getTestingEngine().clickLink(linkId);
2103: }
2104:
2105: /**
2106: * Clicks a radio option. Asserts that the radio option exists first. *
2107: *
2108: * @param radioGroup name of the radio group.
2109: * @param radioOption value of the option to check for.
2110: */
2111: public void clickRadioOption(String radioGroup, String radioOption) {
2112: assertRadioOptionPresent(radioGroup, radioOption);
2113: getTestingEngine().clickRadioOption(radioGroup, radioOption);
2114: }
2115:
2116: /**
2117: * Click element with given xpath.
2118: *
2119: * @param xpath xpath of the element.
2120: */
2121: public void clickElementByXPath(String xpath) {
2122: assertElementPresentByXPath(xpath);
2123: getTestingEngine().clickElementByXPath(xpath);
2124: }
2125:
2126: /**
2127: * Get the attribut value of the gicen element. For example, if you have img src="bla.gif" alt="toto",
2128: * getElementAttributByXPath("//img[@src='bla.gif']", "alt") returns "toto"
2129: *
2130: * @param xpath XPath of the element.
2131: * @param attribut Name of the attribut.
2132: * @return The value of the attribut.
2133: */
2134: public String getElementAttributByXPath(String xpath,
2135: String attribut) {
2136: assertElementPresentByXPath(xpath);
2137: return getTestingEngine().getElementAttributByXPath(xpath,
2138: attribut);
2139: }
2140:
2141: /**
2142: * Get text of the given element.
2143: *
2144: * @param xpath xpath of the element.
2145: */
2146: public String getElementTextByXPath(String xpath) {
2147: assertElementPresentByXPath(xpath);
2148: return getTestingEngine().getElementTextByXPath(xpath);
2149: }
2150:
2151: // Window and Frame Navigation Methods
2152:
2153: /**
2154: * Make a given window active.
2155: *
2156: * @param windowName Name of the window.
2157: */
2158: public void gotoWindow(String windowName) {
2159: assertWindowPresent(windowName);
2160: getTestingEngine().gotoWindow(windowName);
2161: }
2162:
2163: /**
2164: * Make a given window active.
2165: *
2166: * @param windowID Javascript ID of the window
2167: */
2168: public void gotoWindow(int windowID) {
2169: assertWindowPresent(windowID);
2170: getTestingEngine().gotoWindow(windowID);
2171: }
2172:
2173: /**
2174: * Make the root window active.
2175: */
2176: public void gotoRootWindow() {
2177: getTestingEngine().gotoRootWindow();
2178: }
2179:
2180: /**
2181: * Make first window with the given title active.
2182: *
2183: * @param title Title of the window.
2184: */
2185: public void gotoWindowByTitle(String title) {
2186: assertWindowPresentWithTitle(title);
2187: getTestingEngine().gotoWindowByTitle(title);
2188: }
2189:
2190: /**
2191: * Make the given frame active.
2192: *
2193: * @param frameNameOrId Name or ID of the frame. ID is checked first.
2194: */
2195: public void gotoFrame(String frameNameOrId) {
2196: getTestingEngine().gotoFrame(frameNameOrId);
2197: }
2198:
2199: /**
2200: * Go to the given page like if user has typed the URL manually in the browser. Use
2201: * {@link TestContext#setBaseUrl(String) getTestContext().setBaseUrl(String)} to define base URL. Absolute URL
2202: * should start with "http://", "https://" or "www.".
2203: *
2204: * @param url absolute or relative URL (relative to base URL).
2205: * @throws TestingEngineResponseException If something bad happend (404)
2206: */
2207: public void gotoPage(String url)
2208: throws TestingEngineResponseException {
2209: try {
2210: getTestingEngine().gotoPage(
2211: createUrl(url, getTestContext().getBaseUrl()));
2212: } catch (MalformedURLException e) {
2213: Assert.fail(e.getLocalizedMessage());
2214: }
2215: }
2216:
2217: /**
2218: * Print all the cookies to stdout.
2219: *
2220: */
2221: public void dumpCookies() {
2222: List cookies = getTestingEngine().getCookies();
2223: for (Iterator i = cookies.iterator(); i.hasNext();) {
2224: Cookie c = (Cookie) i.next();
2225: System.out.println("Name=" + c.getName() + "; Value="
2226: + c.getValue() + "; Domain=" + c.getDomain()
2227: + "; Comment=" + c.getComment() + "; MaxAge="
2228: + c.getMaxAge() + "; Path=" + c.getPath()
2229: + "; Version=" + c.getVersion());
2230: }
2231: }
2232:
2233: /**
2234: * Get the source of the HTML page (like in a real browser), or HTTP body for a non HTML content.
2235: *
2236: * @return The HTML content.
2237: */
2238: public String getPageSource() {
2239: return getTestingEngine().getPageSource();
2240: }
2241:
2242: /**
2243: * Get the last data sent by the server.
2244: *
2245: * @return HTTP server response.
2246: */
2247: public String getServeurResponse() {
2248: return getTestingEngine().getServerResponse();
2249: }
2250:
2251: /**
2252: * Save the last downloaded page (or file) to the disk.
2253: *
2254: * @param f The file name.
2255: */
2256: public void saveAs(File f) {
2257: InputStream in = getTestingEngine().getInputStream();
2258: int c = 0;
2259: try {
2260: f.createNewFile();
2261: FileOutputStream out = new FileOutputStream(f);
2262: while ((c = in.read()) != -1)
2263: out.write(c);
2264: in.close();
2265: out.close();
2266: } catch (IOException e) {
2267: throw new RuntimeException("Error when writing to file", e);
2268: }
2269:
2270: }
2271:
2272: /**
2273: * Download the current page (or file) and compare it with the given file.
2274: *
2275: * @param expected Expected file URL.
2276: */
2277: public void assertDownloadedFileEquals(URL expected) {
2278: try {
2279: File tmp = File.createTempFile("jwebunit", null);
2280: tmp.deleteOnExit();
2281: saveAs(tmp);
2282: Assert.assertTrue("Files are not binary equals.",
2283: areFilesEqual(expected, tmp.toURL()));
2284: } catch (IOException e) {
2285: e.printStackTrace();
2286: Assert.fail(e.toString());
2287: }
2288: }
2289:
2290: // Debug methods
2291:
2292: /**
2293: * Dump html of current response to System.out - for debugging purposes.
2294: *
2295: * @param stream
2296: * @deprecated Use {@link WebTester#getPageSource()}
2297: */
2298: public void dumpHtml() {
2299: dumpHtml(System.out);
2300: }
2301:
2302: /**
2303: * Dump html of current response to a specified stream - for debugging purposes.
2304: *
2305: * @param stream
2306: * @deprecated Use {@link WebTester#getPageSource()}
2307: */
2308: public void dumpHtml(PrintStream stream) {
2309: stream.println(getTestingEngine().getPageSource());
2310: }
2311:
2312: /**
2313: * Dump the table as the 2D array that is used for assertions - for debugging purposes.
2314: *
2315: * @param tableNameOrId
2316: * @param stream
2317: */
2318: public void dumpTable(String tableNameOrId) {
2319: dumpTable(tableNameOrId, System.out);
2320: }
2321:
2322: /**
2323: * Dump the table as the 2D array that is used for assertions - for debugging purposes.
2324: *
2325: * @param tableNameOrId
2326: * @param table
2327: * @param stream
2328: */
2329: public void dumpTable(String tableNameOrId, PrintStream stream) {
2330: // String[][] table = getDialogInternal().getTable(tableNameOrId).getStrings();
2331: // //TODO Print correctly cells with colspan
2332: // stream.print("\n" + tableNameOrId + ":");
2333: // for (int i = 0; i < table.length; i++) {
2334: // String[] cell = table[i];
2335: // stream.print("\n\t");
2336: // for (int j = 0; j < cell.length; j++) {
2337: // stream.print("[" + cell[j] + "]");
2338: // }
2339: // }
2340:
2341: }
2342:
2343: // Settings
2344:
2345: /**
2346: * Enable or disable Javascript support
2347: */
2348: public void setScriptingEnabled(boolean value) {
2349: getTestingEngine().setScriptingEnabled(value);
2350: }
2351:
2352: /**
2353: * Set the Testing Engine that you want to use for the tests based on the Testing Engine Key.
2354: *
2355: * @see TestingEngineRegistry
2356: * @param testingEngineKey The testingEngineKey to set.
2357: */
2358: public void setTestingEngineKey(String testingEngineKey) {
2359: this .testingEngineKey = testingEngineKey;
2360: dialog = null;
2361: }
2362:
2363: /**
2364: * Gets the Testing Engine Key that is used to find the proper testing engine class (HtmlUnitDialog /
2365: * SeleniumDialog) for the tests.
2366: *
2367: * @return Returns the testingEngineKey.
2368: */
2369: public String getTestingEngineKey() {
2370: if (testingEngineKey == null) {
2371: // use first available dialog
2372: String key = TestingEngineRegistry.getFirstAvailable();
2373: if (key != null) {
2374: setTestingEngineKey(key);
2375: } else {
2376: throw new RuntimeException(
2377: "TestingEngineRegistry contains no dialog. Check you put at least one plugin in the classpath.");
2378: }
2379: }
2380: return testingEngineKey;
2381: }
2382:
2383: private RE getRE(String regexp) {
2384: RE re = null;
2385: try {
2386: re = new RE(regexp, RE.MATCH_SINGLELINE);
2387: } catch (RESyntaxException e) {
2388: Assert.fail(e.toString());
2389: }
2390: return re;
2391: }
2392:
2393: /**
2394: * Return a string array of select box option labels. <br/>
2395: *
2396: * Exemple: <br/>
2397: *
2398: * <pre>
2399: * <FORM action="http://my_host/doit" method="post">
2400: * <P>
2401: * <SELECT multiple size="4" name="component-select">
2402: * <OPTION selected value="Component_1_a">Component_1</OPTION>
2403: * <OPTION selected value="Component_1_b">Component_2</OPTION>
2404: * <OPTION>Component_3</OPTION>
2405: * <OPTION>Component_4</OPTION>
2406: * <OPTION>Component_5</OPTION>
2407: * </SELECT>
2408: * <INPUT type="submit" value="Send"><INPUT type="reset">
2409: * </P>
2410: * </FORM>
2411: * </pre>
2412: *
2413: * Should return [Component_1, Component_2, Component_3, Component_4, Component_5]
2414: *
2415: * @param selectName name of the select box.
2416: * @return Array of options labels.
2417: */
2418: private String[] getOptionsFor(String selectName) {
2419: String[] values = getTestingEngine().getSelectOptionValues(
2420: selectName);
2421: String[] result = new String[values.length];
2422: for (int i = 0; i < values.length; i++) {
2423: result[i] = getTestingEngine()
2424: .getSelectOptionLabelForValue(selectName, values[i]);
2425: }
2426: return result;
2427: }
2428:
2429: /**
2430: * Select options by given labels in a select box.
2431: *
2432: * @param selectName name of the select
2433: * @param labels labels of options to be selected
2434: */
2435: private void selectOptionsByLabel(String selectName, String[] labels) {
2436: String[] values = new String[labels.length];
2437: for (int i = 0; i < values.length; i++) {
2438: values[i] = getTestingEngine()
2439: .getSelectOptionValueForLabel(selectName, labels[i]);
2440: }
2441: getTestingEngine().selectOptions(selectName, values);
2442: }
2443:
2444: private void assertArraysEqual(String[] exptected, String[] returned) {
2445: Assert.assertEquals("Arrays not same length", exptected.length,
2446: returned.length);
2447: for (int i = 0; i < returned.length; i++) {
2448: Assert.assertEquals("Elements " + i + "not equal",
2449: exptected[i], returned[i]);
2450: }
2451: }
2452:
2453: /**
2454: * Set the value of a form input element.
2455: *
2456: * @param formElementName name of form element.
2457: * @param value
2458: * @deprecated use setTextField or other methods
2459: */
2460: public void setFormElement(String formElementName, String value) {
2461: assertFormPresent();
2462: assertFormElementPresent(formElementName);
2463: getTestingEngine().setTextField(formElementName, value);
2464: }
2465:
2466: /**
2467: * Tell that the given alert boxe is expected.
2468: *
2469: * @param message Message in the alert.
2470: */
2471: public void setExpectedJavaScriptAlert(String message) {
2472: try {
2473: getTestingEngine()
2474: .setExpectedJavaScriptAlert(
2475: new JavascriptAlert[] { new JavascriptAlert(
2476: message) });
2477: } catch (ExpectedJavascriptAlertException e) {
2478: Assert.fail("You previously tell that alert with message ["
2479: + e.getAlertMessage()
2480: + "] was expected, but nothing appeared.");
2481: }
2482: }
2483:
2484: /**
2485: * Tell that the given alert boxes are expected in the given order.
2486: *
2487: * @param messages Messages in the alerts.
2488: */
2489: public void setExpectedJavaScriptAlert(String[] messages) {
2490: JavascriptAlert[] alerts = new JavascriptAlert[messages.length];
2491: for (int i = 0; i < messages.length; i++) {
2492: alerts[i] = new JavascriptAlert(messages[i]);
2493: }
2494: try {
2495: getTestingEngine().setExpectedJavaScriptAlert(alerts);
2496: } catch (ExpectedJavascriptAlertException e) {
2497: Assert.fail("You previously tell that alert with message ["
2498: + e.getAlertMessage()
2499: + "] was expected, but nothing appeared.");
2500: }
2501: }
2502:
2503: /**
2504: * Tell that the given confirm boxe is expected.
2505: *
2506: * @param message Message in the confirm.
2507: * @param action Whether we should click on "OK" (true) or "Cancel" (false)
2508: */
2509: public void setExpectedJavaScriptConfirm(String message,
2510: boolean action) {
2511: try {
2512: getTestingEngine().setExpectedJavaScriptConfirm(
2513: new JavascriptConfirm[] { new JavascriptConfirm(
2514: message, action) });
2515: } catch (ExpectedJavascriptConfirmException e) {
2516: Assert
2517: .fail("You previously tell that confirm with message ["
2518: + e.getConfirmMessage()
2519: + "] was expected, but nothing appeared.");
2520: }
2521: }
2522:
2523: /**
2524: * Tell that the given confirm boxes are expected in the given order.
2525: *
2526: * @param messages Messages in the confirms.
2527: * @param actions Whether we should click on "OK" (true) or "Cancel" (false)
2528: */
2529: public void setExpectedJavaScriptConfirm(String[] messages,
2530: boolean[] actions) {
2531: Assert
2532: .assertEquals(
2533: "You should give the same number of messages and actions",
2534: messages.length, actions.length);
2535: JavascriptConfirm[] confirms = new JavascriptConfirm[messages.length];
2536: for (int i = 0; i < messages.length; i++) {
2537: confirms[i] = new JavascriptConfirm(messages[i], actions[i]);
2538: }
2539: try {
2540: getTestingEngine().setExpectedJavaScriptConfirm(confirms);
2541: } catch (ExpectedJavascriptConfirmException e) {
2542: Assert
2543: .fail("You previously tell that confirm with message ["
2544: + e.getConfirmMessage()
2545: + "] was expected, but nothing appeared.");
2546: }
2547: }
2548:
2549: /**
2550: * Tell that the given prompt boxe is expected.
2551: *
2552: * @param message Message in the prompt.
2553: * @param input What we should put in the prompt (null if user press Cancel)
2554: */
2555: public void setExpectedJavaScriptPrompt(String message, String input) {
2556: try {
2557: getTestingEngine().setExpectedJavaScriptPrompt(
2558: new JavascriptPrompt[] { new JavascriptPrompt(
2559: message, input) });
2560: } catch (ExpectedJavascriptPromptException e) {
2561: Assert
2562: .fail("You previously tell that prompt with message ["
2563: + e.getPromptMessage()
2564: + "] was expected, but nothing appeared.");
2565: }
2566: }
2567:
2568: /**
2569: * Tell that the given prompt boxes are expected in the given order.
2570: *
2571: * @param messages Messages in the prompts.
2572: * @param inputs What we should put in the prompt (null if user press Cancel)
2573: */
2574: public void setExpectedJavaScriptPrompt(String[] messages,
2575: String[] inputs) {
2576: Assert
2577: .assertEquals(
2578: "You should give the same number of messages and inputs",
2579: messages.length, inputs.length);
2580: JavascriptPrompt[] prompts = new JavascriptPrompt[messages.length];
2581: for (int i = 0; i < messages.length; i++) {
2582: prompts[i] = new JavascriptPrompt(messages[i], inputs[i]);
2583: }
2584: try {
2585: getTestingEngine().setExpectedJavaScriptPrompt(prompts);
2586: } catch (ExpectedJavascriptPromptException e) {
2587: Assert
2588: .fail("You previously tell that prompt with message ["
2589: + e.getPromptMessage()
2590: + "] was expected, but nothing appeared.");
2591: }
2592: }
2593:
2594: public void assertImagePresent(String imageSrc, String imageAlt) {
2595: String xpath = "//img[@src=\"" + imageSrc + "\"";
2596: if (imageAlt != null) {
2597: xpath += " and @alt=\"" + imageAlt + "\"";
2598: }
2599: xpath += "]";
2600: assertElementPresentByXPath(xpath);
2601: }
2602:
2603: /**
2604: * @see #assertImageValidAndStore(String, String, java.io.File)
2605: */
2606: public void assertImageValid(String imageSrc, String imageAlt) {
2607: validateImage(imageSrc, imageAlt, null);
2608: }
2609:
2610: /**
2611: * Asserts that the image with the given src and alt attribute values exist in the page and is an actual reachable
2612: * image, then saves it as png with the given file name.
2613: *
2614: * @param imageSrc as it appears in the html page, i.e. relative to the current page.
2615: */
2616: public void assertImageValidAndStore(String imageSrc,
2617: String imageAlt, File out) {
2618: validateImage(imageSrc, imageAlt, out);
2619: }
2620:
2621: /**
2622: * @see #assertImageValidAndStore(String, String, java.io.File)
2623: */
2624: public Image getImage(String imageSrc, String imageAlt) {
2625: return validateImage(imageSrc, imageAlt, null);
2626: }
2627:
2628: private Image validateImage(String imageSrc, String imageAlt,
2629: File out) {
2630: assertImagePresent(imageSrc, imageAlt);
2631: URL imageUrl = null;
2632: try {
2633: imageUrl = createUrl(imageSrc, getTestingEngine()
2634: .getPageURL());
2635: } catch (MalformedURLException e1) {
2636: Assert.fail(e1.getLocalizedMessage());
2637: }
2638: try {
2639: final InputStream imgStream = getTestingEngine()
2640: .getInputStream(imageUrl);
2641: final BufferedImage img = ImageIO.read(imgStream);
2642: if (img == null) {
2643: Assert.fail("Could not load image from " + imageUrl);
2644: }
2645: if (out != null) {
2646: ImageIO.write(img, "png", out);
2647: }
2648: return img;
2649: } catch (IOException e) {
2650: Assert
2651: .fail("Could not load or save image from "
2652: + imageUrl);
2653: } catch (TestingEngineResponseException e) {
2654: Assert.fail("The server returns the code "
2655: + e.getHttpStatusCode());
2656: }
2657: throw new IllegalStateException();
2658: }
2659:
2660: protected boolean areFilesEqual(URL f1, URL f2) throws IOException {
2661: // read and compare bytes pair-wise
2662: InputStream i1 = f1.openStream();
2663: InputStream i2 = f2.openStream();
2664: if (f1 == null) {
2665: throw new IOException(f1.toString() + " can't be opened.");
2666: }
2667: if (f2 == null) {
2668: throw new IOException(f2.toString() + " can't be opened.");
2669: }
2670: int b1, b2;
2671: do {
2672: b1 = i1.read();
2673: b2 = i2.read();
2674: } while (b1 == b2 && b1 != -1 && b2 != -1);
2675: i1.close();
2676: i2.close();
2677: // true only if end of file is reached for both
2678: return (b1 == -1) && (b2 == -1);
2679: }
2680:
2681: }
|