Skip to content

Commit 96383c9

Browse files
authored
internal(browser): Add waitForTimeout to editor and debugger (#1169)
1 parent a5da647 commit 96383c9

File tree

21 files changed

+280
-38
lines changed

21 files changed

+280
-38
lines changed

package-lock.json

Lines changed: 1 addition & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Generated by Grafana k6 Studio (0.0.0-vitest) on 2023-10-01T00:00:00.000Z
2+
3+
import { browser } from "k6/browser";
4+
5+
export const options = {
6+
scenarios: {
7+
default: {
8+
executor: "shared-iterations",
9+
options: { browser: { type: "chromium" } },
10+
},
11+
},
12+
};
13+
14+
export default async function () {
15+
const page = await browser.newPage();
16+
17+
try {
18+
await page.waitForTimeout(1500);
19+
} finally {
20+
await page?.close();
21+
}
22+
}

src/codegen/browser/code/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function isBrowserScenario(scenario: ir.Scenario) {
5353
case 'WaitForExpression':
5454
case 'WaitForOptionsExpression':
5555
case 'WaitForNavigationExpression':
56+
case 'WaitForTimeoutExpression':
5657
return true
5758

5859
case 'Identifier':

src/codegen/browser/code/scenario.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,19 @@ function emitWaitForNavigationExpression(
469469
.done()
470470
}
471471

472+
function emitWaitForTimeoutExpression(
473+
context: ScenarioContext,
474+
expression: ir.WaitForTimeoutExpression
475+
): ts.Expression {
476+
const target = emitExpression(context, expression.target)
477+
478+
return new ExpressionBuilder(target)
479+
.member('waitForTimeout')
480+
.call([literal({ value: expression.timeout })])
481+
.await(context)
482+
.done()
483+
}
484+
472485
function emitPromiseAllExpression(
473486
context: ScenarioContext,
474487
expression: ir.PromiseAllExpression
@@ -571,6 +584,9 @@ function emitExpression(
571584
case 'WaitForNavigationExpression':
572585
return emitWaitForNavigationExpression(context, expression)
573586

587+
case 'WaitForTimeoutExpression':
588+
return emitWaitForTimeoutExpression(context, expression)
589+
574590
case 'PromiseAllExpression':
575591
return emitPromiseAllExpression(context, expression)
576592

src/codegen/browser/codegen.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,30 @@ it('should emit a waitFor statement', async ({ expect }) => {
12211221
)
12221222
})
12231223

1224+
it('should emit page.waitForTimeout', async ({ expect }) => {
1225+
const script = await emitScript({
1226+
defaultScenario: {
1227+
nodes: [
1228+
{
1229+
type: 'page',
1230+
nodeId: 'page',
1231+
},
1232+
{
1233+
type: 'wait-for-timeout',
1234+
nodeId: 'wait-for-timeout',
1235+
timeout: 1500,
1236+
inputs: { page: { nodeId: 'page' } },
1237+
},
1238+
],
1239+
},
1240+
scenarios: {},
1241+
})
1242+
1243+
await expect(script).toMatchFileSnapshot(
1244+
'__snapshots__/browser/wait-for-timeout-statement.ts'
1245+
)
1246+
})
1247+
12241248
it('should emit merged try-finally blocks', async ({ expect }) => {
12251249
const script = await emitScript({
12261250
defaultScenario: {

src/codegen/browser/intermediate/ast.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ export interface WaitForNavigationExpression {
152152
target: Expression
153153
}
154154

155+
export interface WaitForTimeoutExpression {
156+
type: 'WaitForTimeoutExpression'
157+
target: Expression
158+
timeout: number
159+
}
160+
155161
export interface PromiseAllExpression {
156162
type: 'PromiseAllExpression'
157163
expressions: Expression[]
@@ -237,6 +243,7 @@ export type Expression =
237243
| WaitForExpression
238244
| WaitForOptionsExpression
239245
| WaitForNavigationExpression
246+
| WaitForTimeoutExpression
240247
| PromiseAllExpression
241248

242249
export interface VariableDeclaration {

0 commit comments

Comments
 (0)