Skip to content

Commit 3853205

Browse files
browser: tableEl.extractData (#1521)
1 parent 2ec2bd4 commit 3853205

10 files changed

Lines changed: 209 additions & 13 deletions

File tree

webtau-browser/src/main/java/org/testingisdocumenting/webtau/browser/page/PageElement.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.testingisdocumenting.webtau.reporter.WebTauStepInputKeyValue;
4848

4949
import java.util.*;
50+
import java.util.function.Consumer;
5051
import java.util.function.Function;
5152
import java.util.function.Supplier;
5253
import java.util.regex.Pattern;
@@ -127,6 +128,13 @@ public TokenizedMessage describe() {
127128
return pathDescription;
128129
}
129130

131+
public void forEach(Consumer<PageElement> consumer) {
132+
Integer count = extractNumberOfElements();
133+
for (int idx = 0; idx < count; idx++) {
134+
consumer.accept(get(idx));
135+
}
136+
}
137+
130138
public void highlight() {
131139
additionalBrowserInteractions.flashWebElements(findElements());
132140
}
@@ -410,7 +418,7 @@ private List<Object> extractListOfValues() {
410418
return result;
411419
}
412420

413-
private Integer extractNumberOfElements() {
421+
Integer extractNumberOfElements() {
414422
return getValueForStaleElement(() -> {
415423
List<WebElement> webElements = path.find(driver);
416424
return webElements.size();

webtau-browser/src/main/java/org/testingisdocumenting/webtau/browser/page/TablePageElement.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
2828

2929
import java.util.List;
30+
import java.util.function.Consumer;
3031
import java.util.stream.Stream;
3132

3233
import static org.testingisdocumenting.webtau.WebTauCore.*;
@@ -60,6 +61,17 @@ public boolean isVisible() {
6061
return root.isVisible();
6162
}
6263

64+
public void forEach(Consumer<TablePageElement> consumer) {
65+
Integer count = root.extractNumberOfElements();
66+
for (int idx = 0; idx < count; idx++) {
67+
consumer.accept(new TablePageElement(root.get(idx)));
68+
}
69+
}
70+
71+
/**
72+
* extracts table data from first matching browser table
73+
* @return extracted TableData
74+
*/
6375
public TableData extractTableData() {
6476
List<HtmlNode> htmlNodes = root.extractHtmlNodes();
6577
if (htmlNodes.isEmpty()) {
@@ -69,6 +81,23 @@ public TableData extractTableData() {
6981
return BrowserTableParser.parse(htmlNodes.stream().findFirst().get().outerHtml());
7082
}
7183

84+
/**
85+
* extracts and combines TableData from all tables matching selector
86+
* @return extracted TableData
87+
*/
88+
public TableData extractAndMergeTableData() {
89+
TableData result = extractTableData();
90+
91+
Integer count = root.extractNumberOfElements();
92+
for (int idx = 1; idx < count; idx++) {
93+
TablePageElement tablePageElement = new TablePageElement(root.get(idx + 1));
94+
TableData extracted = tablePageElement.extractTableData();
95+
result.addRowsExistingColumnsOnly(extracted);
96+
}
97+
98+
return result;
99+
}
100+
72101
@Override
73102
public StepReportOptions shouldReportOption() {
74103
return StepReportOptions.REPORT_ALL;

webtau-docs/znai/browser/tables.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,42 @@ Java:
2121
}
2222
```
2323

24+
# Extracting Table Data
25+
26+
Use `:identifier: extractTableData` to extract `TableData` for further processing, e.g. saving to a disk:
27+
28+
```tabs
29+
Groovy:
30+
:include-file: examples/scenarios/ui/tables.groovy {
31+
title: "table data extraction",
32+
surroundedBy: "extract-single-table-data"
33+
}
34+
35+
Java:
36+
:include-file: com/example/tests/junit5/BrowserTablesJavaTest.java {
37+
title: "table data extraction",
38+
surroundedBy: "extract-single-table-data"
39+
}
40+
```
41+
42+
Use `:identifier: extractAndMergeTableData` to extract `TableData` from multiple tables matching the selector:
43+
44+
```tabs
45+
Groovy:
46+
:include-file: examples/scenarios/ui/tables.groovy {
47+
title: "multiple tables data extraction",
48+
surroundedBy: "extract-all-table-data"
49+
}
50+
51+
Java:
52+
:include-file: com/example/tests/junit5/BrowserTablesJavaTest.java {
53+
title: "multiple tables data extraction",
54+
surroundedBy: "extract-all-table-data"
55+
}
56+
```
57+
58+
Note: Only first table columns will be used
59+
2460
# Supported Tables Flavor
2561

2662
WebTau supports standard HTML tables and [AG Grid](https://www.ag-grid.com).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: [Browser tableEl.extratTableData](browser/tables#extracting-table-data) to get table data from a page

webtau-feature-testing/examples/scenarios/ui/tables.groovy

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,46 @@ scenario("standard table equality") {
1010
// table-data-validation
1111
def summaryTable = browser.table("#summary")
1212

13-
summaryTable.should == ["column A" | "column B"] {
14-
___________________________
15-
"A1" | "B1"
16-
"A2" | "B2" }
13+
summaryTable.should == [ "column A" | "column B" | "column C"] {
14+
_______________________________________
15+
"A-1" | "B-1" | "C-1"
16+
"A-2" | "B-2" | "C-2" }
1717
// table-data-validation
1818
}
1919

20+
scenario("extract single table") {
21+
// extract-single-table-data
22+
def summaryTable = browser.table("#summary")
23+
def tableData = summaryTable.extractTableData()
24+
25+
data.csv.write("table-data.csv", tableData)
26+
// extract-single-table-data
27+
28+
defer {
29+
fs.delete("table-data.csv")
30+
}
31+
}
32+
33+
scenario("extract from each table") {
34+
// extract-all-table-data
35+
def tablesList = browser.table("table")
36+
def combinedTableData = tablesList.extractAndMergeTableData()
37+
38+
data.csv.write("combined-table-data.csv", combinedTableData)
39+
// extract-all-table-data
40+
41+
defer {
42+
fs.delete("combined-table-data.csv")
43+
}
44+
45+
combinedTableData.should == [ "column A" | "column B" | "column C"] {
46+
_______________________________________
47+
"A-1" | "B-1" | "C-1"
48+
"A-2" | "B-2" | "C-2"
49+
"A@1" | "B@1" | "C@1"
50+
"A@2" | "B@2" | "C@2" }
51+
}
52+
2053
scenario("simple ag grid equality") {
2154
def summaryTable = browser.table("#ag-grid-simple")
2255

webtau-feature-testing/src/test/resources/tables.html

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,48 @@
2929
<th>
3030
column B
3131
</th>
32+
<th>
33+
column C
34+
</th>
35+
</tr>
36+
</thead>
37+
<tbody>
38+
<tr>
39+
<td>A-1</td>
40+
<td>B-1</td>
41+
<td>C-1</td>
42+
</tr>
43+
<tr>
44+
<td>A-2</td>
45+
<td>B-2</td>
46+
<td>C-2</td>
47+
</tr>
48+
</tbody>
49+
</table>
50+
<table id='extra-summary'>
51+
<thead>
52+
<tr>
53+
<th>
54+
column A
55+
</th>
56+
<th>
57+
column B
58+
</th>
59+
<th>
60+
column C
61+
</th>
3262
</tr>
3363
</thead>
3464
<tbody>
3565
<tr>
36-
<td>A1</td>
37-
<td>B1</td>
66+
<td>A@1</td>
67+
<td>B@1</td>
68+
<td>C@1</td>
3869
</tr>
3970
<tr>
40-
<td>A2</td>
41-
<td>B2</td>
71+
<td>A@2</td>
72+
<td>B@2</td>
73+
<td>C@2</td>
4274
</tr>
4375
</tbody>
4476
</table>

webtau-feature-testing/test-expectations/scenarios/ui/tables/chrome-run-details.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
"stepsSummary" : {
1818
"numberOfSuccessful" : 1
1919
}
20+
}, {
21+
"scenario" : "extract single table",
22+
"shortContainerId" : "tables.groovy",
23+
"stepsSummary" : {
24+
"numberOfSuccessful" : 4
25+
}
26+
}, {
27+
"scenario" : "extract from each table",
28+
"shortContainerId" : "tables.groovy",
29+
"stepsSummary" : {
30+
"numberOfSuccessful" : 5
31+
}
2032
}, {
2133
"scenario" : "simple ag grid equality",
2234
"shortContainerId" : "tables.groovy",

webtau-feature-testing/test-expectations/scenarios/ui/tables/firefox-run-details.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
"stepsSummary" : {
1818
"numberOfSuccessful" : 1
1919
}
20+
}, {
21+
"scenario" : "extract single table",
22+
"shortContainerId" : "tables.groovy",
23+
"stepsSummary" : {
24+
"numberOfSuccessful" : 4
25+
}
26+
}, {
27+
"scenario" : "extract from each table",
28+
"shortContainerId" : "tables.groovy",
29+
"stepsSummary" : {
30+
"numberOfSuccessful" : 5
31+
}
2032
}, {
2133
"scenario" : "simple ag grid equality",
2234
"shortContainerId" : "tables.groovy",

webtau-junit5-examples/src/test/java/com/example/tests/junit5/BrowserTablesJavaTest.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.jupiter.api.Test;
2020
import org.testingisdocumenting.webtau.junit5.WebTau;
2121

22+
import static org.testingisdocumenting.webtau.WebTauCore.*;
2223
import static org.testingisdocumenting.webtau.WebTauDsl.*;
2324

2425
@WebTau
@@ -30,10 +31,36 @@ public void standardTable() {
3031
// table-data-validation
3132
var summaryTable = browser.table("#summary");
3233

33-
summaryTable.should(equal(table("column A", "column B",
34-
___________________________,
35-
"A1" , "B1",
36-
"A2" , "B2" )));
34+
summaryTable.should(equal(table("column A", "column B", "column C",
35+
____________________________________,
36+
"A-1" , "B-1", "C-1",
37+
"A-2" , "B-2", "C-2" )));
3738
// table-data-validation
3839
}
40+
41+
@Test
42+
public void extractSingleTable() {
43+
browser.open("/tables");
44+
45+
// extract-single-table-data
46+
var summaryTable = browser.table("#summary");
47+
var tableData = summaryTable.extractTableData();
48+
49+
data.csv.write("table-data.csv", tableData);
50+
// extract-single-table-data
51+
52+
defer(() -> fs.delete("table-data.csv"));
53+
}
54+
55+
@Test
56+
public void extractFromAllTables() {
57+
// extract-all-table-data
58+
var tablesList = browser.table("table");
59+
var combinedTableData = tablesList.extractAndMergeTableData();
60+
61+
data.csv.write("combined-table-data.csv", combinedTableData);
62+
// extract-all-table-data
63+
64+
defer(() -> fs.delete("combined-table-data.csv"));
65+
}
3966
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"standardTable()" : {
33
"numberOfSuccessful" : 3
4+
},
5+
"extractSingleTable()" : {
6+
"numberOfSuccessful" : 5
7+
},
8+
"extractFromAllTables()" : {
9+
"numberOfSuccessful" : 4
410
}
511
}

0 commit comments

Comments
 (0)