Skip to content

Commit

Permalink
When the host is unknown, make the HtmlUnitDriver return an error page.
Browse files Browse the repository at this point in the history
Fixes issue SeleniumHQ#6512.
  • Loading branch information
shs96c committed Jan 29, 2014
1 parent e9b0326 commit e59594f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
11 changes: 8 additions & 3 deletions java/client/src/org/openqa/selenium/htmlunit/HtmlUnitDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.htmlunit;

import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_FINDING_BY_CSS;

import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
Expand All @@ -29,7 +31,9 @@
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.SgmlPage;
import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.TopLevelWindow;
import com.gargoylesoftware.htmlunit.UnexpectedPage;
import com.gargoylesoftware.htmlunit.WaitingRefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebClientOptions;
Expand Down Expand Up @@ -116,8 +120,6 @@
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_FINDING_BY_CSS;

public class HtmlUnitDriver implements WebDriver, JavascriptExecutor,
FindsById, FindsByLinkText, FindsByXPath, FindsByName, FindsByCssSelector,
FindsByTagName, FindsByClassName, HasCapabilities, HasInputDevices {
Expand Down Expand Up @@ -476,7 +478,10 @@ protected void get(URL fullUrl) {
// A "get" works over the entire page
currentWindow = getCurrentWindow().getTopWindow();
} catch (UnknownHostException e) {
// This should be fine
getCurrentWindow().getTopWindow().setEnclosedPage(new UnexpectedPage(
new StringWebResponse("Unknown host", fullUrl),
getCurrentWindow().getTopWindow()
));
} catch (ConnectException e) {
// This might be expected
} catch (SocketTimeoutException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2014 Selenium committers
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.openqa.selenium.htmlunit;


import static org.junit.Assert.assertNotEquals;

import org.junit.Test;
import org.openqa.selenium.testing.JUnit4TestBase;

public class ErroneousPageLoadingTest extends JUnit4TestBase {

@Test
public void shouldNotReturnSourceOfOldPageWhenLoadFailsDueToABadHost() {
driver.get(pages.xhtmlTestPage);
String originalSource = driver.getPageSource();

driver.get("http://thishostdoesnotexist.norshallitever");
String currentSource = driver.getPageSource();

assertNotEquals(originalSource, currentSource);
}
}

0 comments on commit e59594f

Please sign in to comment.