From 49140d74bf72ea884026af6d87d2dd11787d41e2 Mon Sep 17 00:00:00 2001 From: ymayarajan3 Date: Sun, 16 Jun 2024 23:22:42 -0400 Subject: [PATCH] adding node script --- extensions/integration_testing/node_script.ts | 84 +++++++++++++++++++ .../integration_testing/playwright_load.ts | 21 ++++- .../playwright_save_all.ts | 18 +++- .../playwright_save_combo.ts | 3 +- 4 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 extensions/integration_testing/node_script.ts diff --git a/extensions/integration_testing/node_script.ts b/extensions/integration_testing/node_script.ts new file mode 100644 index 000000000..d5c2fc130 --- /dev/null +++ b/extensions/integration_testing/node_script.ts @@ -0,0 +1,84 @@ +const { exec } = require('child_process'); + +let devProcess: any; + +async function runDevScript(param: string): Promise { + return new Promise((resolve, reject) => { + devProcess = exec(`cd ../.. && pnpm dev -i ${param}`, (error: any, stdout: any, stderr: any) => { + if (error) { + console.error(`Error running dev script: ${error}`); + reject(error); + } else { + console.log(`Dev script output: ${stdout}`); + resolve(); + } + }); + + devProcess.stdout.on('data', (data: any) => { + console.log(`stdout: ${data}`); + }); + + devProcess.stderr.on('data', (data: any) => { + console.error(`stderr: ${data}`); + }); + }); + } + + async function runPlaywrightScript(scriptName: string) { + return new Promise((resolve, reject) => { + const command = `npx ts-node ${scriptName}`; + const playwrightProcess = exec(command, (error: any, stdout: any, stderr: any) => { + if (error) { + console.error(`Error running script: ${error.message}`); + reject(error); + } + if (stderr) { + console.error(`Error output: ${stderr}`); + reject(stderr); + } + console.log(`Script output: ${stdout}`); + resolve(stdout); + }); + + playwrightProcess.stdout.on('data', (data: any) => { + console.log(`stdout: ${data}`); + }); + + playwrightProcess.stderr.on('data', (data: any) => { + console.error(`stderr: ${data}`); + }); + }); +} + + +async function runPlaywrightTasks() { + try { + await runPlaywrightScript('playwright_save_all.ts'); + await runPlaywrightScript('playwright_save_combo.ts'); + await runPlaywrightScript('playwright_load.ts'); + devProcess.kill(); + } catch (error) { + console.error('Error during Playwright tasks:', error); + } + } + +async function main() { + const args = process.argv.slice(2); + + if (args.length === 0) { + console.error('Please provide a parameter.'); + return; + } + + const param = args[0]; + + try { + runDevScript(param); + console.log('Dev script started successfully'); + runPlaywrightTasks(); + } catch (error) { + console.error('Error running script:', error); + } + } + + main(); diff --git a/extensions/integration_testing/playwright_load.ts b/extensions/integration_testing/playwright_load.ts index 851111737..dd09b1bac 100644 --- a/extensions/integration_testing/playwright_load.ts +++ b/extensions/integration_testing/playwright_load.ts @@ -1,6 +1,8 @@ import { chromium, Browser, BrowserContext, Page, ElementHandle } from 'playwright'; import * as path from 'path'; +const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + (async () => { // Launch a browser instance const browser = await chromium.launch({ headless: false }); // Set headless: false to see the browser actions @@ -11,6 +13,19 @@ import * as path from 'path'; const page = await context.newPage(); // Navigate to 'Create' page + + // Try connecting to the server in a loop with delay +// let connected = false; +// for (let i = 0; i < 120; i++) { // Retry up to 10 times +// try { +// await page.goto('http://localhost:8602/', { waitUntil: 'networkidle', timeout: 6000 }); +// connected = true; +// break; +// } catch (e) { +// console.log('Connection failed, retrying...'); +// await delay(5000); // Wait for 5 seconds before retrying +// } +// } await page.goto('http://localhost:8602/'); await page.click('text=File'); @@ -51,7 +66,8 @@ import * as path from 'path'; } }); - await page.waitForSelector('.blocklyBlockCanvas'); + //await page.waitForSelector('.blocklyBlockCanvas', { timeout: 120000 }); + await page.waitForTimeout(2000); await page.click('text=File'); const [fileChooser2] = await Promise.all([ page.waitForEvent('filechooser'), @@ -59,7 +75,8 @@ import * as path from 'path'; ]); await fileChooser2.setFiles(filePath); - await page.waitForSelector('.blocklyBlockCanvas'); + //await page.waitForSelector('.blocklyBlockCanvas', { timeout: 120000 }); + await page.waitForTimeout(2000); // Wait until blockly canvas has children diff --git a/extensions/integration_testing/playwright_save_all.ts b/extensions/integration_testing/playwright_save_all.ts index cafaa5245..2dcdb5280 100644 --- a/extensions/integration_testing/playwright_save_all.ts +++ b/extensions/integration_testing/playwright_save_all.ts @@ -2,6 +2,8 @@ import { chromium } from 'playwright'; import { expect } from 'playwright/test'; import * as path from 'path'; +const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + (async () => { // Launch a browser instance const browser = await chromium.launch({ headless: false }); // Set headless: false to see the browser actions @@ -10,7 +12,17 @@ import * as path from 'path'; acceptDownloads: true }); const page = await context.newPage(); - await page.goto('http://localhost:8602/'); + let connected = false; + for (let i = 0; i < 120; i++) { // Retry up to 10 times + try { + await page.goto('http://localhost:8602/', { waitUntil: 'networkidle', timeout: 6000 }); + connected = true; + break; + } catch (e) { + console.log('Connection failed, retrying...'); + await delay(5000); // Wait for 5 seconds before retrying + } + } // All the extensions that have been included const includedExtensions = [ @@ -35,8 +47,8 @@ import * as path from 'path'; const startY = 130; // Wait for the editor to load - await page.waitForSelector('.blocklyWorkspace'); - + //await page.waitForSelector('.blocklyBlockCanvas'); + await page.waitForTimeout(2000); // Add the extension const addExtension = await page.$('[title="Add Extension"]'); if (addExtension) { diff --git a/extensions/integration_testing/playwright_save_combo.ts b/extensions/integration_testing/playwright_save_combo.ts index 2c0375065..0df824b89 100644 --- a/extensions/integration_testing/playwright_save_combo.ts +++ b/extensions/integration_testing/playwright_save_combo.ts @@ -31,7 +31,8 @@ import * as path from 'path'; ]; - await page.waitForSelector('.blocklyWorkspace'); + //await page.waitForSelector('.blocklyWorkspace'); + await page.waitForTimeout(2000); // Make sure no blocks are dragged out of the workspace function boundY(y: any) {