001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.web.servlet.htmlunit.webdriver; 018 019import com.gargoylesoftware.htmlunit.BrowserVersion; 020import com.gargoylesoftware.htmlunit.WebClient; 021import com.gargoylesoftware.htmlunit.WebConnection; 022import org.openqa.selenium.Capabilities; 023import org.openqa.selenium.htmlunit.HtmlUnitDriver; 024 025import org.springframework.util.Assert; 026 027/** 028 * {@code WebConnectionHtmlUnitDriver} enables configuration of the 029 * {@link WebConnection} for an {@link HtmlUnitDriver} instance. 030 * 031 * <p>This is useful because it allows a 032 * {@link org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection 033 * MockMvcWebConnection} to be injected. 034 * 035 * @author Rob Winch 036 * @author Sam Brannen 037 * @since 4.2 038 * @see MockMvcHtmlUnitDriverBuilder 039 */ 040public class WebConnectionHtmlUnitDriver extends HtmlUnitDriver { 041 042 private WebClient webClient; 043 044 045 public WebConnectionHtmlUnitDriver() { 046 } 047 048 public WebConnectionHtmlUnitDriver(BrowserVersion browserVersion) { 049 super(browserVersion); 050 } 051 052 public WebConnectionHtmlUnitDriver(boolean enableJavascript) { 053 super(enableJavascript); 054 } 055 056 public WebConnectionHtmlUnitDriver(Capabilities capabilities) { 057 super(capabilities); 058 } 059 060 061 /** 062 * Modify the supplied {@link WebClient} and retain a reference to it so that its 063 * {@link WebConnection} is {@linkplain #getWebConnection accessible} for later use. 064 * <p>Delegates to {@link HtmlUnitDriver#modifyWebClient} for default behavior 065 * and to {@link #modifyWebClientInternal} for further customization. 066 * @param webClient the client to modify 067 * @return the modified client 068 * @see HtmlUnitDriver#modifyWebClient(WebClient) 069 * @see #modifyWebClientInternal(WebClient) 070 */ 071 @Override 072 protected final WebClient modifyWebClient(WebClient webClient) { 073 this.webClient = super.modifyWebClient(webClient); 074 this.webClient = modifyWebClientInternal(this.webClient); 075 return this.webClient; 076 } 077 078 /** 079 * Modify the supplied {@link WebClient}. 080 * <p>The default implementation simply returns the supplied client unmodified. 081 * <p>Subclasses can override this method to customize the {@code WebClient} 082 * that the {@link HtmlUnitDriver} uses. 083 * @param webClient the client to modify 084 * @return the modified client 085 */ 086 protected WebClient modifyWebClientInternal(WebClient webClient) { 087 return webClient; 088 } 089 090 /** 091 * Return the current {@link WebClient}. 092 * @since 4.3 093 */ 094 @Override 095 public WebClient getWebClient() { 096 return this.webClient; 097 } 098 099 /** 100 * Set the {@link WebConnection} to be used with the {@link WebClient}. 101 * @param webConnection the {@code WebConnection} to use 102 */ 103 public void setWebConnection(WebConnection webConnection) { 104 Assert.notNull(webConnection, "WebConnection must not be null"); 105 this.webClient.setWebConnection(webConnection); 106 } 107 108 /** 109 * Access the current {@link WebConnection} for the {@link WebClient}. 110 * @return the current {@code WebConnection} 111 */ 112 public WebConnection getWebConnection() { 113 return this.webClient.getWebConnection(); 114 } 115 116}