Skip to content

Commit

Permalink
Add getTableData method to html Table component
Browse files Browse the repository at this point in the history
  • Loading branch information
labkey-tchad committed Feb 4, 2025
1 parent 2bd83d5 commit e797b06
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/org/labkey/test/components/html/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Table.Elements>
{
Expand Down Expand Up @@ -62,7 +64,7 @@ protected Elements newElementCache()
return new Elements();
}

protected class Elements extends Component.ElementCache
protected class Elements extends Component<?>.ElementCache
{
List<WebElement> rows;

Expand All @@ -86,15 +88,15 @@ public int getRowCount()
*/
public List<String> getTableHeaderTexts()
{
List<WebElement> headerEls = Locator.xpath("//thead//th").findElements(this);
List<WebElement> headerEls = Locator.xpath("./thead/tr[1]/th").findElements(this);
List<String> columnHeaders = new ArrayList<>();
for(WebElement headerEl : headerEls){columnHeaders.add(headerEl.getText());}
return columnHeaders;
}

public int getTableHeaderIndex(String headerText)
{
List<WebElement> headerEls = Locator.xpath("//thead//th").findElements(this);
List<WebElement> headerEls = Locator.xpath("./thead/tr[1]/th").findElements(this);
int counter = 1;
for(WebElement headerEl : headerEls)
{
Expand All @@ -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.<br>
* Assumes a simple table with a single header row with no colspans and unique header labels
* @return table data
*/
public List<Map<String, String>> getTableData()
{
List<Map<String, String>> data = new ArrayList<>();

List<String> headerTexts = getTableHeaderTexts();
List<WebElement> rows = elementCache().getRows();

for (WebElement row : rows)
{
List<String> 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<String, String> 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<String> getTableHeaderColumnData(String headerText)
{
List<String> columnData = new ArrayList<>();
Expand Down

0 comments on commit e797b06

Please sign in to comment.