Skip to content

Commit

Permalink
Add ability to hide Shadow DOM attachments in report
Browse files Browse the repository at this point in the history
  • Loading branch information
vkepin committed Oct 8, 2024
1 parent 9b44c31 commit 1077fa3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/modules/plugins/partials/selenium-properties.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ a|`true` +
|`true`
|Whether to publish the application source code on failure or not

|`ui.publish-shadow-dom-source-on-failure`
a|`true` +
`false`
|`true`
|Whether to publish the source code of Shadow DOM elements on failure or not

|`ui.context.self-healing`
a|`true` +
`false`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ui.wait.timeout=PT1M
ui.wait.polling-period=PT2S

ui.publish-source-on-failure=true
ui.publish-shadow-dom-source-on-failure=true

screenshot.on-failure.debug-modes=

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class WebContextSourceCodeProvider implements ContextSourceCodeProvider
private final IUiContext uiContext;
private final WebJavascriptActions webJavascriptActions;

private boolean collectShadowDomSourceCode;

protected WebContextSourceCodeProvider(IUiContext uiContext, WebJavascriptActions webJavascriptActions)
{
this.uiContext = uiContext;
Expand All @@ -62,7 +64,10 @@ else if (searchContext instanceof WebDriver webDriver)
{
sources.put(APPLICATION_SOURCE_CODE, sourceCode);
}
sources.putAll(getShadowDomSourceCode(elementInContext, searchContext));
if (collectShadowDomSourceCode)
{
sources.putAll(getShadowDomSourceCode(elementInContext, searchContext));
}
return sources;
}

Expand Down Expand Up @@ -103,4 +108,9 @@ private String getElementSource(SearchContext searchContext)
return null;
}
}

public void setCollectShadowDomSourceCode(boolean collectShadowDomSourceCode)
{
this.collectShadowDomSourceCode = collectShadowDomSourceCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@
<bean class="org.vividus.ui.web.monitor.PublishingWebScreenshotOnFailureMonitor"
parent="abstractPublishingScreenshotOnFailureMonitor"/>

<bean class="org.vividus.selenium.WebContextSourceCodeProvider" />
<bean class="org.vividus.selenium.WebContextSourceCodeProvider">
<property name="collectShadowDomSourceCode" value="${ui.publish-shadow-dom-source-on-failure}" />
</bean>

<bean class="org.vividus.ui.listener.SourceCodePublishingOnFailureListener" lazy-init="false">
<property name="publishSourceOnFailure" value="${ui.publish-source-on-failure}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.util.List;
Expand Down Expand Up @@ -73,6 +74,7 @@ class WebContextSourceCodeProviderTests
@Test
void shouldReturnWholePageForDriverContext()
{
sourceCodeProvider.setCollectShadowDomSourceCode(true);
var webDriver = mock(WebDriver.class);
when(uiContext.getSearchContext()).thenReturn(webDriver);
var pageSource = "<html/>";
Expand All @@ -85,9 +87,23 @@ void shouldReturnWholePageForDriverContext()
sourceCodeProvider.getSourceCode());
}

@SuppressFBWarnings("VA_FORMAT_STRING_USES_NEWLINE")
@Test
void shouldReturnWholePageForDriverContextNoShadowDom()
{
sourceCodeProvider.setCollectShadowDomSourceCode(false);
var webDriver = mock(WebDriver.class);
when(uiContext.getSearchContext()).thenReturn(webDriver);
var pageSource = "<html></html>";
when(webDriver.getPageSource()).thenReturn(pageSource);
assertEquals(Map.of(APPLICATION_SOURCE_CODE, pageSource), sourceCodeProvider.getSourceCode());
verifyNoInteractions(webJavascriptActions);
}

@Test
void shouldReturnElementSourceForElementContext()
{
sourceCodeProvider.setCollectShadowDomSourceCode(true);
var webElement = mock(WebElement.class);
when(uiContext.getSearchContext()).thenReturn(webElement);
var elementSource = "<div/>";
Expand All @@ -101,6 +117,7 @@ void shouldReturnElementSourceForElementContext()
@Test
void shouldHandleStaleElementsCorrectly()
{
sourceCodeProvider.setCollectShadowDomSourceCode(true);
var webElement = mock(WebElement.class);
when(uiContext.getSearchContext()).thenReturn(webElement);
when(webJavascriptActions.executeScript(OUTER_HTML_SCRIPT, webElement)).thenThrow(
Expand All @@ -113,6 +130,7 @@ void shouldHandleStaleElementsCorrectly()
@Test
void shouldReturnEmptyValueForNullSearchContext()
{
sourceCodeProvider.setCollectShadowDomSourceCode(true);
when(uiContext.getSearchContext()).thenReturn(null);
when(webJavascriptActions.executeScript(any(String.class))).thenReturn(Map.of());
assertEquals(Map.of(), sourceCodeProvider.getSourceCode());
Expand Down

0 comments on commit 1077fa3

Please sign in to comment.