(#513) Add function to find row in table by text#514
(#513) Add function to find row in table by text#514alexaveldanez wants to merge 1 commit intochocolatey:mainfrom
Conversation
Adds function to find the first row with specified text in a table and return the locator of that row.
|
The audit flow will be fixed in this PR #507. |
There was a problem hiding this comment.
Pull request overview
This PR adds a new Playwright helper function findRowInTable to assist with test automation. The function searches for a row containing specific text within a table and handles pagination automatically by clicking through pages until the row is found.
Changes:
- Added
find-row-in-table.tsfunction that searches tables for rows with specific text content - Includes pagination support by clicking "Next" button and checking aria-disabled attribute
- Returns the locator of the first matching row or throws an error if not found
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| const ariaDisabled = await btnNext.getAttribute('aria-disabled'); | ||
|
|
||
| if (ariaDisabled) { |
There was a problem hiding this comment.
The condition if (ariaDisabled) checks for any truthy value, but getAttribute() returns string | null. This means the check will treat the string "false" as truthy and incorrectly break the loop. The condition should explicitly check for the value "true": if (ariaDisabled === 'true') to properly determine when the Next button is disabled.
| if (ariaDisabled) { | |
| if (ariaDisabled === 'true') { |
| /** | ||
| * Finds the first row with specific text in a table and returns the locator for that row. | ||
| * @function findRowInTable | ||
| * @param {Locator} table - The locator for the table. |
There was a problem hiding this comment.
The JSDoc documents a table parameter that is never used in the function implementation. This documentation is misleading and should either be removed (if the parameter is removed) or updated to reflect the actual usage. The current implementation searches for rows in the entire page, not scoped to a specific table.
| await waitForTables(page); | ||
| } | ||
|
|
||
| throw new Error(`${text} not found in the table after checking all pages.`); |
There was a problem hiding this comment.
The error message uses template literal syntax but doesn't escape the text parameter, which could lead to confusing error messages if the search text contains special characters or is very long. Consider wrapping the text in quotes for clarity: throw new Error(\Row with text "${text}" not found in the table after checking all pages.`);`
| throw new Error(`${text} not found in the table after checking all pages.`); | |
| throw new Error(`Row with text "${text}" not found in the table after checking all pages.`); |
| while(true) { | ||
| const row = page.locator('tr', { has: page.locator('td', { hasText: text })}); | ||
|
|
||
| if (await row.count() > 0) { | ||
| return row.first(); | ||
| } | ||
|
|
||
| const ariaDisabled = await btnNext.getAttribute('aria-disabled'); | ||
|
|
||
| if (ariaDisabled) { | ||
| break; | ||
| } | ||
|
|
||
| await btnNext.click(); | ||
| await waitForTables(page); |
There was a problem hiding this comment.
The function could enter an infinite loop if the "Next" button doesn't have an aria-disabled attribute when it reaches the last page. Consider adding a timeout or maximum page count as a safety mechanism. Additionally, if the Next button doesn't exist on the page at all, the getAttribute() call on line 25 could throw an error. Add existence checks or error handling to make the function more robust.
| * @returns {Locator} - Locator for the row. | ||
| */ | ||
|
|
||
| export const findRowInTable = async(table: Locator, page: Page, text: string) => { |
There was a problem hiding this comment.
The table parameter is declared but never used in the function body. All locator operations use page.locator() directly instead of scoping to the provided table. Either remove this parameter if it's not needed, or use it to scope the row search to a specific table (e.g., table.locator('tr', ...)). Based on the function's purpose, it appears the parameter should be removed since the function searches the entire page.
| * @returns {Locator} - Locator for the row. | ||
| */ | ||
|
|
||
| export const findRowInTable = async(table: Locator, page: Page, text: string) => { |
There was a problem hiding this comment.
Missing space between async and the opening parenthesis. This should be async (table: Locator, page: Page, text: string) to match the formatting convention used in other functions in this codebase (see convert-to-file-path.ts:15, expand-section.ts:17, wait-for-tables.ts:11).
| export const findRowInTable = async(table: Locator, page: Page, text: string) => { | |
| export const findRowInTable = async (table: Locator, page: Page, text: string) => { |
| * @returns {Locator} - Locator for the row. | ||
| */ | ||
|
|
||
| export const findRowInTable = async(table: Locator, page: Page, text: string) => { |
There was a problem hiding this comment.
The function signature is missing an explicit return type. According to the codebase convention, all async functions should specify their return type (see examples in convert-to-file-path.ts:15, expand-section.ts:20, wait-for-tables.ts:11). Add : Promise<Locator> after the parameter list to match this pattern.
| export const findRowInTable = async(table: Locator, page: Page, text: string) => { | ||
| const btnNext = page.getByRole('link', { name: 'Next' }); | ||
|
|
||
| while(true) { |
There was a problem hiding this comment.
Missing space after while. The code has while(true) but should be while (true) to match standard formatting conventions.
| while(true) { | |
| while (true) { |
Description Of Changes
Adds function to find the first row with specified text in a table and return the locator of that row.
Motivation and Context
This will help automating manual tests.
Testing
node "C:\Program Files (x86)\Yarn\bin\yarn.js" up '@chocolatey-software/playwright@alexaveldanez/choco-theme#head=find-row-in-tables&workspace=@chocolatey-software/playwright'row-in-table.tsfile fromchoco-licensed-management-ui\src\ChocolateySoftware.ChocolateyManagement.Web.Mvc\playwright\functionssoftware.spec.tsand update the import toimport { findRowInTable } from '@choco-playwright/functions/find-row-in-table';administration-users-lockout-enabled.spec.tsand update the import toimport { findRowInTable } from '@choco-playwright/functions/find-row-in-table';yarn playwright-functionality software.spec.ts administration-users-lockout-enabled.spec.ts.Operating Systems Testing
Change Types Made
Change Checklist
Related Issue
#513
Fixes #513