Skip to content

Commit

Permalink
Added Date(Time)PickerPw and cleanup
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
mstahv committed Nov 22, 2023
1 parent b055f07 commit 82446b0
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 82 deletions.
10 changes: 10 additions & 0 deletions src/main/java/in/virit/mopo/ComboBoxPw.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

/**
* Helper class to work with vaadin-combo-box component
*/
public class ComboBoxPw {

private final Locator root;
Expand All @@ -28,7 +31,14 @@ public void filterAndSelectFirst(String filter) {
root.locator("input").press("Enter");
}

/**
* Fills given filter to the combobox.
*
* @param filter the filter string to be set
* @return the {@link ComboBoxPw} (for fluent API)
*/
public ComboBoxPw filter(String filter) {
root.locator("input").clear();
root.locator("input").fill(filter);
return this;
}
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/in/virit/mopo/DatePickerPw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package in.virit.mopo;

import com.microsoft.playwright.Locator;

import java.time.LocalDate;

/**
* A helper class to work with vaadin-date-picker component.
*/
public class DatePickerPw {

private final Locator root;

/**
* Creates a DatePicker page object for the given locator.
*
* @param gridLocator the Playwright locator for the vaadin-date-picker to
* be interacted with
*/
public DatePickerPw(Locator gridLocator) {
this.root = gridLocator;
}

/**
* Returns the value from the client side and parses it as
* {@link LocalDate}.
*
* @return the current value of the field
*/
public LocalDate getValue() {
String str = (String) root.evaluate("db => db.value");
try {
return LocalDate.parse(str);
} catch (java.time.format.DateTimeParseException e) {
return null;
}
}

/**
* Sets the value of the field.
*
* @param value the value to be set
*/
public void setValue(LocalDate value) {
root.evaluate("db => db.value = '%s'".formatted(value));
}

/**
* Returns the raw string value in the field.
*
* @return the string value as it is formatted in the field. Note, this may
* be locale dependent.
*/
public String getInputString() {
return root.locator("input").inputValue();
}
}
67 changes: 67 additions & 0 deletions src/main/java/in/virit/mopo/DateTimePickerPw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package in.virit.mopo;

import com.microsoft.playwright.Locator;

import java.time.LocalDateTime;

/**
* A helper class to work with vaadin-date-time-picker
*/
public class DateTimePickerPw {

private final Locator root;

/**
* Creates a DateTimePicker page object for the given locator.
*
* @param gridLocator the Playwright locator for the vaadin-date-time-picker
* to be interacted with
*/
public DateTimePickerPw(Locator gridLocator) {
this.root = gridLocator;
}

/**
* Sets the current value of this field
*
* @param value the value to be set
*/
public void setValue(LocalDateTime value) {
root.evaluate("db => db.value = '%s'".formatted(value));
}

/**
* Gets the currently set value of the field and parses it as
* {@link LocalDateTime}.
*
* @return the current value of the field
*/
public LocalDateTime getValue() {
String str = (String) root.evaluate("db => db.value");
try {
return LocalDateTime.parse(str);
} catch (java.time.format.DateTimeParseException e) {
return null;
}
}

/**
* Gets the string currently in the input field defining the date part.
*
* @return the string value as it is formatted in the field. Note, this may
* be locale dependent.
*/
public String getDateInputString() {
return root.locator("vaadin-date-picker input").inputValue();
}

/**
* Returns the string currently in the input defining the time part
*
* @return the string value as it is formatted in the field. Note, this may
* be locale dependent.
*/
public String getTimeInputString() {
return root.locator("vaadin-time-picker input").inputValue();
}
}
146 changes: 89 additions & 57 deletions src/main/java/in/virit/mopo/GridPw.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,16 @@
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;

/**
* A helper class to work with the vaadin-grid component.
*/
public class GridPw {

public class RowPw {
private final int rowIndex;

private RowPw(int rowIndex) {
this.rowIndex = rowIndex;
}

/**
* Gets the cell locator at the given index.
*
* @param cellIndex the cell index (0-based, unlike the CSS nth-child selector, whose designer should be hung by the balls, in case they have any)
* @return the cell locator
*/
public Locator getCell(int cellIndex) {
int indexInVirtualTable = (Integer) root.evaluate("g => g._getRenderedRows().indexOf(g._getRenderedRows().filter(r => r.index == %s)[0]);".formatted(rowIndex));
indexInVirtualTable += 1; // 1-based :-)
String name = root.locator("#items tr:nth-child(%s) td:nth-child(%s) slot".formatted(indexInVirtualTable, cellIndex+1))
.getAttribute("name");
return root.locator("vaadin-grid-cell-content[slot='%s']".formatted(name));
}

/**
* Gets the cell with the given header text.
* @param headerText the header text
* @return the cell locator
*/
public Locator getCell(String headerText) {
// this depends heavily on Grid's internal implementation
// Grid developers probably have a better way to do this
String slot = root.locator("vaadin-grid-cell-content")
.filter(new Locator.FilterOptions().setHasText(headerText))
.getAttribute("slot");
String substring = slot.substring(slot.lastIndexOf("-") + 1);
int cellIndex = Integer.parseInt(substring);
return getCell(cellIndex);
}

public void select() {
GridPw.this.selectRow(rowIndex);
}
}

private final Locator root;

/**
* Creates a Grid page object for the given grid locator.
*
* @param gridLocator the Playwright locator for the grid
*/
public GridPw(Locator gridLocator) {
Expand All @@ -59,6 +21,7 @@ public GridPw(Locator gridLocator) {

/**
* Creates a Grid page object for the first grid on the page.
*
* @param page the Playwright page
*/
public GridPw(Page page) {
Expand All @@ -75,6 +38,11 @@ public int getRenderedRowCount() {
return evaluate;
}

/**
* Returns the index of the first visible row
*
* @return the index.
*/
public int getFirstVisibleRowIndex() {
return (Integer) root.elementHandle().evaluate("e => e._firstVisibleIndex");
}
Expand Down Expand Up @@ -131,7 +99,7 @@ public void scrollToIndex(int index) {
}, 100);
});
}""");
// System.out.println("RETURN value = " + value);
// System.out.println("RETURN value = " + value);
}

/**
Expand All @@ -141,20 +109,27 @@ public void scrollToIndex(int index) {
*/
public void selectRow(int rowIndex) {
String script = """
grid => {
var firstRowIndex = %s;
var lastRowIndex = firstRowIndex;
var rowsInDom = grid._getRenderedRows();
var rows = Array.from(rowsInDom).filter((row) => { return row.index >= firstRowIndex && row.index <= lastRowIndex;});
var row = rows[0];
grid.activeItem = row._item;
}
""".formatted(rowIndex);
grid => {
var firstRowIndex = %s;
var lastRowIndex = firstRowIndex;
var rowsInDom = grid._getRenderedRows();
var rows = Array.from(rowsInDom).filter((row) => { return row.index >= firstRowIndex && row.index <= lastRowIndex;});
var row = rows[0];
grid.activeItem = row._item;
}
""".formatted(rowIndex);
root.elementHandle().evaluate(script);
}

