Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getTableData method to html Table component #2257

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all 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
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