diff --git a/src/main/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.java b/src/main/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.java index 7cac0ef1..b2eef8d2 100644 --- a/src/main/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.java +++ b/src/main/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.java @@ -160,8 +160,6 @@ public class HtmlUnitDriver implements WebDriver, JavascriptExecutor, private Point windowPosition = new Point(0, 0); private Dimension initialWindowDimension; - private boolean enableJavascript; - private ProxyConfig proxyConfig; private long implicitWait = 0; private long scriptTimeout = 0; private HtmlUnitKeyboard keyboard; @@ -197,7 +195,16 @@ public class HtmlUnitDriver implements WebDriver, JavascriptExecutor, * and the {@link BrowserVersion#getDefault() default} BrowserVersion. */ public HtmlUnitDriver() { - this(false); + this(BrowserVersion.getDefault(), false); + } + + /** + * Constructs a new instance with the specified {@link BrowserVersion}. + * + * @param version the browser version to use + */ + public HtmlUnitDriver(BrowserVersion version) { + this(version, false); } /** @@ -217,17 +224,67 @@ public HtmlUnitDriver(boolean enableJavascript) { * @param enableJavascript whether to enable JavaScript support or not */ public HtmlUnitDriver(BrowserVersion version, boolean enableJavascript) { - this(version); - setJavascriptEnabled(enableJavascript); + this(version, enableJavascript, null); + + modifyWebClient(webClient); } /** - * Constructs a new instance with the specified {@link BrowserVersion}. + * The browserName is {@link BrowserType#HTMLUNIT} "htmlunit" and the browserVersion + * denotes the required browser AND its version. + * For example "chrome" for Chrome, "firefox-45" for Firefox 45 + * or "internet explorer" for IE. * - * @param version the browser version to use + * @param capabilities desired capabilities requested for the htmlunit driver session */ - public HtmlUnitDriver(BrowserVersion version) { - webClient = createWebClient(version); + public HtmlUnitDriver(Capabilities capabilities) { + this(determineBrowserVersion(capabilities), + capabilities.getCapability(JAVASCRIPT_ENABLED) == null || capabilities.is(JAVASCRIPT_ENABLED), + Proxy.extractFrom(capabilities)); + + setDownloadImages(capabilities.is(DOWNLOAD_IMAGES_CAPABILITY)); + + unexpectedAlertBehaviour = (UnexpectedAlertBehaviour) capabilities.getCapability(UNEXPECTED_ALERT_BEHAVIOUR); + if (unexpectedAlertBehaviour == null) { + unexpectedAlertBehaviour = UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY; + } + + Boolean acceptSslCerts = (Boolean) capabilities.getCapability(ACCEPT_SSL_CERTS); + if (acceptSslCerts == null) { + acceptSslCerts = true; + } + setAcceptSslCertificates(acceptSslCerts); + + String pageLoadStrategyString = (String) capabilities.getCapability(PAGE_LOAD_STRATEGY); + if ("none".equals(pageLoadStrategyString)) { + pageLoadStrategy = PageLoadStrategy.NONE; + } + else if ("eager".equals(pageLoadStrategyString)) { + pageLoadStrategy = PageLoadStrategy.EAGER; + } + + modifyWebClient(webClient); + } + + public HtmlUnitDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities) { + this(new DesiredCapabilities(desiredCapabilities, requiredCapabilities)); + } + + private HtmlUnitDriver(BrowserVersion version, boolean enableJavascript, Proxy proxy) { + webClient = newWebClient(version); + + final WebClientOptions clienOptions = webClient.getOptions(); + clienOptions.setHomePage(UrlUtils.URL_ABOUT_BLANK.toString()); + clienOptions.setThrowExceptionOnFailingStatusCode(false); + clienOptions.setPrintContentOnFailingStatusCode(false); + clienOptions.setRedirectEnabled(true); + clienOptions.setUseInsecureSSL(true); + + setJavascriptEnabled(enableJavascript); + setProxySettings(proxy); + + webClient.setRefreshHandler(new WaitingRefreshHandler()); + alert = new HtmlUnitAlert(this); currentWindow = webClient.getCurrentWindow(); initialWindowDimension = new Dimension(currentWindow.getOuterWidth(), currentWindow.getOuterHeight()); @@ -236,7 +293,6 @@ public HtmlUnitDriver(BrowserVersion version) { defaultExecutor = Executors.newCachedThreadPool(); executor = defaultExecutor; - webClient.addWebWindowListener(new WebWindowListener() { @Override public void webWindowOpened(WebWindowEvent webWindowEvent) { @@ -272,54 +328,13 @@ public void webWindowClosed(WebWindowEvent event) { }); // Now put us on the home page, like a real browser - get(webClient.getOptions().getHomePage()); + get(clienOptions.getHomePage()); gotPage = false; resetKeyboardAndMouseState(); options = new HtmlUnitOptions(); } - /** - * The browserName is {@link BrowserType#HTMLUNIT} "htmlunit" and the browserVersion - * denotes the required browser AND its version. - * For example "chrome" for Chrome, "firefox-45" for Firefox 45 - * or "internet explorer" for IE. - * - * @param capabilities desired capabilities requested for the htmlunit driver session - */ - public HtmlUnitDriver(Capabilities capabilities) { - this(determineBrowserVersion(capabilities)); - - setJavascriptEnabled(capabilities.getCapability(JAVASCRIPT_ENABLED) == null - || capabilities.is(JAVASCRIPT_ENABLED)); - - setProxySettings(Proxy.extractFrom(capabilities)); - - setDownloadImages(capabilities.is(DOWNLOAD_IMAGES_CAPABILITY)); - - unexpectedAlertBehaviour = (UnexpectedAlertBehaviour) capabilities.getCapability(UNEXPECTED_ALERT_BEHAVIOUR); - if (unexpectedAlertBehaviour == null) { - unexpectedAlertBehaviour = UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY; - } - - Boolean acceptSslCerts = (Boolean) capabilities.getCapability(ACCEPT_SSL_CERTS); - if (acceptSslCerts == null) { - acceptSslCerts = true; - } - setAcceptSslCertificates(acceptSslCerts); - - String pageLoadStrategyString = (String) capabilities.getCapability(PAGE_LOAD_STRATEGY); - if ("none".equals(pageLoadStrategyString)) { - pageLoadStrategy = PageLoadStrategy.NONE; - } - else if ("eager".equals(pageLoadStrategyString)) { - pageLoadStrategy = PageLoadStrategy.EAGER; - } - } - - public HtmlUnitDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities) { - this(new DesiredCapabilities(desiredCapabilities, requiredCapabilities)); - } static BrowserVersion determineBrowserVersion(Capabilities capabilities) { String capBrowserName = capabilities.getBrowserName(); @@ -386,27 +401,6 @@ else if (version == BrowserVersion.FIREFOX.getBrowserVersionNumeric()) { return browserVersionObject; } - private WebClient createWebClient(BrowserVersion version) { - WebClient client = newWebClient(version); - - final WebClientOptions clienOptions = client.getOptions(); - clienOptions.setHomePage(UrlUtils.URL_ABOUT_BLANK.toString()); - clienOptions.setThrowExceptionOnFailingStatusCode(false); - clienOptions.setPrintContentOnFailingStatusCode(false); - clienOptions.setJavaScriptEnabled(enableJavascript); - clienOptions.setRedirectEnabled(true); - clienOptions.setUseInsecureSSL(true); - - // Ensure that we've set the proxy if necessary - if (proxyConfig != null) { - clienOptions.setProxyConfig(proxyConfig); - } - - client.setRefreshHandler(new WaitingRefreshHandler()); - - return modifyWebClient(client); - } - /** * @return to process or not to proceed */ @@ -609,7 +603,7 @@ public void setProxy(String host, int port) { * @param noProxyHosts The list of hosts which need to bypass HTTP proxy */ public void setHTTPProxy(String host, int port, List noProxyHosts) { - proxyConfig = new ProxyConfig(); + ProxyConfig proxyConfig = new ProxyConfig(); proxyConfig.setProxyHost(host); proxyConfig.setProxyPort(port); if (noProxyHosts != null && noProxyHosts.size() > 0) { @@ -638,7 +632,7 @@ public void setSocksProxy(String host, int port) { * @param noProxyHosts The list of hosts which need to bypass SOCKS proxy */ public void setSocksProxy(String host, int port, List noProxyHosts) { - proxyConfig = new ProxyConfig(); + ProxyConfig proxyConfig = new ProxyConfig(); proxyConfig.setProxyHost(host); proxyConfig.setProxyPort(port); proxyConfig.setSocksProxy(true); @@ -668,7 +662,7 @@ public void setExecutor(Executor executor) { * @param autoProxyUrl The Proxy Autoconfiguration URL */ public void setAutoProxy(String autoProxyUrl) { - proxyConfig = new ProxyConfig(); + ProxyConfig proxyConfig = new ProxyConfig(); proxyConfig.setProxyAutoConfigUrl(autoProxyUrl); getWebClient().getOptions().setProxyConfig(proxyConfig); } @@ -1491,7 +1485,6 @@ public boolean isJavascriptEnabled() { } public void setJavascriptEnabled(boolean enableJavascript) { - this.enableJavascript = enableJavascript; getWebClient().getOptions().setJavaScriptEnabled(enableJavascript); } diff --git a/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java b/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java index 2c97943a..593a2597 100644 --- a/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java +++ b/src/test/java/org/openqa/selenium/htmlunit/HtmlUnitDriverTest.java @@ -18,10 +18,15 @@ package org.openqa.selenium.htmlunit; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.openqa.selenium.Proxy; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.remote.BrowserType; +import org.openqa.selenium.remote.CapabilityType; +import org.openqa.selenium.remote.DesiredCapabilities; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.WebClient; @@ -50,4 +55,377 @@ protected WebClient modifyWebClient(WebClient client) { assertTrue(webDriver instanceof HtmlUnitDriver); assertEquals("newWebClient-modifyWebClient", detector.toString()); } + + @Test + public void ctorWebClient() { + new HtmlUnitDriver(){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.getDefault(), client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientJsFalse() { + new HtmlUnitDriver(false){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.getDefault(), client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientJsTrue() { + new HtmlUnitDriver(true){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.getDefault(), client.getBrowserVersion()); + + assertTrue("client.getOptions().isJavaScriptEnabled() is false", client.getOptions().isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEnabled() is false", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientBrowserVersionChrome() { + new HtmlUnitDriver(BrowserVersion.CHROME){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.CHROME, client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientBrowserVersionEdge() { + new HtmlUnitDriver(BrowserVersion.EDGE){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.EDGE, client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientBrowserVersionFirefox() { + new HtmlUnitDriver(BrowserVersion.FIREFOX){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.FIREFOX, client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientBrowserVersionFirefox78() { + new HtmlUnitDriver(BrowserVersion.FIREFOX_78) { + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.FIREFOX_78, client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientBrowserVersionIE() { + new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.INTERNET_EXPLORER, client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientBrowserVersionFirefoxJsFalse() { + new HtmlUnitDriver(BrowserVersion.FIREFOX, false){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.FIREFOX, client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientBrowserVersionFirefox78JsTrue() { + new HtmlUnitDriver(BrowserVersion.FIREFOX_78, true){ + @Override + protected WebClient modifyWebClient(WebClient client){ + + assertEquals(BrowserVersion.FIREFOX_78, client.getBrowserVersion()); + + assertTrue("client.getOptions().isJavaScriptEnabled() is false", client.getOptions().isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEnabled() is false", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientCapabilitiesJsTrue() { + DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.getDefault(), client.getBrowserVersion()); + + assertTrue("client.getOptions().isJavaScriptEnabled() is false", client.getOptions().isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEnabled() is false", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientCapabilitiesJsFalse() { + DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, false); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.getDefault(), client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientCapabilitiesVersionString() { + DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setVersion("firefox"); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.FIREFOX, client.getBrowserVersion()); + + return client; + } + }; + + capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setVersion("googlechrome"); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.CHROME, client.getBrowserVersion()); + + return client; + } + }; + + capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setVersion("MicrosoftEdge"); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.EDGE, client.getBrowserVersion()); + + return client; + } + }; + + capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setVersion("firefox-78"); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.FIREFOX_78, client.getBrowserVersion()); + + return client; + } + }; + } + + @Test + public void ctorWebClientCapabilitiesVersion() { + DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setVersion(BrowserType.FIREFOX); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.FIREFOX, client.getBrowserVersion()); + + return client; + } + }; + + capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setVersion(BrowserType.CHROME); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.CHROME, client.getBrowserVersion()); + + return client; + } + }; + + capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setVersion(BrowserType.EDGE); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.EDGE, client.getBrowserVersion()); + + return client; + } + }; + + capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setVersion(BrowserType.FIREFOX + "-78"); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.FIREFOX_78, client.getBrowserVersion()); + + return client; + } + }; + } + + @Test + public void ctorWebClientCapabilitiesJsEnabledTrue() { + DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setJavascriptEnabled(true); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.getDefault(), client.getBrowserVersion()); + + assertTrue("client.getOptions().isJavaScriptEnabled() is false", client.getOptions().isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEnabled() is false", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientCapabilitiesJsEnabledFalse() { + DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setJavascriptEnabled(false); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.getDefault(), client.getBrowserVersion()); + + assertFalse("client.getOptions().isJavaScriptEnabled() is true", client.getOptions().isJavaScriptEnabled()); + assertFalse("client.isJavaScriptEnabled() is true", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + return client; + } + }; + } + + @Test + public void ctorWebClientCapabilitiesProxy() { + Proxy proxy = new Proxy(); + proxy.setHttpProxy("hostname:1234"); + + DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit(); + capabilities.setCapability(CapabilityType.PROXY, proxy); + + new HtmlUnitDriver(capabilities){ + @Override + protected WebClient modifyWebClient(WebClient client){ + assertEquals(BrowserVersion.getDefault(), client.getBrowserVersion()); + + assertTrue("client.getOptions().isJavaScriptEnabled() is false", client.getOptions().isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEnabled() is false", client.isJavaScriptEnabled()); + assertTrue("client.isJavaScriptEngineEnabled() is false", client.isJavaScriptEngineEnabled()); + + assertEquals("hostname", client.getOptions().getProxyConfig().getProxyHost()); + assertEquals(1234, client.getOptions().getProxyConfig().getProxyPort()); + + return client; + } + }; + } }