Skip to content

Commit

Permalink
fixes 1374: ShadowRoot in handle ChromeDriver 96+ (#1409)
Browse files Browse the repository at this point in the history
Co-authored-by: filip.cynarski <filip.cynarski@ocado.com>
  • Loading branch information
filipcynarski and filip.cynarski committed Dec 15, 2021
1 parent 47dd3d7 commit 25a3150
Showing 1 changed file with 28 additions and 2 deletions.
Expand Up @@ -7,15 +7,19 @@
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.fluentlenium.core.FluentPage;
import org.fluentlenium.core.annotation.Unshadow;
import org.fluentlenium.core.domain.FluentWebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -68,8 +72,30 @@ private List<WebElement> extractElementsFromShadowRoot(List<WebElement> previous
}

private WebElement unshadow(WebElement elements) {
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
return (WebElement) executor.executeScript("return arguments[0].shadowRoot", elements);
WebElement returnObj = null;

JavascriptExecutor executor = ((JavascriptExecutor) webDriver);
Object shadowRoot = executor.executeScript("return arguments[0].shadowRoot", elements);

if (shadowRoot instanceof WebElement) {
// ChromeDriver 95
returnObj = (WebElement) shadowRoot;
}
else if (shadowRoot instanceof Map) {
// ChromeDriver 96+
// Based on https://github.com/SeleniumHQ/selenium/issues/10050#issuecomment-974231601
Map<String, Object> shadowRootMap = (Map<String, Object>) shadowRoot;
String shadowRootKey = (String) shadowRootMap.keySet().toArray()[0];
String id = (String) shadowRootMap.get(shadowRootKey);
RemoteWebElement remoteWebElement = new RemoteWebElement();
remoteWebElement.setParent((RemoteWebDriver) webDriver);
remoteWebElement.setId(id);
returnObj = remoteWebElement;
}
else {
LOGGER.error("Unexpected return type for shadowRoot in expandRootElement()");
}
return returnObj;
}

private void setValue(Field field, List<FluentWebElement> elements) {
Expand Down

0 comments on commit 25a3150

Please sign in to comment.