Skip to content

Commit b75ee13

Browse files
committed
Update package version to 1.0.8, implement 'wait' action in step executor, and modify examples to utilize the new wait functionality. Enhance integration tests to validate wait behavior with both 'value' and 'wait' properties.
1 parent 0b1e993 commit b75ee13

File tree

8 files changed

+173
-23
lines changed

8 files changed

+173
-23
lines changed

examples/advanced-usage.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ async function advancedExample() {
1010
},
1111
{
1212
id: 'wait_for_page',
13-
action: 'scroll',
14-
value: '100',
15-
wait: 3000
13+
action: 'wait',
14+
value: '3000'
1615
}
1716
],
1817
perPageSteps: [{

examples/advanced-usage.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ async function advancedExample() {
1212
},
1313
{
1414
id: 'wait_for_page',
15-
action: 'scroll',
16-
value: '100',
17-
wait: 3000
15+
action: 'wait',
16+
value: '3000'
1817
}
1918
],
2019
perPageSteps: [

examples/basic-usage.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ async function basicExample() {
1010
},
1111
{
1212
id: 'wait_for_page',
13-
action: 'scroll',
14-
value: '100',
15-
wait: 2000
13+
action: 'wait',
14+
value: '2000'
1615
},
1716
{
1817
id: 'get_title',
@@ -22,6 +21,11 @@ async function basicExample() {
2221
key: 'title',
2322
data_type: 'text'
2423
},
24+
{
25+
id: 'wait_after_title',
26+
action: 'wait',
27+
wait: 1000
28+
},
2529
{
2630
id: 'get_description',
2731
action: 'data',

examples/basic-usage.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@ async function basicExample() {
1212
},
1313
{
1414
id: 'wait_for_page',
15-
action: 'scroll',
16-
value: '100',
17-
wait: 2000
15+
action: 'wait',
16+
value: '2000'
17+
},
18+
{
19+
id: 'get_title',
20+
action: 'data',
21+
object_type: 'tag',
22+
object: 'h1',
23+
key: 'title',
24+
data_type: 'text'
1825
},
19-
{
20-
id: 'get_title',
21-
action: 'data',
22-
object_type: 'tag',
23-
object: 'h1',
24-
key: 'title',
25-
data_type: 'text'
26-
},
2726
{
2827
id: "reload",
2928
action: 'reload',
30-
value: 'load',
29+
value: 'load'
30+
},
31+
{
32+
id: 'wait_after_reload',
33+
action: 'wait',
3134
wait: 2000
3235
},
3336
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stepwright",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "A powerful web scraping library built with Playwright",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/step-executor.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,25 @@ export async function executeStep(
394394
await page.evaluate((y: number) => window.scrollBy(0, y), offset);
395395
break;
396396
}
397+
case 'wait': {
398+
// Wait for a specified duration in milliseconds
399+
// Duration can be provided via step.value (as string) or step.wait (as number)
400+
let duration = 0;
401+
if (step.value) {
402+
duration = parseInt(step.value, 10);
403+
} else if (step.wait) {
404+
duration = step.wait;
405+
} else {
406+
console.log(` ⚠️ Wait step ${step.id} requires 'value' (duration in ms) or 'wait' property`);
407+
break;
408+
}
409+
410+
if (duration > 0) {
411+
console.log(` ⏳ Waiting ${duration}ms...`);
412+
await page.waitForTimeout(duration);
413+
}
414+
break;
415+
}
397416
case 'reload': {
398417
// Reload the current page
399418
try {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface BaseStep {
1111
description?: string;
1212
object_type?: SelectorType;
1313
object?: string;
14-
action: 'navigate' | 'input' | 'click' | 'data' | 'scroll' | 'reload' | 'eventBaseDownload' | 'foreach' | 'open' | 'savePDF' | 'printToPDF' | 'downloadPDF' | 'downloadFile';
14+
action: 'navigate' | 'input' | 'click' | 'data' | 'scroll' | 'reload' | 'eventBaseDownload' | 'foreach' | 'open' | 'savePDF' | 'printToPDF' | 'downloadPDF' | 'downloadFile' | 'wait';
1515
value?: string;
1616
key?: string; // property key when collecting data
1717
data_type?: DataType;

test/integration.test.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,5 +330,131 @@ describe('Integration Tests', () => {
330330
expect(results[0].title).toBe('StepWright Test Page');
331331
expect(results[0].page_title).toBe('StepWright Test Page');
332332
});
333+
334+
it('should handle wait action with value property', async () => {
335+
const templates :TabTemplate[] = [
336+
{
337+
tab: 'wait_test',
338+
steps: [
339+
{
340+
id: 'navigate',
341+
action: 'navigate',
342+
value: testPageUrl
343+
},
344+
{
345+
id: 'wait_before_extraction',
346+
action: 'wait',
347+
value: '500'
348+
},
349+
{
350+
id: 'get_title',
351+
action: 'data',
352+
object_type: 'id',
353+
object: 'main-title',
354+
key: 'title',
355+
data_type: 'text'
356+
}
357+
]
358+
}
359+
];
360+
361+
const startTime = Date.now();
362+
const results = await runScraper(templates);
363+
const endTime = Date.now();
364+
const duration = endTime - startTime;
365+
366+
expect(results).toHaveLength(1);
367+
expect(results[0].title).toBe('StepWright Test Page');
368+
// Verify that wait actually occurred (allowing some margin for test execution)
369+
expect(duration).toBeGreaterThanOrEqual(400);
370+
});
371+
372+
it('should handle wait action with wait property', async () => {
373+
const templates :TabTemplate[] = [
374+
{
375+
tab: 'wait_test_wait_prop',
376+
steps: [
377+
{
378+
id: 'navigate',
379+
action: 'navigate',
380+
value: testPageUrl
381+
},
382+
{
383+
id: 'wait_before_extraction',
384+
action: 'wait',
385+
wait: 300
386+
},
387+
{
388+
id: 'get_title',
389+
action: 'data',
390+
object_type: 'id',
391+
object: 'main-title',
392+
key: 'title',
393+
data_type: 'text'
394+
}
395+
]
396+
}
397+
];
398+
399+
const startTime = Date.now();
400+
const results = await runScraper(templates);
401+
const endTime = Date.now();
402+
const duration = endTime - startTime;
403+
404+
expect(results).toHaveLength(1);
405+
expect(results[0].title).toBe('StepWright Test Page');
406+
// Verify that wait actually occurred (allowing some margin for test execution)
407+
expect(duration).toBeGreaterThanOrEqual(200);
408+
});
409+
410+
it('should handle wait action in sequence with other actions', async () => {
411+
const templates :TabTemplate[] = [
412+
{
413+
tab: 'wait_sequence_test',
414+
steps: [
415+
{
416+
id: 'navigate',
417+
action: 'navigate',
418+
value: testPageUrl
419+
},
420+
{
421+
id: 'wait_after_navigate',
422+
action: 'wait',
423+
value: '200'
424+
},
425+
{
426+
id: 'fill_search',
427+
action: 'input',
428+
object_type: 'id',
429+
object: 'search-box',
430+
value: 'test search'
431+
},
432+
{
433+
id: 'wait_after_input',
434+
action: 'wait',
435+
value: '200'
436+
},
437+
{
438+
id: 'get_search_value',
439+
action: 'data',
440+
object_type: 'id',
441+
object: 'search-box',
442+
key: 'search_value',
443+
data_type: 'value'
444+
}
445+
]
446+
}
447+
];
448+
449+
const startTime = Date.now();
450+
const results = await runScraper(templates);
451+
const endTime = Date.now();
452+
const duration = endTime - startTime;
453+
454+
expect(results).toHaveLength(1);
455+
expect(results[0].search_value).toBe('test search');
456+
// Verify that waits occurred (allowing some margin for test execution)
457+
expect(duration).toBeGreaterThanOrEqual(300);
458+
});
333459
});
334460
});

0 commit comments

Comments
 (0)