Skip to content

Commit f860cbd

Browse files
vaadin-botweb-padawanclaude
authored
feat: add Breadcrumbs TestBench element classes (#9530) (CP: 25.2) (#9540)
This PR cherry-picks changes from the original PR #9530 to branch 25.2. --- #### Original PR description > ## Description > > Task 8 of the Breadcrumbs SDD. > > Added TestBench element classes. > > ## Type of change > > - Feature > > ## Note > > The first commit is intentionally fully generated by Claude without any modifications on my part. > > It contains questionable APIs like `getOverflowOverlay()`. I will fix any suggestions in follow up commits. Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent e5b821d commit f860cbd

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

‎vaadin-breadcrumbs-flow-parent/vaadin-breadcrumbs-testbench/src/main/java/com/vaadin/flow/component/breadcrumbs/testbench/BreadcrumbsElement.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.vaadin.flow.component.breadcrumbs.testbench;
1717

18+
import java.util.List;
19+
1820
import com.vaadin.testbench.TestBenchElement;
1921
import com.vaadin.testbench.elementsbase.Element;
2022

@@ -24,4 +26,47 @@
2426
*/
2527
@Element("vaadin-breadcrumbs")
2628
public class BreadcrumbsElement extends TestBenchElement {
29+
30+
/**
31+
* Gets all breadcrumb items in the trail.
32+
*
33+
* @return list of all breadcrumb item elements
34+
*/
35+
public List<BreadcrumbsItemElement> getItems() {
36+
return $(BreadcrumbsItemElement.class).all();
37+
}
38+
39+
/**
40+
* Gets the item that represents the current page.
41+
*
42+
* @return the current breadcrumb item, or {@code null} if none
43+
*/
44+
public BreadcrumbsItemElement getCurrentItem() {
45+
return getItems().stream().filter(BreadcrumbsItemElement::isCurrent)
46+
.findFirst().orElse(null);
47+
}
48+
49+
/**
50+
* Gets the breadcrumb item whose visible text matches the given string.
51+
*
52+
* @param text
53+
* the text to search for
54+
* @return the matching breadcrumb item, or {@code null} if not found
55+
*/
56+
public BreadcrumbsItemElement getItemByText(String text) {
57+
return getItems().stream().filter(item -> text.equals(item.getText()))
58+
.findFirst().orElse(null);
59+
}
60+
61+
/**
62+
* Gets the breadcrumb item whose path matches the given path.
63+
*
64+
* @param path
65+
* the path to search for
66+
* @return the matching breadcrumb item, or {@code null} if not found
67+
*/
68+
public BreadcrumbsItemElement getItemByPath(String path) {
69+
return getItems().stream().filter(item -> path.equals(item.getPath()))
70+
.findFirst().orElse(null);
71+
}
2772
}

‎vaadin-breadcrumbs-flow-parent/vaadin-breadcrumbs-testbench/src/main/java/com/vaadin/flow/component/breadcrumbs/testbench/BreadcrumbsItemElement.java‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package com.vaadin.flow.component.breadcrumbs.testbench;
1717

18+
import org.openqa.selenium.By;
19+
import org.openqa.selenium.NoSuchElementException;
20+
import org.openqa.selenium.WebElement;
21+
22+
import com.vaadin.testbench.ElementQuery;
1823
import com.vaadin.testbench.TestBenchElement;
1924
import com.vaadin.testbench.elementsbase.Element;
2025

@@ -24,4 +29,77 @@
2429
*/
2530
@Element("vaadin-breadcrumbs-item")
2631
public class BreadcrumbsItemElement extends TestBenchElement {
32+
33+
/**
34+
* Gets the visible text of this breadcrumb item.
35+
*
36+
* @return the item's text
37+
*/
38+
@Override
39+
public String getText() {
40+
// The default implementation uses innerText, which adds a lot of
41+
// whitespace in some browsers.
42+
return getPropertyString("textContent");
43+
}
44+
45+
/**
46+
* Gets the path this breadcrumb item links to.
47+
*
48+
* @return the path, or {@code null} for the current item, which has no path
49+
*/
50+
public String getPath() {
51+
return getDomAttribute("path");
52+
}
53+
54+
/**
55+
* Returns {@code true} if this item represents the current page.
56+
*
57+
* @return {@code true} if this is the current item, {@code false} otherwise
58+
*/
59+
public boolean isCurrent() {
60+
return hasAttribute("current");
61+
}
62+
63+
/**
64+
* Returns {@code true} if this item has a prefix component.
65+
*
66+
* @return {@code true} if a prefix component is present, {@code false}
67+
* otherwise
68+
*/
69+
public boolean hasPrefix() {
70+
return hasAttribute("has-prefix");
71+
}
72+
73+
/**
74+
* Gets the prefix component of this breadcrumb item.
75+
*
76+
* @return the prefix component, or {@code null} if none is set
77+
*/
78+
public TestBenchElement getPrefixComponent() {
79+
ElementQuery<TestBenchElement> prefix = $("*").withAttribute("slot",
80+
"prefix");
81+
return prefix.exists() ? prefix.single() : null;
82+
}
83+
84+
/**
85+
* Navigates by activating this breadcrumb item's link.
86+
*
87+
* @throws NoSuchElementException
88+
* if this item has no link, such as the current item
89+
*/
90+
public void navigate() {
91+
WebElement anchor;
92+
try {
93+
anchor = getWrappedElement().getShadowRoot()
94+
.findElement(By.cssSelector("a"));
95+
} catch (NoSuchElementException e) {
96+
throw new NoSuchElementException("Item does not contain an anchor",
97+
e);
98+
}
99+
// click() on elements in shadow DOM does not work with Chrome driver.
100+
// Using executeScript also navigates items that are collapsed into the
101+
// overflow overlay, which are not visible on the page when the overflow
102+
// is not opened.
103+
executeScript("arguments[0].click();", anchor);
104+
}
27105
}

0 commit comments

Comments
 (0)