Skip to content

Commit

Permalink
Neater JavaScript
Browse files Browse the repository at this point in the history
Removed constants, ran it through Prettier
  • Loading branch information
simonw authored Sep 4, 2024
1 parent 806408f commit 399e34a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions playwright/testing-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ I figured out this pattern today for testing an HTML table dynamically added to
```python
table_js = """
function tableToJson() {
const tables = document.querySelectorAll('table');
return Array.from(tables).map(table => {
const headers = Array.from(table.querySelectorAll('th')).map(th => th.textContent.trim());
const rows = Array.from(table.querySelectorAll('tr')).slice(1).map(tr => {
const cells = Array.from(tr.querySelectorAll('td, th')).map(cell => cell.textContent.trim());
return cells;
});
return {
headers: headers,
rows: rows
};
});
return Array.from(document.querySelectorAll("table")).map((table) => {
return {
headers: Array.from(table.querySelectorAll("th")).map((th) =>
th.textContent.trim(),
),
rows: Array.from(table.querySelectorAll("tr"))
.slice(1)
.map((tr) => {
return Array.from(tr.querySelectorAll("td, th")).map((cell) =>
cell.textContent.trim(),
);
}),
};
});
}
"""

Expand Down

0 comments on commit 399e34a

Please sign in to comment.