Skip to content

Commit

Permalink
test: Remove unnecessary shadow root helpers (#12573)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- authored and ZheSun88 committed Dec 17, 2021
1 parent 67aaf35 commit 976ee2c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,52 +182,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 @@ -413,16 +367,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 @@ -40,8 +40,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 @@ -72,7 +71,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 @@ -210,8 +210,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 @@ -31,26 +31,22 @@ public void overlayShouldRender() {
open();
// Upon opening, the LiveReloadUI should show the indicator but not the
// message window
waitForElementPresent(By.tagName("vaadin-devmode-gizmo"));
List<WebElement> liveReloads = findElements(
By.tagName("vaadin-devmode-gizmo"));
Assert.assertEquals(1, liveReloads.size());
WebElement liveReload = liveReloads.get(0);
DevModeGizmoElement liveReload = $(DevModeGizmoElement.class).waitForFirst();

WebElement window = findInShadowRoot(liveReload, By.className("window"))
.get(0);
TestBenchElement 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 @@ -62,22 +58,22 @@ public void splashMessageShownOnAutoReloadAndClosedOnBodyClick() {
By.id(LiveReloadView.JAVA_LIVE_RELOAD_TRIGGER_BUTTON));
liveReloadTrigger.click();

WebElement liveReload = findElement(By.tagName("vaadin-devmode-gizmo"));
Assert.assertNotNull(liveReload);
WebElement gizmo1 = findInShadowRoot(liveReload, By.className("gizmo"))
.get(0);
Assert.assertTrue(
gizmo1.getAttribute("class").contains("active"));
waitForLiveReload();

DevModeGizmoElement liveReload = $(DevModeGizmoElement.class).waitForFirst();
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"));
DevModeGizmoElement liveReload2 = $(DevModeGizmoElement.class).waitForFirst();
Assert.assertNotNull(liveReload2);
WebElement gizmo2 = findInShadowRoot(liveReload2, By.className("gizmo"))
.get(0);
Assert.assertFalse(
gizmo2.getAttribute("class").contains("active"));

WebElement gizmo2 = liveReload2.$("*")
.attributeContains("class", "gizmo").first();
Assert.assertFalse(gizmo2.getAttribute("class").contains("active"));
Assert.assertTrue(gizmo2.getAttribute("class").contains("gizmo"));
}

Expand All @@ -86,15 +82,11 @@ public void deactivateLiveReload() {
open();

// given: live reload is deactivated
waitForElementPresent(By.tagName("vaadin-devmode-gizmo"));
WebElement liveReload = findElement(By.tagName("vaadin-devmode-gizmo"));

WebElement liveReloadIcon = findInShadowRoot(liveReload,
By.className("gizmo")).get(0);
DevModeGizmoElement liveReload = $(DevModeGizmoElement.class).waitForFirst();
WebElement liveReloadIcon = liveReload.$("*").attributeContains("class", "gizmo").first();
liveReloadIcon.click();

WebElement deactivateCheckbox = findInShadowRoot(liveReload,
By.id("toggle")).get(0);
WebElement deactivateCheckbox = liveReload.$("*").id("toggle").first();
deactivateCheckbox.click();

// when: live reload is triggered
Expand All @@ -103,10 +95,8 @@ public void deactivateLiveReload() {
liveReloadTrigger.click();

// then: page is not reloaded
WebElement liveReload2 = findElement(
By.tagName("vaadin-devmode-gizmo"));
WebElement gizmo2 = findInShadowRoot(liveReload2, By.className("gizmo"))
.get(0);
DevModeGizmoElement liveReload2 = $("vaadin-devmode-gizmo").first();
WebElement gizmo2 = .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 @@ -16,6 +16,9 @@
package com.vaadin.flow.uitest.ui;

import net.jcip.annotations.NotThreadSafe;

import com.vaadin.flow.testutil.DevModeGizmoElement;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
Expand All @@ -28,10 +31,9 @@ public class PreserveOnRefreshLiveReloadIT extends AbstractLiveReloadIT {
public void notificationShownWhenLoadingPreserveOnRefreshView() {
open();

WebElement liveReload = findElement(By.tagName("vaadin-devmode-gizmo"));
Assert.assertNotNull(liveReload);
WebElement messageDetails = findInShadowRoot(liveReload,
By.className("warning")).get(0);
DevModeGizmoElement liveReload = $(DevModeGizmoElement.class).waitForFirst();
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 @@ -27,17 +27,18 @@
import org.openqa.selenium.logging.LogEntry;

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

public class EmptyListsIT extends ChromeBrowserTest {

@Test
public void emptyListsAreProperlyHandled() {
open();

WebElement template = findElement(By.id("template"));
TestBenchElement template = $("*").id("template");

Assert.assertTrue(
isPresentInShadowRoot(template, By.className("item")));
template.$("*").attributeContains("class", "item").exists());

findElement(By.id("set-empty")).click();

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ public void openPage_allButtonsRenderSuccessfully() {
open();

waitUntil(input -> {
return !findInShadowRoot(
input.findElement(By.id("scalability-view")),
By.id(TemplateScalabilityView.COMPLETED)).isEmpty();
TestBenchElement view = $("*").id("scalability-view");
return view.$("*")
.attribute("id", TemplateScalabilityView.COMPLETED)
.exists();
});

WebElement viewTemplate = getDriver()
.findElement(By.id("scalability-view"));
int buttons = findInShadowRoot(viewTemplate,
By.tagName("template-scalability-panel")).size();
TestBenchElement viewTemplate = $("*").id("scalability-view");
int buttons = viewTemplate.$("template-scalability-panel").all().size();

Assert.assertEquals("Template should have created "
+ TemplateScalabilityView.NUM_ITEMS + " panels with buttons.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void lazyLoadedPolymerTemplateWorksAsElement() {
String input = "InputMaster";
Assert.assertFalse(
"No greeting should be present before we press the button",
isPresentInShadowRoot(template, By.id("greeting")));
template.$("*").attribute("id", "greeting").exists());

template.$(TestBenchElement.class).id("input").sendKeys(input);
template.$(TestBenchElement.class).id("button").click();
Expand Down

0 comments on commit 976ee2c

Please sign in to comment.