Skip to content

Commit

Permalink
Remove unnecessary shadow root helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- committed Dec 14, 2021
1 parent c8891f9 commit f43ee46
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,52 +189,6 @@ protected static void assertEquals(WebElement expectedElement,
Assert.assertEquals(unwrappedExpected, unwrappedActual);
}

/**
* Returns <code>true</code> if a component can be found with given By
* selector in the shadow DOM of the {@code webComponent}.
*
* @param webComponent
* the web component owning shadow DOM to start search from
* @param by
* the selector used to find element
* @return <code>true</code> if the component can be found
*/
protected boolean isPresentInShadowRoot(WebElement webComponent, By by) {
return !findInShadowRoot(webComponent, by).isEmpty();
}

/**
* Find the first {@link WebElement} using the given {@link By} selector.
*
* @param shadowRootOwner
* the web component owning shadow DOM to start search from
* @param by
* the selector used to find element
* @return an element from shadow root, if located
* @throws AssertionError
* if shadow root is not present or element is not found in the
* shadow root
*/
protected WebElement getInShadowRoot(WebElement shadowRootOwner, By by) {
return getShadowRoot(shadowRootOwner).findElements(by).stream()
.findFirst().orElseThrow(() -> new AssertionError(
"Could not find required element in the shadowRoot"));
}

/**
* Find all {@link WebElement}s using the given {@link By} selector.
*
* @param webComponent
* the web component owning shadow DOM to start search from
* @param by
* the selector used to find elements
* @return a list of found elements
*/
protected List<WebElement> findInShadowRoot(WebElement webComponent,
By by) {
return getShadowRoot(webComponent).findElements(by);
}

