diff --git a/src/org/labkey/test/components/html/Table.java b/src/org/labkey/test/components/html/Table.java index ddfc74e2f8..b7dfdfaf3c 100644 --- a/src/org/labkey/test/components/html/Table.java +++ b/src/org/labkey/test/components/html/Table.java @@ -26,7 +26,9 @@ import org.openqa.selenium.WebElement; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; public class Table extends WebDriverComponent { @@ -62,7 +64,7 @@ protected Elements newElementCache() return new Elements(); } - protected class Elements extends Component.ElementCache + protected class Elements extends Component.ElementCache { List rows; @@ -86,7 +88,7 @@ public int getRowCount() */ public List getTableHeaderTexts() { - List headerEls = Locator.xpath("//thead//th").findElements(this); + List headerEls = Locator.xpath("./thead/tr[1]/th").findElements(this); List columnHeaders = new ArrayList<>(); for(WebElement headerEl : headerEls){columnHeaders.add(headerEl.getText());} return columnHeaders; @@ -94,7 +96,7 @@ public List getTableHeaderTexts() public int getTableHeaderIndex(String headerText) { - List headerEls = Locator.xpath("//thead//th").findElements(this); + List headerEls = Locator.xpath("./thead/tr[1]/th").findElements(this); int counter = 1; for(WebElement headerEl : headerEls) { @@ -105,6 +107,36 @@ public int getTableHeaderIndex(String headerText) throw new RuntimeException( headerText + " column not found"); } + /** + * Get table data as a list of maps. Each map represents a row.
+ * Assumes a simple table with a single header row with no colspans and unique header labels + * @return table data + */ + public List> getTableData() + { + List> data = new ArrayList<>(); + + List headerTexts = getTableHeaderTexts(); + List rows = elementCache().getRows(); + + for (WebElement row : rows) + { + List dataTexts = getWrapper().getTexts(Locator.tag("td").findElements(row)); + if (headerTexts.size() != dataTexts.size()) + { + throw new IllegalStateException("Size of row %s doesn't match table header %s".formatted(dataTexts, headerTexts)); + } + Map rowMap = new LinkedHashMap<>(); + for (int i = 0; i < headerTexts.size(); i++) + { + rowMap.put(headerTexts.get(i), dataTexts.get(i)); + } + data.add(rowMap); + } + + return data; + } + public List getTableHeaderColumnData(String headerText) { List columnData = new ArrayList<>();