/test/src/main/java/com/gargoylesoftware/htmlunit/WebClientUtil.java

https://github.com/jpederzolli/jenkins-1 · Java · 139 lines · 50 code · 14 blank · 75 comment · 0 complexity · c873f2d31645af0b13dca234091d261a MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2015, CloudBees, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package com.gargoylesoftware.htmlunit;
  25. import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
  26. import org.junit.Assert;
  27. import java.net.MalformedURLException;
  28. import java.net.URL;
  29. /**
  30. * {@link WebClient} helper methods.
  31. * @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
  32. */
  33. public class WebClientUtil {
  34. /**
  35. * Wait for all async JavaScript tasks associated with the supplied {@link WebClient} instance
  36. * to complete.
  37. * <p>
  38. * Waits for 10 seconds before timing out.
  39. * @param webClient The {@link WebClient} instance.
  40. */
  41. public static void waitForJSExec(WebClient webClient) {
  42. waitForJSExec(webClient, 10000);
  43. }
  44. /**
  45. * Wait for all async JavaScript tasks associated with the supplied {@link WebClient} instance
  46. * to complete.
  47. * @param webClient The {@link WebClient} instance.
  48. * @param timeout The timeout in milliseconds.
  49. */
  50. public static void waitForJSExec(WebClient webClient, long timeout) {
  51. webClient.getJavaScriptEngine().processPostponedActions();
  52. webClient.waitForBackgroundJavaScript(timeout);
  53. }
  54. /**
  55. * Create and add an {@link ExceptionListener} to the {@link WebClient} instance.
  56. * @param webClient The {@link WebClient} instance.
  57. * @return The {@link ExceptionListener}.
  58. */
  59. public static ExceptionListener addExceptionListener(WebClient webClient) {
  60. ExceptionListener exceptionListener = new ExceptionListener(webClient);
  61. webClient.setJavaScriptErrorListener(exceptionListener);
  62. return exceptionListener;
  63. }
  64. /**
  65. * JavaScript Exception listener.
  66. * @see #addExceptionListener(WebClient)
  67. */
  68. public static class ExceptionListener implements JavaScriptErrorListener {
  69. private final WebClient webClient;
  70. private ScriptException scriptException;
  71. private ExceptionListener(WebClient webClient) {
  72. this.webClient = webClient;
  73. }
  74. /**
  75. * Get the last {@link ScriptException}.
  76. * @return The last {@link ScriptException}, or {@code null} if none happened.
  77. */
  78. public ScriptException getScriptException() {
  79. return scriptException;
  80. }
  81. /**
  82. * Get the last {@link ScriptException}.
  83. * <p>
  84. * Performs a call to {@link #assertHasException()}.
  85. * @return The last {@link ScriptException}.
  86. */
  87. public ScriptException getExpectedScriptException() {
  88. assertHasException();
  89. return scriptException;
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. @Override
  95. public void scriptException(InteractivePage htmlPage, ScriptException scriptException) {
  96. this.scriptException = scriptException;
  97. }
  98. /**
  99. * Assert that a {@link ScriptException} occurred within the JavaScript executing
  100. * on the associated {@link WebClient}.
  101. */
  102. public void assertHasException() {
  103. WebClientUtil.waitForJSExec(webClient);
  104. Assert.assertNotNull("A JavaScript Exception was expected.", scriptException);
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. @Override
  110. public void timeoutError(InteractivePage htmlPage, long allowedTime, long executionTime) {
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. @Override
  116. public void malformedScriptURL(InteractivePage htmlPage, String url, MalformedURLException malformedURLException) {
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. @Override
  122. public void loadScriptError(InteractivePage htmlPage, URL scriptUrl, Exception exception) {
  123. }
  124. }
  125. }