|
1 | 1 | import { check } from 'k6'; |
2 | | -import http from 'k6/http'; |
| 2 | +import { browser } from 'k6/browser'; |
3 | 3 | import { defaultOptions, url } from './lib/util.js'; |
4 | 4 |
|
5 | | -export const options = defaultOptions(); |
| 5 | +const streamingUrl = url('stream_async_components?delay=5'); |
6 | 6 |
|
7 | | -export default () => { |
8 | | - const streamingUrl = url('stream_async_components?delay=5'); |
9 | | - check(http.get(streamingUrl), { |
10 | | - 'status was 200': (res) => res.status === 200, |
11 | | - 'has all comments': (res) => { |
12 | | - const body = res.html().text(); |
13 | | - const commentIds = [1, 2, 3, 4]; |
14 | | - const hasAllComments = commentIds.every((commentId) => body.includes(`Comment ${commentId}`)); |
15 | | - const hasFailedRequests = !!body.match(/Request to .+ failed/i); |
16 | | - return hasAllComments && !hasFailedRequests; |
17 | | - }, |
18 | | - }); |
| 7 | +export const options = { |
| 8 | + scenarios: { |
| 9 | + browser: defaultOptions({ isBrowser: true }), |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +export default async () => { |
| 14 | + const page = await browser.newPage(); |
| 15 | + try { |
| 16 | + const response = await page.goto(streamingUrl); |
| 17 | + check(response, { |
| 18 | + 'status was 200': (res) => res.status() === 200, |
| 19 | + }); |
| 20 | + await page.waitForFunction(() => !document.body.textContent.includes('Loading'), { |
| 21 | + // in milliseconds |
| 22 | + timeout: 5000, |
| 23 | + }); |
| 24 | + check(await page.locator('html').textContent(), { |
| 25 | + 'has all comments': (text) => { |
| 26 | + // can't define commentIds as a constant outside, this runs in browser context |
| 27 | + const commentIds = [1, 2, 3, 4]; |
| 28 | + return commentIds.every((id) => text.includes(`Comment ${id}`)); |
| 29 | + }, |
| 30 | + }); |
| 31 | + } finally { |
| 32 | + await page.close(); |
| 33 | + } |
19 | 34 | }; |
0 commit comments