Progress Bar
A progress bar that gradually reaches 100%.
Waiting for a value, then acting on it
Read progress from ARIA, not from styles
The bar has role="progressbar" and reports its value in aria-valuenow. That attribute is made for assistive technology, which makes it a stable thing to test. The bar's width in pixels is not.
const bar = page.getByRole('progressbar', { name: 'Upload progress' });
await page.getByRole('button', { name: 'Start progress' }).click();
await expect(bar).toHaveAttribute('aria-valuenow', '100');Act once a threshold is reached
To stop the bar at 70% or more, wait until the value is high enough, then click. expect.poll() retries any function until its result passes the matcher, which is useful for numbers:
await expect
.poll(async () => Number(await bar.getAttribute('aria-valuenow')), {
intervals: [50],
})
.toBeGreaterThanOrEqual(70);
await page.getByRole('button', { name: 'Stop' }).click();
await expect(page.getByTestId('progress-result')).toHaveText(/Stopped at (7\d|8\d|9\d)%/);intervals sets how often it checks. The default starts slowly, at 100 ms, then 250 ms, then 500 ms, so the bar could reach 100% between two checks.
The bar moves on while your test runs, so you cannot hit an exact value such as 75%. Assert a range instead.
Upload
Moves 5% every 200 ms. Stop it at 70% or more.
0%
Next in Dynamic Elements · 4 of 6Actionability →Understand what Playwright waits for before it clicks, and what force: true hides.
Related exercises
- Dynamic ElementsBeginnerChanging TextAssert text that changes over time or is different on every run.
- Dynamic ElementsAdvancedTimers & ClockTest time-based behaviour instantly, and date-based content on any day.
- Content & AttributesIntermediateARIA WidgetsLocate widgets by their ARIA state, and assert whole structures with aria snapshots.
Read the guide: Playwright auto-waiting: how to fix flaky tests