Skip to content

Commit 0f36aa9

Browse files
committed
Handle the case where executeScript returns an HTMLDocument.
The W3C spec says that if we return a document element, we should, instead, return document.documentElement.
1 parent 32dfa03 commit 0f36aa9

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

java/client/src/org/openqa/selenium/htmlunit/HtmlUnitDriver.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
import com.gargoylesoftware.htmlunit.html.HtmlElement;
5353
import com.gargoylesoftware.htmlunit.html.HtmlHtml;
5454
import com.gargoylesoftware.htmlunit.html.HtmlPage;
55+
import com.gargoylesoftware.htmlunit.javascript.host.Element;
5556
import com.gargoylesoftware.htmlunit.javascript.host.Location;
57+
import com.gargoylesoftware.htmlunit.javascript.host.html.DocumentProxy;
5658
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection;
5759
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement;
5860

@@ -760,6 +762,14 @@ private Object parseNativeJavascriptResult(Object result) {
760762
return newHtmlUnitWebElement(((HTMLElement) value).getDomNodeOrDie());
761763
}
762764

765+
if (value instanceof DocumentProxy) {
766+
Element element = ((DocumentProxy) value).getDelegee().getDocumentElement();
767+
if (element instanceof HTMLElement) {
768+
return newHtmlUnitWebElement(((HTMLElement) element).getDomNodeOrDie());
769+
}
770+
throw new WebDriverException("Do not know how to coerce to an HTMLElement: " + element);
771+
}
772+
763773
if (value instanceof Number) {
764774
final Number n = (Number) value;
765775
final String s = n.toString();

java/client/test/org/openqa/selenium/ExecutingJavascriptTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,19 @@ public void testShouldBeAbleToReturnADateObject() {
590590
try {
591591
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(date);
592592
} catch (ParseException e) {
593-
assertTrue(false);
593+
fail();
594594
}
595595
}
596596

597+
@JavascriptEnabled
598+
@Test(timeout = 10000)
599+
@Ignore(value = {ANDROID, CHROME, IE, IPHONE, OPERA, OPERA_MOBILE, PHANTOMJS, SAFARI, MARIONETTE})
600+
public void shouldReturnDocumentElementIfDocumentIsReturned() {
601+
driver.get(pages.simpleTestPage);
602+
603+
Object value = executeScript("return document");
604+
605+
assertTrue(value instanceof WebElement);
606+
assertTrue(((WebElement) value).getText().contains("A single line of text"));
607+
}
597608
}

javascript/firefox-driver/js/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,11 @@ Utils.wrapResult = function(result, doc) {
868868
return array;
869869
}
870870

871+
// Document. Grab the document element.
872+
if (result.nodeType == 9) {
873+
return Utils.wrapResult(result.documentElement);
874+
}
875+
871876
try {
872877
var nodeList = result.QueryInterface(CI.nsIDOMNodeList);
873878
var array = [];

0 commit comments

Comments
 (0)