From d90e9eacca09f02392b03ce1102bb4973301382b Mon Sep 17 00:00:00 2001 From: ymayarajan3 Date: Fri, 7 Jun 2024 00:43:09 -0400 Subject: [PATCH] cleaning folder --- .../playwright_load_old.ts | 289 ------------- .../playwright_save copy.ts | 380 ------------------ extensions/integration_testing/test_fn.ts | 21 - 3 files changed, 690 deletions(-) delete mode 100644 extensions/integration_testing/playwright_load_old.ts delete mode 100644 extensions/integration_testing/playwright_save copy.ts delete mode 100644 extensions/integration_testing/test_fn.ts diff --git a/extensions/integration_testing/playwright_load_old.ts b/extensions/integration_testing/playwright_load_old.ts deleted file mode 100644 index 6b728baf2..000000000 --- a/extensions/integration_testing/playwright_load_old.ts +++ /dev/null @@ -1,289 +0,0 @@ -import { chromium, Browser, BrowserContext, Page, ElementHandle } from 'playwright'; -import * as path from 'path'; - -(async () => { - // Launch a browser instance - const browser = await chromium.launch({ headless: false }); // Set headless: false to see the browser actions - const context = await browser.newContext({ - permissions: ['camera'], - acceptDownloads: true - }); - const page = await context.newPage(); - - // Navigate to 'Create' page - await page.goto('http://localhost:8602/'); - await page.click('text=File'); - //await page.click('text=Load from your computer'); - const [fileChooser] = await Promise.all([ - page.waitForEvent('filechooser'), - page.click('text=Load from your computer'), - ]); - - const filePath = path.resolve(__dirname, 'downloads', 'My Project.sb3'); - console.log(`Uploading file from: ${filePath}`); - - // Set the files to upload - await fileChooser.setFiles(filePath); - - page.on('dialog', async dialog => { - console.log(`Dialog message: ${dialog.message()}`); - if (dialog.message().includes("The project file that was selected failed to load.")) { - console.log("Invalid opcode found.") - console.log("TEST FAILED"); - } - // Optionally accept the dialog - await dialog.accept(); - }); - - let incompatibleTypes = false; - page.on('console', (message) => { - // Check if the message is an error - if (message.type() === 'error') { - // Check if the specific error message is logged - if (message.text().includes('gui Attempt to connect incompatible types.')) { - console.log("Incompatible function types detected.") - console.log("TEST FAILED"); - incompatibleTypes = true; - } - } - }); - await page.waitForSelector('.blocklyBlockCanvas'); - // Upload the file again for data-opcode to show up - await page.click('text=File'); - const [fileChooser2] = await Promise.all([ - page.waitForEvent('filechooser'), - page.click('text=Load from your computer'), - ]); - - await fileChooser2.setFiles(filePath); - await page.waitForSelector('.blocklyBlockCanvas'); - - - // Wait until blockly canvas has children - await page.$eval('.blocklyBlockCanvas', (element) => { - return element.children.length > 0; - }); - - const blocklyZoomElement = await page.waitForSelector('.blocklyZoom', { timeout: 5000 }); - const boundingBox = await blocklyZoomElement.boundingBox(); - if (boundingBox) { - // Calculate the middle of the element - const x = boundingBox.x + boundingBox.width / 2; - const y = boundingBox.y + boundingBox.height / 2; - - // Click the middle of the element - await page.mouse.click(x, y); - await page.mouse.click(x, y); - await page.mouse.click(x, y); - await page.mouse.click(x, y); - await page.mouse.click(x, y); - await page.mouse.click(x, y); - console.log('Successfully clicked in the middle of the blocklyZoom element.'); - } - - // WE WON'T NEED THIS except for defining the elements and removing priitive opcodes - var dataIds = await page.$eval('.blocklyBlockCanvas', (blockCanvas) => { - const elements = blockCanvas.querySelectorAll('[data-id]'); - const ids: any = []; - - const calculateDepth = (element: any) => { - let depth = 0; - let currentElement = element.parentElement; - while (currentElement) { - depth++; - currentElement = currentElement.parentElement; - } - return depth; - }; - - elements.forEach((element: any) => { - const opcode = element.getAttribute('data-opcode'); - if (opcode && opcode !== "math_number" && opcode !== "text" && !opcode.includes("_menu_")) { - const dataId = element.getAttribute('data-id'); - if (dataId) { - const shape = element.getAttribute('data-shapes'); - const opcode = element.getAttribute('data-opcode'); - const depth = calculateDepth(element); - ids.push({ id: dataId, shape, depth, opcode }); - } - } - }); - - // Sort the ids array based on depth in descending order - ids.sort((a: any, b: any) => b.depth - a.depth); - return ids; - }); - - // const seenOpcodes: any[] = []; - // for (const block of dataIds) { - // if (seenOpcodes.includes(block.opcode)) { - // console.log("HERE"); - // const elementHandle = await page.$(`[data-id="${block.id}"]`); - // const box = await elementHandle?.boundingBox(); - // if (box) { - // const x = box.x + box.width / 2; - // const y = box.y + box.height / 2; - // await page.mouse.click(x, y, { button: 'right' }); - // try { - // await page.click('text=Delete Block', { timeout: 500 }); - // } catch(e) { - - // } - // await page.waitForTimeout(500); - // } - - // } else { - // seenOpcodes.push(block.opcode); - // } - // } - - - const minDepthNode = dataIds.reduce((minObj: any, currentObj: any) => { - return currentObj.depth < minObj.depth ? currentObj : minObj; - }, dataIds[0]); - const minDepth = minDepthNode.depth; - - let moveX = 600; - let moveY = 100; - let alternate = false; - for (const dataId of dataIds) { - const elementHandle = await page.$(`[data-id="${dataId.id}"]`); - if (elementHandle && dataId.depth > minDepth) { - const box = await elementHandle.boundingBox(); - if (box) { - const startX = box.x + box.width / 2; - const startY = box.y + box.height / 2; - - - await page.mouse.move(startX, startY); - await page.mouse.down(); - await page.mouse.move(moveX, moveY); - await page.mouse.up(); - moveY = moveY + 20; - if (alternate) { - moveX = moveX + 20; - alternate = false; - } else { - moveX = moveX - 20; - alternate = true; - } - await page.waitForTimeout(1000); - } - } - } - // END WE WON'T NEED THIS - - var blocks = await page.$eval('.blocklyBlockCanvas', blockCanvas => { - const elements = blockCanvas.querySelectorAll('[data-id]'); - return Array.from(elements).map(element => { - const argumentElements = (element as any).querySelectorAll('[data-argument-type]'); - - const argumentList = Array.from(argumentElements).map(argElement => { - const argumentType = (argElement as any).getAttribute('data-argument-type'); - const transform = (argElement as any).getAttribute('transform'); - const match = transform.match(/translate\(([\d.]+),\s*([\d.]+)\)/); - let yValue = 0.0; - if (match && match.length > 1) { - yValue = parseFloat(match[1]); - } - return { - type: argumentType, - xValue: String(yValue) - } - }); - const filteredArgumentList = argumentList.filter((arg, index, self) => { - console.log(arg.type); - if (arg.type == 'round') { - const otherValueIncluded = self.some((otherArg: any, otherIndex: any) => otherIndex !== index && otherArg.xValue === arg.xValue); - if (!otherValueIncluded) { - console.log("New argument detected."); - console.log("TEST FAILED"); - return true; - } else { - return false; - } - } - return true; - }); - const sortedArgumentList = filteredArgumentList.sort((a: any, b: any) => a.xValue - b.xValue).map(element => element.type); - - return { - id: (element as any).getAttribute('data-id'), - arguments: sortedArgumentList, - opcode: (element as any).getAttribute('data-opcode'), - }; - }); - }); - - blocks = blocks.filter((block: any) => { - return block.opcode && block.opcode != "math_number" && block.opcode != "text" && !block.opcode.includes("_menu_"); - }) - - - function arraysMatch(arr1: string[], arr2: string[]): boolean { - if (arr1.length !== arr2.length) return false; - return arr1.every((value, index) => value === arr2[index]); - } - - - - for (const block of blocks) { - const selector = `[data-id="${block.opcode}"]`; - const elementHandle = await page.$(selector); - - if (elementHandle) { - const argumentElements = await elementHandle.$$('[data-argument-type]'); - - const argumentList = await Promise.all(argumentElements.map(async argHandle => { - const argumentType = await argHandle.getAttribute('data-argument-type'); - const transform = await argHandle.getAttribute('transform'); - let xValue = 0.0; - - if (transform) { - const match = transform.match(/translate\(([\d.]+),\s*([\d.]+)\)/); - if (match && match.length > 1) { - xValue = parseFloat(match[1]); // Adjusted to get the y value instead of x value - } - } - return { - type: argumentType, - xValue: xValue - }; - })); - const filteredArgumentList = argumentList.filter((arg, index, self) => { - if (arg.type == 'round') { - const otherValueIncluded = self.some((otherArg: any, otherIndex: any) => otherIndex !== index && otherArg.xValue === arg.xValue); - if (!otherValueIncluded) { - console.log("New argument detected.") - console.log("TEST FAILED"); - return true; - } else { - return false; - } - } - return true; - }); - const sortedArgumentList: any[] = filteredArgumentList.sort((a: any, b: any) => a.xValue - b.xValue).map(element => element.type); - if (!arraysMatch(block.arguments, sortedArgumentList)) { - - console.log(block.arguments); - console.log(sortedArgumentList); - console.log("Argument order does not match.") - console.log("TEST FAILED") - } - - - } - - } - - - - - - await page.waitForTimeout(20000); - - // Close the browser - await browser.close(); -})(); - \ No newline at end of file diff --git a/extensions/integration_testing/playwright_save copy.ts b/extensions/integration_testing/playwright_save copy.ts deleted file mode 100644 index e43adf9df..000000000 --- a/extensions/integration_testing/playwright_save copy.ts +++ /dev/null @@ -1,380 +0,0 @@ -import { chromium } from 'playwright'; -import { expect } from 'playwright/test'; -import * as path from 'path'; - -(async () => { - // Launch a browser instance - const browser = await chromium.launch({ headless: false }); // Set headless: false to see the browser actions - const context = await browser.newContext({ - permissions: ['camera'], - acceptDownloads: true - }); - const page = await context.newPage(); - - const wheelDelta = 20; - - // Navigate to 'Create' page - await page.goto('http://localhost:8602/'); - - const includedExtensions = [ - "PRG Microbit Robot", - "PRG Gizmo Robot", - "PRG Arduino Robot", - "Music", - "Pen", - "Video Sensing", - "Text to Speech", - "Translate", - "Makey Makey", - "micro:bit", - "LEGO MINDSTORMS EV3", - "LEGO BOOST", - "LEGO Education WeDo 2.0", - "Go Direct Force & Acceleration" - ]; - - // Wait for the editor to load - await page.waitForSelector('.blocklyWorkspace'); - - const addExtension = await page.$('[title="Add Extension"]'); - if (addExtension) { - await addExtension.click(); - const parentElement = await page.$('.library_library-scroll-grid_1jyXm.library_withFilterBar_26Opm'); - - if (parentElement) { - // Get the last child element of the parent element - // library-item_library-item_1DcMO - const childElements = await parentElement.$$('.library-item_library-item_1DcMO.library-item_featured-item_3V2-t.library-item_library-item-extension_3xus9'); // Adjust the selector to match the child element type - const elementsWithSpan: any = []; - - for (const child of childElements) { - const spanElement = await child.$('span:first-of-type'); - if (spanElement) { - const textContent = await child.textContent(); - if (includedExtensions.some(phrase => textContent?.includes(phrase))) { - elementsWithSpan.push(child); - } - } - } - var chosenElements: any = []; - for (const child of childElements) { - if (!elementsWithSpan.includes(child)) { - chosenElements.push(child); - } - } - - if (chosenElements[0]) { - console.log("Last child element found"); - const firstSpan = await chosenElements[0].$('span:first-of-type'); - - - // Perform actions with the last child element if needed - // Example: Click the last child element - - if (firstSpan) { - const spanText = await firstSpan.textContent(); - //const spanText = "Face Sensing"; - await chosenElements[0].click(); - - const elementLocator = page.locator(`.blocklyFlyoutLabelText:has-text("${spanText}")`); - await elementLocator.waitFor(); - // await page.waitForFunction(() => { - // const elements = Array.from(document.querySelectorAll('[transform^="translate("]')); - // for (const element of elements) { - // const transformValue = (element as any).getAttribute('transform'); - // if (transformValue && transformValue.includes(',8688)')) { - // return true; - // } - // } - // return false; - // }); - await page.waitForTimeout(1000); - - const hatElements = await page.$$(`[data-category="${spanText}"][data-shapes="hat"]`); - const stackElements = await page.$$(`[data-category="${spanText}"][data-shapes="stack"]`); - const elements = await page.$$(`[data-category="${spanText}"]:not([data-shapes="stack"]):not([data-shapes="hat"])`); - - const viewport = page.viewportSize(); - console.log("viewport"); - console.log(viewport); - - let scrollMax = 0; - let stackMax = 0; - - if (hatElements.length > 0) { - let xOffset = 300; - let scroll = 0; - for (const hatElement of hatElements) { - await hatElement.scrollIntoViewIfNeeded(); - const hatBoundingBox = await hatElement.boundingBox(); - if (hatBoundingBox) { - // scroll back up if needed - let yOffset = 450; - if (scroll > 0) { - scroll = 0; - await page.mouse.move(500, 300); - await page.mouse.wheel(-1*scroll, 0); - } - // move the block - await page.mouse.move(hatBoundingBox.x + hatBoundingBox.width / 2, hatBoundingBox.y + hatBoundingBox.height / 2); - await page.mouse.down(); - await page.mouse.move(xOffset+hatBoundingBox.width/2, yOffset); - await page.mouse.up(); - // add to the offset - yOffset += hatBoundingBox.height * 0.8; - - // loop through stack elements - for (const stackElement of stackElements) { - await stackElement.scrollIntoViewIfNeeded(); - const stackBoundingBox = await stackElement.boundingBox(); - if (stackBoundingBox) { - await page.mouse.move(stackBoundingBox.x + stackBoundingBox.width / 2, stackBoundingBox.y + stackBoundingBox.height / 2); - await page.mouse.down(); - await page.mouse.move(xOffset+stackBoundingBox.width / 2, yOffset); - await page.mouse.up(); - // add to the offset - yOffset += stackBoundingBox.height * 0.8; - - if (yOffset > 500) { // if you need to scroll - scroll = scroll + wheelDelta; - if (scroll > scrollMax) { - scrollMax = scroll; // record the max scroll - } - yOffset -= wheelDelta; // take away from offset - if (yOffset > stackMax) { - stackMax = yOffset; - } - await page.mouse.move(500, 300); - await page.mouse.wheel(0, wheelDelta); - } else { - if (yOffset > stackMax) { - stackMax = yOffset; - } - } - } - } - xOffset = xOffset + 50; - // move back to the original position - if (scroll > 0) { - await page.mouse.move(500, 300); - await page.mouse.wheel(0, -1*scroll); - scroll = 0; - } - } else { - console.log("Failed to get bounding box for the hat element"); - } - } - - } else { - let yOffset = 450; - let scroll = 0; - let tempScroll = 0; - // make one stack of stack elements - for (const element of stackElements) { - if (tempScroll > 0) { - await page.mouse.move(100, 100); - await page.mouse.wheel(0, -1*tempScroll); - tempScroll = 0; - } - const boundingBox1 = await element.boundingBox(); - // check if we have to scroll down to find the element - if (boundingBox1) { - let found = false; - - while (!found) { - try { - await element.hover({ timeout: 1000 }); - found = true; - console.log('Successfully hovered over the element.'); - } catch (error) { - await page.mouse.move(100, 100); - await page.mouse.wheel(0, boundingBox1.height); - tempScroll = tempScroll + boundingBox1.height; - } - } - - } - const boundingBox = await element.boundingBox(); - if (boundingBox) { - await page.mouse.move(boundingBox.x + boundingBox.width / 2, boundingBox.y + boundingBox.height / 2); - await page.mouse.down(); - await page.mouse.move(400+boundingBox.width / 2, yOffset); - await page.mouse.up(); - - yOffset += boundingBox.height*0.8; - console.log("getting y offset ", yOffset); - if (yOffset > 500) { - scroll = scroll + boundingBox.height*0.8; - if (scroll > scrollMax) { - scrollMax = scroll; - } - - yOffset -= boundingBox.height*0.8; - if (yOffset > stackMax) { - console.log("new y offset ", yOffset); - stackMax = yOffset; - } - await page.mouse.move(500, 300); - await page.mouse.wheel(0, boundingBox.height*0.8); - } else { - if (yOffset > stackMax) { - stackMax = yOffset; - } - } - await page.waitForTimeout(1000); - } else { - console.log("Failed to get bounding box for the element"); - } - - } - //move the mouse back to the original scroll position - if (scroll > 0) { - await page.mouse.move(500, 300); - await page.mouse.wheel(0, -1*scroll); - scroll = 0; - } - if (tempScroll > 0) { - await page.mouse.move(100, 100); - await page.mouse.wheel(0, -1*tempScroll); - tempScroll = 0; - } - } - // find where all the input elements are - const firstBlocklyBlockCanvas = await page.$('.blocklyBlockCanvas'); - const inputElements = firstBlocklyBlockCanvas - ? await firstBlocklyBlockCanvas.$$('.blocklyEditableText:not([data-argument-type="dropdown"]):not(:has(.blocklyDropdownText))') - : []; - - // Find out where we can place in the input elements in the stacks - var maxX = 400; - var maxY = stackMax; // stack max holds where the last position is with scroll - console.log("max y is", maxY); - var inputBoxes: any = []; - if (inputElements.length > 0) { - for (const element of inputElements) { - const boundingBox = await element.boundingBox(); - if (boundingBox) { - inputBoxes.push(boundingBox); - } - } - } else { - inputBoxes = [{ - x: 400, - y: stackMax, - width: 5, - height: 5 - }] - } - - let inputIndex = 0; - let inputOffset = 0; - let scrollDown = false; - - // Now loop through the rest of the elements - let tempScroll = 0; - for (const element of elements) { - if (tempScroll > 0) { - await page.mouse.move(100, 100); - await page.mouse.wheel(0, -1*tempScroll); - tempScroll = 0; - } - const boundingBox1 = await element.boundingBox(); - // check if we have to scroll down to find the element - if (boundingBox1) { - let found = false; - let tempScroll = 0; - while (!found) { - try { - await element.hover({ timeout: 1000 }); - found = true; - console.log('Successfully hovered over the element.'); - } catch (error) { - await page.mouse.move(100, 100); - await page.mouse.wheel(0, boundingBox1.height); - tempScroll = tempScroll + boundingBox1.height; - } - } - - } - - const boundingBox = await element.boundingBox(); - if (boundingBox) { - var moveX; - var moveY; - let tempScroll2 = 0; - // If we still have input elements to go through - if (inputIndex >= inputBoxes.length) { - if (!scrollDown) { - await page.mouse.move(500, 300); - await page.mouse.wheel(0, scrollMax); - console.log("scrolled down by, ", scrollMax); - scrollDown = true; - } - inputOffset = inputOffset + boundingBox.height*0.8; - moveX = maxX; - moveY = maxY + inputOffset; - if (moveY > 500) { - await page.mouse.move(500, 300); - await page.mouse.wheel(0, wheelDelta); - inputOffset = inputOffset - wheelDelta; - moveY = moveY - wheelDelta; - } - } else { - moveX = inputBoxes[inputIndex].x + boundingBox.width / 2; - moveY = inputBoxes[inputIndex].y - if (moveY > 500) { - await page.mouse.move(500, 300); - tempScroll2 = moveY-500 - await page.mouse.wheel(0, tempScroll2); - maxY = maxY - tempScroll2; - } - } - - await page.mouse.move(boundingBox.x + boundingBox.width / 2, boundingBox.y + boundingBox.height / 2); - await page.mouse.down(); - await page.mouse.move(moveX, moveY); - await page.mouse.up(); - console.log(`Element dragged to the same spot: ${await element.textContent()}`); - - console.log(moveX); - console.log(moveY); - inputIndex = inputIndex + 1; - await page.waitForTimeout(1000); - if (tempScroll2 > 0) { - await page.mouse.move(500, 300); - await page.mouse.wheel(0, -1*tempScroll2); - } - - // Increment the yOffset for the next element - } else { - console.log("Failed to get bounding box for the element"); - } - } - } else { - console.log("No span elements found within the last child element"); - } - } else { - console.log("No child elements found"); - } - } else { - console.log("Parent element not found"); - } - } - - // Save the project (if you want to) - const downloadPath = path.resolve(__dirname, 'downloads'); - await page.click('text=File'); - await page.click('text=Save to your computer'); - const [download] = await Promise.all([ - page.waitForEvent('download'), // Wait for the download to start - ]); - - // Save the download to the specified path - await download.saveAs(path.join(downloadPath, await download.suggestedFilename())); - console.log(`File downloaded to: ${path.join(downloadPath, await download.suggestedFilename())}`); - await page.waitForTimeout(5000); - - - // Close the browser - await browser.close(); -})(); diff --git a/extensions/integration_testing/test_fn.ts b/extensions/integration_testing/test_fn.ts deleted file mode 100644 index e406a822f..000000000 --- a/extensions/integration_testing/test_fn.ts +++ /dev/null @@ -1,21 +0,0 @@ -type InputTuple = [number, string]; -type OutputTuple = [number, string]; - -function transformFunction(func: (x: number, y: number) => number[]): (inputs: InputTuple[]) => OutputTuple[] { - return (inputs: InputTuple[]) => { - return inputs.map(([value, name]) => { - const [x, y] = func(value, value); - return name === "input1" ? [y, name] : [x, name]; - }); - }; -} - -// Example usage: -const originalFunction = (x: number, y: number) => [y * 2, x * 2]; -const inputTuples: InputTuple[] = [ - [5, "input1"], - [10, "input2"] -]; - -const transformedFunction = transformFunction(originalFunction); -console.log(transformedFunction(inputTuples)); // Output: [[20, "input2"], [10, "input1"]]