Skip to content

Commit

Permalink
[java] Add test to check that executeScript returns a ShadowRoot object
Browse files Browse the repository at this point in the history
  • Loading branch information
AutomatedTester committed Nov 16, 2021
1 parent af66838 commit 2e32ee1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions java/src/org/openqa/selenium/remote/ShadowRoot.java
Expand Up @@ -63,6 +63,10 @@ public WebDriver getWrappedDriver() {
return parent;
}

public String getId() {
return this.id;
}

private Map<String, Object> toJson() {
return singletonMap(W3C.getShadowRootElementKey(), id);
}
Expand Down
26 changes: 26 additions & 0 deletions java/test/org/openqa/selenium/remote/ShadowDomTest.java
Expand Up @@ -23,6 +23,7 @@
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchShadowRootException;
import org.openqa.selenium.SearchContext;
Expand Down Expand Up @@ -175,4 +176,29 @@ public void failingToFindAnElementFromAShadowRootThrowsAnException() {
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> context.findElement(By.cssSelector("#cheese")));
}

@Test
public void shouldBeAbleToGetShadowRootFromExecuteScript() {
String shadowId = UUID.randomUUID().toString();

HttpRequest execute = new HttpRequest(POST, String.format("/session/%s/execute/sync", id));

cannedResponses.put(
execute,
new HttpResponse()
.setContent(Contents.asJson(
singletonMap("value", singletonMap("shadow-6066-11e4-a52e-4f735466cecf", shadowId)))));

HttpRequest shadow = new HttpRequest(GET, String.format("/session/%s/element/%s/shadow", id, elementId));
cannedResponses.put(
shadow,
new HttpResponse()
.setContent(Contents.asJson(
singletonMap("value", singletonMap("shadow-6066-11e4-a52e-4f735466cecf", shadowId))))
);

ShadowRoot shadowContext = (ShadowRoot) element.getShadowRoot();
ShadowRoot executeContext = (ShadowRoot)((JavascriptExecutor)driver).executeScript("return Arguments[0].shadowRoot");
assertThat(shadowContext.getId()).isEqualTo(executeContext.getId());
}
}

0 comments on commit 2e32ee1

Please sign in to comment.