Skip to content

Commit

Permalink
test: Remove unnecessary shadow root helpers (#12573) (#12601) (#12610)
Browse files Browse the repository at this point in the history
Co-authored-by: Artur <[email protected]>

Co-authored-by: Zhe Sun <[email protected]>
Co-authored-by: Artur <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2021
1 parent ffe244e commit 79fe2a6
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 130 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 @@ -46,11 +46,11 @@ protected void assertTemplate(String id, String initialLabelText,
Assert.assertEquals("default", getLabel(id).getText());
}

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

protected WebElement getLabel(String id) {
return getInShadowRoot(findElement(By.id(id)), By.id("label"));
protected TestBenchElement getLabel(String id) {
return $("*").id(id).$("*").id("label");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.vaadin.flow.uitest.ui.template;

import com.vaadin.testbench.TestBenchElement;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
Expand All @@ -28,10 +29,10 @@ public class BundledTemplateInTemplateWithIdIT extends ChromeBrowserTest {
public void childTemplateInstanceHandlesEvent() {
open();

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

WebElement text = getInShadowRoot(child, By.id("text"));
WebElement text = child.$("*").id("text");
Assert.assertEquals("div", text.getTagName());
Assert.assertEquals("@Id injected!", text.getText());
}
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 @@ -17,6 +17,7 @@

import java.util.List;

import com.vaadin.testbench.TestBenchElement;
import net.jcip.annotations.NotThreadSafe;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -32,25 +33,24 @@ public void overlayShouldRender() {
// 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"));
List<TestBenchElement> liveReloads = $("vaadin-devmode-gizmo").all();
Assert.assertEquals(1, liveReloads.size());
WebElement liveReload = liveReloads.get(0);
TestBenchElement 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 @@ -62,20 +62,19 @@ public void splashMessageShownOnAutoReloadAndClosedOnBodyClick() {
By.id(LiveReloadView.JAVA_LIVE_RELOAD_TRIGGER_BUTTON));
liveReloadTrigger.click();

WebElement liveReload = findElement(By.tagName("vaadin-devmode-gizmo"));
TestBenchElement liveReload = $("vaadin-devmode-gizmo").first();
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"));
TestBenchElement liveReload2 = $("vaadin-devmode-gizmo").first();
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 @@ -87,14 +86,13 @@ public void deactivateLiveReload() {

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

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

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

// when: live reload is triggered
Expand All @@ -103,10 +101,9 @@ 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);
TestBenchElement liveReload2 = $("vaadin-devmode-gizmo").first();
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 @@ -15,10 +15,10 @@
*/
package com.vaadin.flow.uitest.ui;

import com.vaadin.testbench.TestBenchElement;
import net.jcip.annotations.NotThreadSafe;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

@NotThreadSafe
Expand All @@ -28,10 +28,10 @@ public class PreserveOnRefreshLiveReloadIT extends AbstractLiveReloadIT {
public void notificationShownWhenLoadingPreserveOnRefreshView() {
open();

WebElement liveReload = findElement(By.tagName("vaadin-devmode-gizmo"));
TestBenchElement liveReload = $("vaadin-devmode-gizmo").first();
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 @@ -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
Loading

0 comments on commit 79fe2a6

Please sign in to comment.