/**
* Executes the given JavaScript.
* <p>
Expand Down Expand Up @@ -419,16 +373,6 @@ protected void waitForDevServer() {
} while (Boolean.TRUE.equals(result));
}

private WebElement getShadowRoot(WebElement webComponent) {
waitUntil(driver -> getCommandExecutor().executeScript(
"return arguments[0].shadowRoot", webComponent) != null);
WebElement shadowRoot = (WebElement) getCommandExecutor()
.executeScript("return arguments[0].shadowRoot", webComponent);
Assert.assertNotNull("Could not locate shadowRoot in the element",
shadowRoot);
return shadowRoot;
}

/**
* Calls the {@code blur()} function on the current active element of the
* page, if any.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.vaadin.flow.component.html.testbench.NativeButtonElement;
import com.vaadin.flow.component.html.testbench.SpanElement;
import com.vaadin.flow.testutil.ChromeBrowserTest;
import com.vaadin.testbench.TestBenchElement;

import static com.vaadin.flow.navigate.HelloWorldView.IS_CONNECTED_ON_ATTACH;
import static com.vaadin.flow.navigate.HelloWorldView.IS_CONNECTED_ON_INIT;
Expand All @@ -47,8 +48,9 @@ public void openFlowView_navigateToTsView_navigationSuccessful() {

Assert.assertThat(getDriver().getCurrentUrl(),
CoreMatchers.endsWith("/about"));
Assert.assertTrue(getInShadowRoot(findElement(By.tagName("about-view")),
By.id("navigate-hello")).isDisplayed());
TestBenchElement aboutView = $("about-view").first();

Assert.assertTrue(aboutView.$("*").id("navigate-hello").isDisplayed());
}

@Test
Expand All @@ -61,8 +63,8 @@ public void openTsView_navigateToFlow_navigationSuccessful() {
waitUntil(input -> $("about-view").first().$("a").id("navigate-hello")
.isDisplayed());

getInShadowRoot(findElement(By.tagName("about-view")),
By.id("navigate-hello")).click();
TestBenchElement aboutView = $("about-view").first();
aboutView.$("*").id("navigate-hello").click();

getCommandExecutor().waitForVaadin();

Expand All @@ -88,8 +90,9 @@ public void openFlowView_isConnectedOnAttach() {
waitUntil(input -> $("about-view").first().$("a").id("navigate-hello")
.isDisplayed());

getInShadowRoot(findElement(By.tagName("about-view")),
By.id("navigate-hello")).click();
TestBenchElement aboutView = $("about-view").first();

aboutView.$("*").id("navigate-hello").click();

assertIsConnected();
}
Expand All @@ -102,8 +105,8 @@ public void openTsView_navigateToFlowView_isConnectedOnAttach() {
waitUntil(input -> $("about-view").first().$("a").id("navigate-hello")
.isDisplayed());

getInShadowRoot(findElement(By.tagName("about-view")),
By.id("navigate-hello")).click();
TestBenchElement aboutView = $("about-view").first();
aboutView.$("*").id("navigate-hello").click();

getCommandExecutor().waitForVaadin();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openqa.selenium.mobile.NetworkConnection;

import com.vaadin.flow.testutil.ChromeDeviceTest;
import com.vaadin.testbench.TestBenchElement;

import elemental.json.Json;
import elemental.json.JsonObject;
Expand Down Expand Up @@ -114,9 +115,9 @@ public void offlineNonRoot_reload_viewReloaded() throws IOException {

MatcherAssert.assertThat(getDriver().getCurrentUrl(),
CoreMatchers.endsWith("/another"));
TestBenchElement anotherView = $("another-view").first();
Assert.assertTrue(
getInShadowRoot(findElement(By.tagName("another-view")),
By.id("another-content")).isDisplayed());
anotherView.$("*").id("another-content").isDisplayed());
} finally {
// Reset network conditions back
setConnectionType(NetworkConnection.ConnectionType.ALL);
Expand Down Expand Up @@ -149,9 +150,9 @@ public void offlineDeepPath_reload_viewReloaded_baseUrlRewritten()

MatcherAssert.assertThat(getDriver().getCurrentUrl(),
CoreMatchers.endsWith("/another"));
TestBenchElement anotherView = $("another-view").first();
Assert.assertTrue(
getInShadowRoot(findElement(By.tagName("another-view")),
By.id("another-content")).isDisplayed());
anotherView.$("*").id("another-content").isDisplayed());

// Verify <base href> by navigating with a relative link
$("main-view").first().$("a").id("menu-about").click();
Expand Down Expand Up @@ -189,9 +190,9 @@ public void offlineTsView_navigateToOtherTsView_navigationSuccessful()

MatcherAssert.assertThat(getDriver().getCurrentUrl(),
CoreMatchers.endsWith("/another"));
TestBenchElement anotherView = $("another-view").first();
Assert.assertTrue(
getInShadowRoot(findElement(By.tagName("another-view")),
By.id("another-content")).isDisplayed());
anotherView.$("*").id("another-content").isDisplayed());
} finally {
// Reset network conditions back
setConnectionType(NetworkConnection.ConnectionType.ALL);
Expand All @@ -218,9 +219,9 @@ public void offlineServerView_navigateToTsView_navigationSuccessful()

MatcherAssert.assertThat(getDriver().getCurrentUrl(),
CoreMatchers.endsWith("/another"));
TestBenchElement anotherView = $("another-view").first();
Assert.assertTrue(
getInShadowRoot(findElement(By.tagName("another-view")),
By.id("another-content")).isDisplayed());
anotherView.$("*").id("another-content").isDisplayed());
} finally {
// Reset network conditions back
setConnectionType(NetworkConnection.ConnectionType.ALL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package com.vaadin.flow.uitest.ui.template;

import com.vaadin.flow.testutil.ChromeBrowserTest;
import com.vaadin.testbench.TestBenchElement;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

import com.vaadin.flow.testutil.ChromeBrowserTest;
import com.vaadin.testbench.TestBenchElement;

public class AttachExistingElementByIdIT extends ChromeBrowserTest {

@Test
Expand All @@ -39,29 +38,31 @@ private void assertTemplate(String id) {

private void assertTemplate(String id, String initialLabelText,
String placeholder) {
WebElement input = getInput(id);
TestBenchElement template = $("*").id(id);
WebElement input = getInput(template);

Assert.assertEquals(initialLabelText, getLabel(id).getText());
Assert.assertEquals(initialLabelText, getLabel(template).getText());

Assert.assertEquals(placeholder, input.getAttribute("placeholder"));

input.sendKeys("Harley!");
input.sendKeys(Keys.ENTER);

Assert.assertEquals("Text from input Harley!", getLabel(id).getText());
Assert.assertEquals("Text from input Harley!",
getLabel(template).getText());

// Reset values to defaults
$(TestBenchElement.class).id(id).$(TestBenchElement.class).id("button")
.click();

Assert.assertEquals("default", getLabel(id).getText());
Assert.assertEquals("default", getLabel(template).getText());
}

private WebElement getInput(String id) {
return getInShadowRoot(findElement(By.id(id)), By.id("input"));
private WebElement getInput(TestBenchElement template) {
return template.$("*").id("input");
}

private WebElement getLabel(String id) {
return getInShadowRoot(findElement(By.id(id)), By.id("label"));
private WebElement getLabel(TestBenchElement template) {
return template.$("*").id("label");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public void indexPageGetsWebComponent_attributeIsReflectedToServer() {
waitForElementVisible(By.id("show-message"));

TestBenchElement showMessage = byId("show-message");
waitUntil(driver -> isPresentInShadowRoot(showMessage,
By.tagName("select")));
waitUntil(driver -> showMessage.$("select").exists());
TestBenchElement select = showMessage.$("select").first();

// Selection is visibly changed and event manually dispatched
Expand Down Expand Up @@ -69,7 +68,8 @@ public void downloadLinkHasCorrectBaseURL() {

waitForElementVisible(By.id("show-message"));
TestBenchElement showMessage = byId("show-message");
waitUntil(driver -> isPresentInShadowRoot(showMessage, By.id("link")));
waitUntil(
driver -> showMessage.$("*").attribute("id", "link").exists());
TestBenchElement link = showMessage.$("a").id("link");
String href = link.getAttribute("href");
// self check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ public void stylesCssImport_externalLinkAddedToShadowroot() {
open();
checkLogsForErrors();
final TestBenchElement themedComponent = $("themed-component").first();
final List<WebElement> links = findInShadowRoot(themedComponent,
By.tagName("link"));
final List<TestBenchElement> links = themedComponent.$("link").all();

List<String> linkUrls = links.stream()
.map(link -> link.getAttribute("href"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public void liveReloadOnTouchedFrontendFile() {
// then: the frontend changes are visible in the DOM
WebElement customComponent = findElement(
By.id(FrontendLiveReloadView.CUSTOM_COMPONENT));
WebElement embeddedDiv = findInShadowRoot(customComponent,
By.id("custom-div")).get(0);
WebElement embeddedDiv = customComponent.$("*").id("custom-div");
Assert.assertEquals("Updated component contents",
embeddedDiv.getText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ public void overlayShouldRender() {
Assert.assertEquals(1, liveReloads.size());
WebElement liveReload = liveReloads.get(0);

WebElement window = findInShadowRoot(liveReload, By.className("window"))
.get(0);
WebElement window = liveReload.$("*")
.attributeContains("class", "window").first();
Assert.assertFalse(window.isDisplayed());

// After clicking the icon in the indicator, the live-reload message
// window should appear
WebElement liveReloadIcon = findInShadowRoot(liveReload,
By.className("gizmo")).get(0);
WebElement liveReloadIcon = liveReload.$("*")
.attributeContains("class", "gizmo").first();
liveReloadIcon.click();

waitForElementPresent(By.tagName("vaadin-devmode-gizmo"));

WebElement window2 = findInShadowRoot(liveReload, By.className("gizmo"))
.get(0);
WebElement window2 = liveReload.$("*")
.attributeContains("class", "gizmo").first();
Assert.assertTrue(window2.isDisplayed());
}

Expand All @@ -69,17 +69,18 @@ public void splashMessageShownOnAutoReloadAndClosedOnBodyClick() {

WebElement liveReload = findElement(By.tagName("vaadin-devmode-gizmo"));
Assert.assertNotNull(liveReload);
WebElement gizmo1 = findInShadowRoot(liveReload, By.className("gizmo"))
.get(0);
WebElement gizmo1 = liveReload.$("*")
.attributeContains("class", "gizmo").first();

Assert.assertTrue(gizmo1.getAttribute("class").contains("active"));

findElement(By.tagName("body")).click();

WebElement liveReload2 = findElement(
By.tagName("vaadin-devmode-gizmo"));
Assert.assertNotNull(liveReload2);
WebElement gizmo2 = findInShadowRoot(liveReload2, By.className("gizmo"))
.get(0);
WebElement gizmo2 = liveReload2.$("*")
.attributeContains("class", "gizmo").first();
Assert.assertFalse(gizmo2.getAttribute("class").contains("active"));
Assert.assertTrue(gizmo2.getAttribute("class").contains("gizmo"));
}
Expand All @@ -101,8 +102,8 @@ public void deactivateLiveReload() {
// then: page is not reloaded
WebElement liveReload2 = findElement(
By.tagName("vaadin-devmode-gizmo"));
WebElement gizmo2 = findInShadowRoot(liveReload2, By.className("gizmo"))
.get(0);
WebElement gizmo2 = liveReload2.$("*")
.attributeContains("class", "gizmo").first();
Assert.assertFalse(gizmo2.getAttribute("class").contains("active"));
Assert.assertTrue(gizmo2.getAttribute("class").contains("gizmo"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void notificationShownWhenLoadingPreserveOnRefreshView() {

WebElement liveReload = findElement(By.tagName("vaadin-devmode-gizmo"));
Assert.assertNotNull(liveReload);
WebElement messageDetails = findInShadowRoot(liveReload,
By.className("warning")).get(0);
WebElement messageDetails = liveReload.$("*")
.attributeContains("class", "warning").first();
Assert.assertTrue(
messageDetails.getText().contains("@PreserveOnRefresh"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.vaadin.flow.component.html.testbench.NativeButtonElement;
import com.vaadin.flow.testutil.ChromeBrowserTest;
import com.vaadin.testbench.TestBenchElement;

public class InnerTemplateVisibilityIT extends ChromeBrowserTest {

Expand All @@ -21,10 +22,10 @@ public void innerTemplateIsHiddenWithDisplayNone() {

// then: element is not visible, attribute 'hidden' and 'display: none'
// set
WebElement outer = findElement(
By.id(InnerTemplateVisibilityView.OUTER_ID));
WebElement inner = findInShadowRoot(outer,
By.id(InnerTemplateVisibilityView.INNER_ID)).get(0);
TestBenchElement outer = $("*")
.id(InnerTemplateVisibilityView.OUTER_ID);
TestBenchElement inner = outer.$("*")
.id(InnerTemplateVisibilityView.INNER_ID);
Assert.assertFalse("expected inner to be hidden", inner.isDisplayed());
Assert.assertNotNull("expected attribute hidden on inner",
inner.getAttribute("hidden"));
Expand All @@ -44,10 +45,10 @@ public void innerTemplateDisplayStyleRestored() {

// then: element is visible, attribute and 'display: none' are no longer
// present
WebElement outer = findElement(
By.id(InnerTemplateVisibilityView.OUTER_ID));
WebElement inner = findInShadowRoot(outer,
By.id(InnerTemplateVisibilityView.INNER_ID)).get(0);
TestBenchElement outer = $("*")
.id(InnerTemplateVisibilityView.OUTER_ID);
TestBenchElement inner = outer.$("*")
.id(InnerTemplateVisibilityView.INNER_ID);
Assert.assertTrue("expected inner to be visible", inner.isDisplayed());
Assert.assertNull("inner should not have attribute hidden",
inner.getAttribute("hidden"));
Expand Down
Loading

0 comments on commit f43ee46

Please sign in to comment.