public RowPw getTableRow(int rowIndex) {
if(!isRowInView(rowIndex)) {
/**
* Returns a RowPw helper representing the row defined by the given index.
*
* @param rowIndex the row index
* @return the RowPw for editing the UI state or to get cell locators for
* assertions.
*/
public RowPw getRow(int rowIndex) {
if (!isRowInView(rowIndex)) {
scrollToIndex(rowIndex);
}
return new RowPw(rowIndex);
Expand All @@ -163,19 +138,76 @@ public RowPw getTableRow(int rowIndex) {
/**
* Checks if the given row is in the visible viewport.
*
* @param rowIndex
* the row to check
* @param rowIndex the row to check
* @return <code>true</code> if the row is at least partially in view,
* <code>false</code> otherwise
* <code>false</code> otherwise
*/
public boolean isRowInView(int rowIndex) {
return (getFirstVisibleRowIndex() <= rowIndex
&& rowIndex <= getLastVisibleRowIndex());
}

/**
* Returns the index of last visible row.
*
* @return the index
*/
public int getLastVisibleRowIndex() {
return (Integer) root.elementHandle().evaluate("e => e._lastVisibleIndex");
}

/**
* Represents a row in the vaadin-grid component. Not that there is no DOM
* element backing this row, but this is purely virtual helper class based
* on row index.
*/
public class RowPw {

private final int rowIndex;

private RowPw(int rowIndex) {
this.rowIndex = rowIndex;
}

/**
* Gets the cell locator at the given index.
*
* @param cellIndex the cell index (0-based, unlike the CSS nth-child
* selector, whose designer should be hung by the balls, in case they
* have any)
* @return the cell locator
*/
public Locator getCell(int cellIndex) {
int indexInVirtualTable = (Integer) root.evaluate("g => g._getRenderedRows().indexOf(g._getRenderedRows().filter(r => r.index == %s)[0]);".formatted(rowIndex));
indexInVirtualTable += 1; // 1-based :-)
String name = root.locator("#items tr:nth-child(%s) td:nth-child(%s) slot".formatted(indexInVirtualTable, cellIndex + 1))
.getAttribute("name");
return root.locator("vaadin-grid-cell-content[slot='%s']".formatted(name));
}

/**
* Gets the cell with the given header text.
*
* @param headerText the header text
* @return the cell locator
*/
public Locator getCell(String headerText) {
// this depends heavily on Grid's internal implementation
// Grid developers probably have a better way to do this
String slot = root.locator("vaadin-grid-cell-content")
.filter(new Locator.FilterOptions().setHasText(headerText))
.getAttribute("slot");
String substring = slot.substring(slot.lastIndexOf("-") + 1);
int cellIndex = Integer.parseInt(substring);
return getCell(cellIndex);
}

/**
* Selects the given row.
*/
public void select() {
GridPw.this.selectRow(rowIndex);
}
}

}
Loading

0 comments on commit 82446b0

Please sign in to comment.