Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,9 @@

/**
* A TestBench element representing a <code>&lt;vaadin-grid-column&gt;</code>
* element. This is not a TestBenchElement as polyfilled browsers are not
* capable of finding it or handling it as a web element.
* element.
*/
public class GridColumnElement {

private GridElement grid;
private Long __generatedId;

public GridColumnElement(Long __generatedId, GridElement grid) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to drop the constructor. It's public, but I have a hard time seeing how someone might have directly used this.

this.grid = grid;
this.__generatedId = __generatedId;
}

/**
* For internal use only.
*
* @return the generated id for the column
*/
protected Long get__generatedId() {
return __generatedId;
}

public class GridColumnElement extends TestBenchElement {
/**
* Gets the header cell for this column.
* <p>
Expand All @@ -49,16 +30,7 @@ protected Long get__generatedId() {
* @return the header cell for the column
*/
public GridTHTDElement getHeaderCell() {
return ((TestBenchElement) execJs("return column._headerCell"))
.wrap(GridTHTDElement.class);
}

private Object execJs(String js) {
return grid.getCommandExecutor()
.executeScript("var grid = arguments[0];" //
+ "var generatedId = arguments[1];"
+ "var column = grid._getColumns().filter(function(column) {return column.__generatedTbId == generatedId;})[0];"
+ js, grid, __generatedId);
return getPropertyElement("_headerCell").wrap(GridTHTDElement.class);
}

/**
Expand All @@ -69,18 +41,6 @@ private Object execJs(String js) {
* @return the footer cell for the column
*/
public GridTHTDElement getFooterCell() {
return ((TestBenchElement) execJs("return column._footerCell"))
.wrap(GridTHTDElement.class);
return getPropertyElement("_footerCell").wrap(GridTHTDElement.class);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof GridColumnElement)) {
return false;
}

return get__generatedId()
.equals(((GridColumnElement) obj).get__generatedId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
Expand Down Expand Up @@ -78,16 +77,10 @@ public void scrollToColumn(int columnIndex) {
* the column to scroll to
*/
public void scrollToColumn(GridColumnElement column) {
executeScript("""
const grid = arguments[0];
const columnId = arguments[1];
const column = grid._getColumns().find((col) => {
return col.__generatedTbId === columnId;
});
if (column) {
grid.scrollToColumn(column);
}
""", this, column.get__generatedId());
if (column == null) {
return;
}
callFunction("scrollToColumn", column);
}

/**
Expand Down Expand Up @@ -305,29 +298,15 @@ public GridTRElement getRow(int rowIndex, boolean scroll)
* given column
*/
public List<GridColumnElement> getAllColumns() {
generatedColumnIdsIfNeeded();
String getVisibleColumnsJS = "return arguments[0]._getColumns().sort(function(a,b) { return a._order - b._order;}).map(function(column) { return column.__generatedTbId;});";
@SuppressWarnings("unchecked")
List<Long> elements = (List<Long>) executeScript(getVisibleColumnsJS,
List<TestBenchElement> columns = (List<TestBenchElement>) executeScript(
"""
const [grid] = arguments;
return grid._getColumns().sort((a, b) => a._order - b._order);
""",
this);
return elements.stream()
.map(generatedId -> new GridColumnElement(generatedId, this))
.collect(Collectors.toList());
}

protected void generatedColumnIdsIfNeeded() {
String generateIds = "const grid = arguments[0];"
+ "if (!grid.__generatedTbId) {"//
+ " grid.__generatedTbId = 1;"//
+ "}" //
+ "grid._getColumns().forEach(function(column) {"
+ " if (!column.__generatedTbId) {"
+ " column.__generatedTbId = grid.__generatedTbId++;" //
+ " }" //
+ "});";

executeScript(generateIds, this);
//
return columns.stream()
.map(element -> element.wrap(GridColumnElement.class)).toList();
}

/**
Expand All @@ -338,13 +317,16 @@ protected void generatedColumnIdsIfNeeded() {
* given column
*/
public List<GridColumnElement> getVisibleColumns() {
generatedColumnIdsIfNeeded();
String getVisibleColumnsJS = "return arguments[0]._getColumns().filter(function(column) {return !column.hidden;}).sort(function(a,b) { return a._order - b._order;}).map(function(column) { return column.__generatedTbId;});";
List<Long> elements = (List<Long>) executeScript(getVisibleColumnsJS,
this);
return elements.stream().map(id -> new GridColumnElement(id, this))
.collect(Collectors.toList());

@SuppressWarnings("unchecked")
List<TestBenchElement> columns = (List<TestBenchElement>) executeScript(
"""
const [grid] = arguments;
return grid._getColumns()
.filter((column) => !column.hidden)
.sort((a, b) => a._order - b._order);
""", this);
return columns.stream()
.map(element -> element.wrap(GridColumnElement.class)).toList();
}

/**
Expand Down Expand Up @@ -556,13 +538,16 @@ private void removeActiveItem(GridTRElement row) {
* @return the multi-select column, or null
*/
private GridColumnElement getMultiSelectColumn() {
generatedColumnIdsIfNeeded();
List<Long> columnIds = (List<Long>) executeScript(
"return arguments[0]._getColumns().filter(function(col) { return typeof col.selectAll != 'undefined';}).map(function(column) { return column.__generatedTbId;});",
TestBenchElement column = (TestBenchElement) executeScript(
"""
const [grid] = arguments;
return grid._getColumns().find((column) => column.selectAll !== undefined) ?? null;
""",
this);
if (columnIds.isEmpty())
if (column == null) {
return null;
return new GridColumnElement(columnIds.get(0), this);
}
return column.wrap(GridColumnElement.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,12 @@ public int getRow() {
* @return the column element
*/
public GridColumnElement getColumn() {
Double id = getPropertyDouble("_column", "__generatedTbId");
GridElement grid = getGrid();
if (id == null) {
grid.generatedColumnIdsIfNeeded();
id = getPropertyDouble("_column", "__generatedTbId");
}
if (id == null) {
TestBenchElement column = getPropertyElement("_column");
if (column == null) {
throw new NoSuchElementException(
"Unable to find column. This should not really happen.");
}
return new GridColumnElement(id.longValue(), grid);
return column.wrap(GridColumnElement.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.vaadin.flow.component.grid.testbench;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.vaadin.testbench.TestBenchElement;
Expand Down Expand Up @@ -47,20 +45,15 @@ public GridTHTDElement getCell(GridColumnElement column) {
* columns
*/
public List<GridTHTDElement> getCells(GridColumnElement... columns) {
Object cells = executeScript("const row = arguments[0];" //
+ "const columnIds = arguments[1];"
+ "return Array.from(row.children)."
+ "filter(function(cell) { return cell._column && columnIds.includes(cell._column.__generatedTbId);})",
this, Arrays.stream(columns)
.map(GridColumnElement::get__generatedId).toArray());
if (cells != null) {
return ((ArrayList<?>) cells).stream()
.map(elem -> ((TestBenchElement) elem)
.wrap(GridTHTDElement.class))
.toList();
} else {
return new ArrayList<>();
}
@SuppressWarnings("unchecked")
List<TestBenchElement> cells = (List<TestBenchElement>) executeScript(
"""
const [row, columns] = arguments;
return Array.from(row.children)
.filter((cell) => columns.includes(cell._column));
""", this, columns);
return cells.stream().map(cell -> cell.wrap(GridTHTDElement.class))
.toList();
}

/**
Expand Down
Loading