Skip to content

Commit

Permalink
test: Remove unnecessary shadow root helpers (#12573) (CP: 8.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- committed Dec 15, 2021
1 parent 5b43ce0 commit 54abf05
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 136 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 @@ -22,6 +22,7 @@

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

import static com.vaadin.flow.navigate.HelloWorldView.NAVIGATE_ABOUT;

Expand All @@ -44,8 +45,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 @@ -58,8 +60,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 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 @@ -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 @@ -212,8 +212,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 @@ -22,6 +22,8 @@
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.TestBenchElement;

@NotThreadSafe
public class FrontendLiveReloadIT extends AbstractLiveReloadIT {

Expand Down Expand Up @@ -53,10 +55,9 @@ public void liveReloadOnTouchedFrontendFile() {
waitForLiveReload();

// 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);
TestBenchElement customComponent = $("*")
.id(FrontendLiveReloadView.CUSTOM_COMPONENT);
TestBenchElement 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 @@ -17,6 +17,8 @@

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,26 +34,22 @@ 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"));
Assert.assertEquals(1, liveReloads.size());
WebElement liveReload = liveReloads.get(0);
TestBenchElement liveReload = $("vaadin-devmode-gizmo").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 @@ -65,19 +63,18 @@ public void splashMessageShownOnAutoReloadAndClosedOnBodyClick() {

waitForLiveReload();

WebElement liveReload = findElement(By.tagName("vaadin-devmode-gizmo"));
Assert.assertNotNull(liveReload);
WebElement gizmo1 = findInShadowRoot(liveReload, By.className("gizmo"))
.get(0);
TestBenchElement liveReload = $("vaadin-devmode-gizmo").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"));
TestBenchElement liveReload2 = $("vaadin-devmode-gizmo").waitForFirst();
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,8 +84,7 @@ public void deactivateLiveReload() {
open();

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

WebElement liveReloadIcon = findInShadowRoot(liveReload,
By.className("gizmo")).get(0);
Expand All @@ -104,10 +100,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").waitForFirst();
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 @@ -16,6 +16,9 @@
package com.vaadin.flow.uitest.ui;

import net.jcip.annotations.NotThreadSafe;

import com.vaadin.testbench.TestBenchElement;

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);
TestBenchElement liveReload = $("vaadin-devmode-gizmo").waitForFirst();
WebElement messageDetails = liveReload.$("*")
.attributeContains("class", "warning").first();
Assert.assertTrue(
messageDetails.getText().contains("@PreserveOnRefresh"));
}
Expand Down
Loading

0 comments on commit 54abf05

Please sign in to comment.