提交 405af038 编写于 作者: T tfennelly

Reverting the WebClientResponseLoadListenable stuff

No need for it as it turns out. We can get the "current" page (i.e the form submit result) from the webclient.
上级 a35bba6e
......@@ -26,12 +26,7 @@ package com.gargoylesoftware.htmlunit.html;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebWindow;
import org.junit.Assert;
import org.jvnet.hudson.test.WebClientResponseLoadListenable;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.List;
......@@ -53,34 +48,22 @@ public class HtmlFormUtil {
final HtmlPage htmlPage = (HtmlPage) htmlForm.getPage();
final WebClient webClient = htmlPage.getWebClient();
Page resultPage = null;
try {
resultPage = htmlForm.submit((SubmittableElement) submitElement);
htmlForm.submit((SubmittableElement) submitElement);
} finally {
// The HtmlForm submit doesn't really do anything. It just adds a "LoadJob"
// to an internal queue. What we are doing here is manually forcing the load of
// the response for that submit LoadJob and listening in on the WebClient
// instance for the response, allowing us to create the correct HtmlPage
// object for return to the tests.
if (webClient instanceof WebClientResponseLoadListenable) {
FormSubmitResponseLoadListener loadListener = new FormSubmitResponseLoadListener();
((WebClientResponseLoadListenable) webClient).addResponseLoadListener(loadListener);
try {
webClient.loadDownloadedResponses();
resultPage = loadListener.getPage();
} finally {
((WebClientResponseLoadListenable) webClient).removeResponseLoadListener(loadListener);
}
if (resultPage == htmlPage) {
// We're still on the same page (form submit didn't bring us anywhere).
// Hackery. Seems like YUI is messing us about.
return submitElement.click();
}
} else {
Assert.fail("WebClient doesn't implement WebClientResponseLoadListenable.");
// the response for that submit LoadJob and then getting the enclosing page
// from the current window on the WebClient, allowing us to return the correct
// HtmlPage object to the test.
webClient.loadDownloadedResponses();
Page resultPage = webClient.getCurrentWindow().getEnclosedPage();
if (resultPage == htmlPage) {
// We're still on the same page (form submit didn't bring us anywhere).
// Hackery. Seems like YUI is messing us about.
return submitElement.click();
}
return resultPage;
......@@ -125,18 +108,4 @@ public class HtmlFormUtil {
}
throw new ElementNotFoundException("button", "caption", caption);
}
private static class FormSubmitResponseLoadListener implements WebClientResponseLoadListenable.WebClientResponseLoadListener {
private WebWindow webWindow;
@Override
public void onLoad(@Nonnull WebResponse webResponse, @Nonnull WebWindow webWindow) {
this.webWindow = webWindow;
}
private Page getPage() {
if (webWindow == null) {
Assert.fail("Expected FormSubmitResponseLoadListener to be called on form submit.");
}
return webWindow.getEnclosedPage();
}
}
}
......@@ -1625,11 +1625,9 @@ public abstract class HudsonTestCase extends TestCase implements RootAction {
* Extends {@link com.gargoylesoftware.htmlunit.WebClient} and provide convenience methods
* for accessing Hudson.
*/
public class WebClient extends com.gargoylesoftware.htmlunit.WebClient implements WebClientResponseLoadListenable {
public class WebClient extends com.gargoylesoftware.htmlunit.WebClient {
private static final long serialVersionUID = 5808915989048338267L;
private List<WebClientResponseLoadListener> loadListeners = new CopyOnWriteArrayList<>();
public WebClient() {
// default is IE6, but this causes 'n.doScroll('left')' to fail in event-debug.js:1907 as HtmlUnit doesn't implement such a method,
// so trying something else, until we discover another problem.
......@@ -1691,31 +1689,6 @@ public abstract class HudsonTestCase extends TestCase implements RootAction {
//setTimeout(60*1000);
}
@Override
public void addResponseLoadListener(@Nonnull WebClientResponseLoadListener listener) {
removeResponseLoadListener(listener);
loadListeners.add(listener);
}
@Override
public void removeResponseLoadListener(@Nonnull WebClientResponseLoadListener listener) {
loadListeners.remove(listener);
}
@Override
public Page loadWebResponseInto(WebResponse webResponse, WebWindow webWindow) throws IOException, FailingHttpStatusCodeException {
try {
return super.loadWebResponseInto(webResponse, webWindow);
} finally {
if (!loadListeners.isEmpty()) {
for (WebClientResponseLoadListener listener : loadListeners) {
listener.onLoad(webResponse, webWindow);
}
}
}
}
/**
* Logs in to Jenkins.
*/
......
......@@ -1830,11 +1830,9 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {
* Extends {@link com.gargoylesoftware.htmlunit.WebClient} and provide convenience methods
* for accessing Hudson.
*/
public class WebClient extends com.gargoylesoftware.htmlunit.WebClient implements WebClientResponseLoadListenable {
public class WebClient extends com.gargoylesoftware.htmlunit.WebClient {
private static final long serialVersionUID = 5808915989048338267L;
private List<WebClientResponseLoadListener> loadListeners = new CopyOnWriteArrayList<>();
public WebClient() {
// default is IE6, but this causes 'n.doScroll('left')' to fail in event-debug.js:1907 as HtmlUnit doesn't implement such a method,
// so trying something else, until we discover another problem.
......@@ -1894,30 +1892,6 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {
//setTimeout(60*1000);
}
@Override
public void addResponseLoadListener(@Nonnull WebClientResponseLoadListener listener) {
removeResponseLoadListener(listener);
loadListeners.add(listener);
}
@Override
public void removeResponseLoadListener(@Nonnull WebClientResponseLoadListener listener) {
loadListeners.remove(listener);
}
@Override
public Page loadWebResponseInto(WebResponse webResponse, WebWindow webWindow) throws IOException, FailingHttpStatusCodeException {
try {
return super.loadWebResponseInto(webResponse, webWindow);
} finally {
if (!loadListeners.isEmpty()) {
for (WebClientResponseLoadListener listener : loadListeners) {
listener.onLoad(webResponse, webWindow);
}
}
}
}
/**
* Logs in to Jenkins.
*/
......
/*
* The MIT License
*
* Copyright (c) 2015, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jvnet.hudson.test;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebWindow;
import javax.annotation.Nonnull;
/**
* Hook into the HtmlUnit {@link org.jvnet.hudson.test.JenkinsRule.WebClient}
* response loading into a "window".
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
public interface WebClientResponseLoadListenable {
/**
* Add a listener.
* @param listener The listener.
*/
void addResponseLoadListener(@Nonnull WebClientResponseLoadListener listener);
/**
* Removing a listener.
* @param listener The listener.
*/
void removeResponseLoadListener(@Nonnull WebClientResponseLoadListener listener);
/**
* Load listener interface.
*/
public interface WebClientResponseLoadListener {
/**
* Response load event.
* @param webResponse The response.
* @param webWindow The window into which the response is being loaded.
*/
void onLoad(@Nonnull WebResponse webResponse, @Nonnull WebWindow webWindow);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册