Skip to content

Commit

Permalink
getAttribute(property) should be able to use js properties also if th…
Browse files Browse the repository at this point in the history
…e exceution of js methods is disabled for the driver (issue #148)
  • Loading branch information
rbri committed May 18, 2024
1 parent ce3b06f commit 65d7d4c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ public String getAttribute(final String name) {
return "";
}

if (driver_.isJavascriptEnabled()) {
// it is sufficient to have to javascript engine enable to be
// able to use the properties from the script element
if (driver_.getWebClient().isJavaScriptEngineEnabled()) {
final HtmlUnitScriptable scriptable = element_.getScriptableObject();
if (scriptable != null) {
final Object slotVal = ScriptableObject.getProperty(scriptable, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.openqa.selenium.htmlunit;

import java.nio.charset.StandardCharsets;

import org.htmlunit.MockWebConnection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
Expand Down Expand Up @@ -429,4 +432,44 @@ public void src() throws Exception {
elem = driver.findElement(By.id("img3"));
assertEquals("https://www.htmlunit.org/src", elem.getAttribute("src"));
}

@Test
public void innerHTML() throws Exception {
final String html = "<html>\n"
+ "<head>\n"
+ "</head>\n"
+ "<body>\n"
+ " <div id='testDivId'>TestDiv</div>\n"
+ "</body>\n"
+ "</html>\n";

final WebDriver driver = loadPage2(html);
final WebElement elem = driver.findElement(By.id("testDivId"));
assertEquals("TestDiv", elem.getAttribute("innerHTML"));
}

@Test
public void innerHTMLJsDisabled() throws Exception {
final HtmlUnitDriver driver = new HtmlUnitDriver(false);
assertFalse(driver.isJavascriptEnabled());
assertFalse(driver.getWebClient().getOptions().isJavaScriptEnabled());
assertFalse(driver.getWebClient().isJavaScriptEnabled());
assertTrue(driver.getWebClient().isJavaScriptEngineEnabled());

final String html = "<html>\n"
+ "<head>\n"
+ "</head>\n"
+ "<body>\n"
+ " <div id='testDivId'>TestDiv</div>\n"
+ "</body>\n"
+ "</html>\n";

final MockWebConnection mockWebConnection = getMockWebConnection();
mockWebConnection.setDefaultResponse(html);
startWebServer(mockWebConnection, StandardCharsets.UTF_8);
driver.get(URL_FIRST.toExternalForm());

final WebElement elem = driver.findElement(By.id("testDivId"));
assertEquals("TestDiv", elem.getAttribute("innerHTML"));
}
}

0 comments on commit 65d7d4c

Please sign in to comment.