PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jira-project/jira-functional-tests/jira-func-tests/src/main/java/com/atlassian/jira/webtests/WebTestCaseWrapper.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 237 lines | 139 code | 37 blank | 61 comment | 10 complexity | 9a5c9535ae515140a98e72b42eaebe72 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.webtests;
  2. import com.atlassian.jira.functest.framework.assertions.Assertions;
  3. import com.atlassian.jira.functest.framework.locator.Locator;
  4. import com.atlassian.jira.functest.framework.locator.XPathLocator;
  5. import com.atlassian.jira.testkit.client.log.FuncTestOut;
  6. import com.atlassian.jira.util.ProgressPageControl;
  7. import com.meterware.httpunit.HttpUnitOptions;
  8. import net.sourceforge.jwebunit.WebTestCase;
  9. import net.sourceforge.jwebunit.WebTester;
  10. import org.apache.commons.lang.StringUtils;
  11. import java.io.PrintWriter;
  12. import java.io.StringWriter;
  13. /**
  14. * This class pretty much wraps the WebTestCase to dump out the response whenever the WebTestFails. A definite candidate
  15. * for some simple AOP work :)
  16. */
  17. public class WebTestCaseWrapper extends WebTestCase {
  18. //---------------------------------------------------------------------------------------------------- static helper
  19. public static void raiseRuntimeException(Throwable e) {
  20. if (e instanceof Error) {
  21. throw (Error) e;
  22. }
  23. if (e instanceof RuntimeException) {
  24. throw (RuntimeException) e;
  25. }
  26. throw new RuntimeException(e);
  27. }
  28. //------------------------------------------------------------------------------------------------------------ ctors
  29. public WebTestCaseWrapper(String name) {
  30. super(name);
  31. }
  32. public WebTestCaseWrapper() {
  33. }
  34. // ------------------------------------------------------------------------------------------ Wrapped Assert Methods
  35. /**
  36. * Clicks the standard JIRA form's cancel button. Note: this requires
  37. * {@link HttpUnitOptions#setScriptingEnabled(boolean)} to be set true BEFORE
  38. * the containing page is loaded. If you do this, make sure you set back in
  39. * a try/finally block
  40. */
  41. public void clickCancelButton() {
  42. assertTrue("Scripting must be enabled in the HttpUnit for cancel to work correctly", HttpUnitOptions.isScriptingEnabled());
  43. clickLink("cancelButton");
  44. }
  45. public void assertTextPresent(String text) {
  46. log("Asserting text present: " + text);
  47. super.assertTextPresent(text);
  48. }
  49. public void assertTextNotPresent(String text) {
  50. log("Asserting text *not* present: " + text);
  51. super.assertTextNotPresent(text);
  52. }
  53. public void assertTextInTable(String tableSummaryOrId, String text) {
  54. log("Asserting text present in table (" + tableSummaryOrId + ") : " + text);
  55. super.assertTextInTable(tableSummaryOrId, text);
  56. }
  57. /**
  58. * @deprecated Use {@link Assertions#assertNodeByIdExists(java.lang.String)} instead. Since v7.1.
  59. */
  60. @Deprecated
  61. public void assertElementPresent(String anID) {
  62. Locator xPathLocator = new XPathLocator(tester, "//*[@id='" + anID + "']");
  63. assertTrue("Could not find any elements with the id '" + anID + "'", xPathLocator.getNodes().length > 0);
  64. }
  65. /**
  66. * @deprecated Use {@link Assertions#assertNodeByIdDoesNotExist(java.lang.String)} instead. Since v7.1.
  67. */
  68. @Deprecated
  69. public void assertElementNotPresent(String anID) {
  70. Locator xPathLocator = new XPathLocator(tester, "//*[@id='" + anID + "']");
  71. assertTrue("Found an element with the id '" + anID + "' - was expecting none", xPathLocator.getNodes().length == 0);
  72. }
  73. public void assertElementPresentBy(String attrName, String attrValue) {
  74. Locator xPathLocator = new XPathLocator(tester, "//*[@" + attrName + "='" + attrValue + "']");
  75. assertTrue("Could not find any elements with " + attrName + " = '" + attrValue + "'", xPathLocator.getNodes().length > 0);
  76. }
  77. public void assertElementNotPresentBy(String attrName, String attrValue) {
  78. Locator xPathLocator = new XPathLocator(tester, "//*[@" + attrName + "='" + attrValue + "']");
  79. assertTrue("Could not find any elements with " + attrName + " = '" + attrValue + "'", xPathLocator.getNodes().length == 0);
  80. }
  81. // ----------------------------------------------------------------------------------------------- RuntimeExceptions
  82. /**
  83. * @deprecated use {@link com.atlassian.jira.functest.framework.Form#selectOptionsByDisplayName(String, String[])} instead.
  84. */
  85. @Deprecated
  86. public void selectMultiOption(String selectName, String option) {
  87. // A bit of a hack. The only way to really select multiple options at the moment is to treat it like a checkbox
  88. String value = getDialog().getValueForOption(selectName, option);
  89. checkCheckbox(selectName, value);
  90. }
  91. /**
  92. * Selects an option from a multiple select field by specifying the value of the desired option.
  93. *
  94. * @param selectName the name of the select form element.
  95. * @param value the value of the option to be selected.
  96. */
  97. public void selectMultiOptionByValue(String selectName, String value) {
  98. checkCheckbox(selectName, value);
  99. }
  100. /**
  101. * Selects an option from a select form field by specifying the visible option label (as opposed to the value)
  102. *
  103. * @param selectName the name of the select form element.
  104. * @param option the string label for the option to be selected.
  105. */
  106. public void selectOption(String selectName, String option) {
  107. super.selectOption(selectName, option);
  108. }
  109. /**
  110. * Asserts that an option with the given option label is present on the
  111. * given select form element.
  112. *
  113. * @param selectName the name of the select form element.
  114. * @param optionLabel the value of the option to assert on.
  115. */
  116. public void assertOptionValuePresent(String selectName, String optionLabel) {
  117. if (!getDialog().hasRadioOption(selectName, optionLabel)) {
  118. fail("Option with value " + optionLabel + " not found for select name " + selectName);
  119. }
  120. }
  121. /**
  122. * Asserts that an option with the given option label is not present on the
  123. * given select form element.
  124. *
  125. * @param selectName the name of the select form element.
  126. * @param option the value of the option to assert on.
  127. * @deprecated Use {@link net.sourceforge.jwebunit.WebTester#assertRadioOptionValueNotPresent(String, String)}
  128. */
  129. public void assertOptionValueNotPresent(String selectName, String option) {
  130. if (getDialog().hasRadioOption(selectName, option)) {
  131. fail("Option with value " + option + " found for select name " + selectName);
  132. }
  133. }
  134. /**
  135. * Submits the current form using the button with the given name.
  136. *
  137. * @param buttonName The name attribute on the submit input element.
  138. * @throws RuntimeException if the form or button is not present.
  139. */
  140. public void submit(String buttonName) {
  141. try {
  142. super.submit(buttonName);
  143. } catch (Throwable e) {
  144. raiseRuntimeException(e);
  145. }
  146. }
  147. /**
  148. * @deprecated use {{@link com.atlassian.jira.functest.framework.BulkOperations#waitAndReloadBulkOperationProgressPage()}
  149. */
  150. @Deprecated
  151. public void waitAndReloadBulkOperationProgressPage() {
  152. waitAndReloadBulkOperationProgressPage(false);
  153. }
  154. public void waitAndReloadBulkOperationProgressPage(boolean optional) {
  155. waitAndReloadBulkOperationProgressPage(optional, tester);
  156. }
  157. public void waitAndReloadBulkOperationProgressPage(boolean optional, WebTester webTester) {
  158. if (optional && !webTester.getDialog().getResponsePageTitle().contains("Bulk Operation"))
  159. return;
  160. assertTextPresent("Bulk Operation Progress");
  161. ProgressPageControl.waitAndReload(webTester, "bulkoperationprogressform", "Refresh", "Acknowledge");
  162. log("waitAndReloadBulkOperationProgressPage");
  163. }
  164. // -------------------------------------------------------------------------------------------------- Helper Methods
  165. public static void log(String msg) {
  166. FuncTestOut.log(msg);
  167. }
  168. public static void logSection(String msg) {
  169. log("");
  170. log(StringUtils.repeat("-", msg.length()));
  171. log(msg);
  172. }
  173. public static void log(Throwable t) {
  174. StringWriter sw = new StringWriter();
  175. PrintWriter pw = new PrintWriter(sw);
  176. t.printStackTrace(pw);
  177. log(sw.toString());
  178. }
  179. public static void log(String msg, Throwable t) {
  180. log(msg);
  181. log(t);
  182. }
  183. public void dumpResponse() {
  184. super.dumpResponse(FuncTestOut.out);
  185. }
  186. public void dumpResponse(Throwable t) {
  187. if (t != null) {
  188. t.printStackTrace(FuncTestOut.out);
  189. }
  190. super.dumpResponse(FuncTestOut.out);
  191. }
  192. public void tearDown() {
  193. tester = null;
  194. try {
  195. super.tearDown();
  196. } catch (Throwable t) {
  197. raiseRuntimeException(t);
  198. }
  199. }
  200. }