Skip to content

Commit 7b397ed

Browse files
JuaniVegaAuDuarte
authored andcommitted
refactor: update GridPw
Add the getGridRows method. Apply formatting to the code.
1 parent 887bcf2 commit 7b397ed

File tree

1 file changed

+95
-62
lines changed

1 file changed

+95
-62
lines changed

src/main/java/in/virit/mopo/GridPw.java

Lines changed: 95 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.microsoft.playwright.Locator;
44
import com.microsoft.playwright.Page;
5+
import java.util.ArrayList;
6+
import java.util.List;
57

68
/**
79
* A helper class to work with the vaadin-grid component.
@@ -13,7 +15,8 @@ public class GridPw {
1315
/**
1416
* Creates a Grid page object for the given grid locator.
1517
*
16-
* @param gridLocator the Playwright locator for the grid
18+
* @param gridLocator
19+
* the Playwright locator for the grid
1720
*/
1821
public GridPw(Locator gridLocator) {
1922
this.root = gridLocator;
@@ -22,7 +25,8 @@ public GridPw(Locator gridLocator) {
2225
/**
2326
* Creates a Grid page object for the first grid on the page.
2427
*
25-
* @param page the Playwright page
28+
* @param page
29+
* the Playwright page
2630
*/
2731
public GridPw(Page page) {
2832
this(page.locator("vaadin-grid"));
@@ -34,78 +38,95 @@ public GridPw(Page page) {
3438
* @return the number of rows
3539
*/
3640
public int getRenderedRowCount() {
37-
Integer evaluate = (Integer) root.elementHandle().evaluate("e => e._getRenderedRows().length");
41+
Integer evaluate = (Integer) root.elementHandle()
42+
.evaluate("e => e._getRenderedRows().length");
3843
return evaluate;
3944
}
4045

46+
/**
47+
* Returns a list of RowPw objects representing the grid rows.
48+
*
49+
* @return A List of RowPw objects representing the grid rows.
50+
*/
51+
public List<RowPw> getGridRows() {
52+
List<RowPw> allRows = new ArrayList<>();
53+
for (int i = 0; i < getRenderedRowCount(); i++) {
54+
allRows.add(getRow(i));
55+
}
56+
return allRows;
57+
}
58+
4159
/**
4260
* Returns the index of the first visible row
4361
*
4462
* @return the index.
4563
*/
4664
public int getFirstVisibleRowIndex() {
47-
return (Integer) root.elementHandle().evaluate("e => e._firstVisibleIndex");
65+
return (Integer) root.elementHandle()
66+
.evaluate("e => e._firstVisibleIndex");
4867
}
4968

5069
/**
5170
* Scrolls the grid to the given index.
5271
*
53-
* @param index the row index to scroll to
72+
* @param index
73+
* the row index to scroll to
5474
*/
5575
public void scrollToIndex(int index) {
5676
root.elementHandle().evaluate("e => e.scrollToIndex(" + index + ")");
5777

5878
// FIXME this don't seem to be stable, but
5979
// 10*100ms timeout seems to do the trick most of the time
60-
int value = (Integer) root.evaluate("""
61-
g => {
62-
return new Promise((resolve, reject) => {
63-
var x = 0;
64-
const isDoneLoading = () => {
65-
return !g.$connector.hasRootRequestQueue()
66-
};
67-
68-
const isVaadinConnectionActive = () => {
69-
if (window.Vaadin && window.Vaadin.Flow && window.Vaadin.Flow.clients) {
70-
var clients = window.Vaadin.Flow.clients;
71-
for (var client in clients) {
72-
if (clients[client].isActive()) {
73-
return false;
80+
int value = (Integer) root.evaluate(
81+
"""
82+
g => {
83+
return new Promise((resolve, reject) => {
84+
var x = 0;
85+
const isDoneLoading = () => {
86+
return !g.$connector.hasRootRequestQueue()
87+
};
88+
89+
const isVaadinConnectionActive = () => {
90+
if (window.Vaadin && window.Vaadin.Flow && window.Vaadin.Flow.clients) {
91+
var clients = window.Vaadin.Flow.clients;
92+
for (var client in clients) {
93+
if (clients[client].isActive()) {
94+
return false;
95+
}
96+
}
97+
return true;
98+
} else if (window.Vaadin && window.Vaadin.Flow && window.Vaadin.Flow.devServerIsNotLoaded) {
99+
return false;
100+
} else {
101+
return true;
102+
}
103+
};
104+
105+
if (isDoneLoading() && !isVaadinConnectionActive()) {
106+
resolve(x);
107+
return;
74108
}
75-
}
76-
return true;
77-
} else if (window.Vaadin && window.Vaadin.Flow && window.Vaadin.Flow.devServerIsNotLoaded) {
78-
return false;
79-
} else {
80-
return true;
81-
}
82-
};
83-
84-
if (isDoneLoading() && !isVaadinConnectionActive()) {
85-
resolve(x);
86-
return;
87-
}
88-
89-
var intervalID = window.setInterval(function () {
90-
if (isDoneLoading() && !isVaadinConnectionActive()) {
91-
window.clearInterval(intervalID);
92-
resolve(x+1);
93-
} else {
94-
if (++x === 10) {
95-
window.clearInterval(intervalID);
96-
resolve(-1);
97-
}
98-
}
99-
}, 100);
100-
});
101-
}""");
102-
// System.out.println("RETURN value = " + value);
109+
110+
var intervalID = window.setInterval(function () {
111+
if (isDoneLoading() && !isVaadinConnectionActive()) {
112+
window.clearInterval(intervalID);
113+
resolve(x+1);
114+
} else {
115+
if (++x === 10) {
116+
window.clearInterval(intervalID);
117+
resolve(-1);
118+
}
119+
}
120+
}, 100);
121+
});
122+
}""");
103123
}
104124

105125
/**
106126
* Selects the given row.
107127
*
108-
* @param rowIndex the row index to select
128+
* @param rowIndex
129+
* the row index to select
109130
*/
110131
public void selectRow(int rowIndex) {
111132
String script = """
@@ -117,16 +138,18 @@ public void selectRow(int rowIndex) {
117138
var row = rows[0];
118139
grid.activeItem = row._item;
119140
}
120-
""".formatted(rowIndex);
141+
"""
142+
.formatted(rowIndex);
121143
root.elementHandle().evaluate(script);
122144
}
123145

124146
/**
125147
* Returns a RowPw helper representing the row defined by the given index.
126148
*
127-
* @param rowIndex the row index
149+
* @param rowIndex
150+
* the row index
128151
* @return the RowPw for editing the UI state or to get cell locators for
129-
* assertions.
152+
* assertions.
130153
*/
131154
public RowPw getRow(int rowIndex) {
132155
if (!isRowInView(rowIndex)) {
@@ -138,9 +161,10 @@ public RowPw getRow(int rowIndex) {
138161
/**
139162
* Checks if the given row is in the visible viewport.
140163
*
141-
* @param rowIndex the row to check
164+
* @param rowIndex
165+
* the row to check
142166
* @return <code>true</code> if the row is at least partially in view,
143-
* <code>false</code> otherwise
167+
* <code>false</code> otherwise
144168
*/
145169
public boolean isRowInView(int rowIndex) {
146170
return (getFirstVisibleRowIndex() <= rowIndex
@@ -153,7 +177,8 @@ public boolean isRowInView(int rowIndex) {
153177
* @return the index
154178
*/
155179
public int getLastVisibleRowIndex() {
156-
return (Integer) root.elementHandle().evaluate("e => e._lastVisibleIndex");
180+
return (Integer) root.elementHandle()
181+
.evaluate("e => e._lastVisibleIndex");
157182
}
158183

159184
/**
@@ -172,22 +197,30 @@ private RowPw(int rowIndex) {
172197
/**
173198
* Gets the cell locator at the given index.
174199
*
175-
* @param cellIndex the cell index (0-based, unlike the CSS nth-child
176-
* selector, whose designer should be hung by the balls, in case they
177-
* have any)
200+
* @param cellIndex
201+
* the cell index (0-based, unlike the CSS nth-child
202+
* selector, whose designer should be hung by the balls, in
203+
* case they have any)
178204
* @return the cell locator
179205
*/
180206
public Locator getCell(int cellIndex) {
181-
int indexInVirtualTable = (Integer) root.evaluate("g => g._getRenderedRows().indexOf(g._getRenderedRows().filter(r => r.index == %s)[0]);".formatted(rowIndex));
182-
Locator row = root.locator("#items tr").filter(new Locator.FilterOptions().setVisible(true)).nth(indexInVirtualTable);
183-
String name = row.locator("td:nth-child(%s) slot".formatted(cellIndex + 1)).getAttribute("name");
184-
return root.locator("vaadin-grid-cell-content[slot='%s']".formatted(name));
207+
int indexInVirtualTable = (Integer) root.evaluate(
208+
"g => g._getRenderedRows().indexOf(g._getRenderedRows().filter(r => r.index == %s)[0]);"
209+
.formatted(rowIndex));
210+
indexInVirtualTable += 1; // 1-based :-)
211+
String name = root
212+
.locator("#items tr:nth-child(%s) td:nth-child(%s) slot"
213+
.formatted(indexInVirtualTable, cellIndex + 1))
214+
.getAttribute("name");
215+
return root.locator(
216+
"vaadin-grid-cell-content[slot='%s']".formatted(name));
185217
}
186218

187219
/**
188220
* Gets the cell with the given header text.
189221
*
190-
* @param headerText the header text
222+
* @param headerText
223+
* the header text
191224
* @return the cell locator
192225
*/
193226
public Locator getCell(String headerText) {

0 commit comments

Comments
 (0)