UNPKG

ctrlshiftleft

Version:

AI-powered toolkit for embedding QA and security testing into development workflows

145 lines (134 loc) 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateSeleniumTest = generateSeleniumTest; /** * Generate a Selenium test file from test scenarios * @param scenarios Test scenarios to include * @param sourcePath Path to the source file (for documentation) * @returns Test template with content */ function generateSeleniumTest(scenarios, sourcePath) { const template = { content: '', format: 'selenium' }; // Generate imports and header template.content = ` const { Builder, By, Key, until } = require('selenium-webdriver'); const assert = require('assert'); /** * End-to-end tests generated for: ${sourcePath} * Generated by Ctrl.shift.left */ // Test suite implementation for the Ctrl.shift.left test runner module.exports = { async runTests(driver, options = {}) { let total = 0; let passed = 0; let failed = 0; let skipped = 0; // If driver not provided, create a new one const ownDriver = !driver; if (ownDriver) { driver = await new Builder().forBrowser('chrome').build(); } try { // Run all tests with the webdriver instance for (const testFn of Object.values(tests)) { total++; try { await testFn(driver); passed++; } catch (error) { failed++; console.error(error); } } } finally { // Close driver if we created it if (ownDriver) { await driver.quit(); } } return { total, passed, failed, skipped }; } }; // Test implementations const tests = { `; // Generate a test function for each scenario scenarios.forEach((scenario, index) => { const testName = scenario.id || `test${index + 1}`; const testFunction = ` // ${scenario.description} ${testName}: async (driver) => { // Test timeout jest.setTimeout(60000); // Test steps ${scenario.steps.map(step => ` // ${step}`).join('\n')} // Assertions ${scenario.assertions.map(assertion => { // Convert plain text assertions to code const assertionCode = convertAssertionToCode(assertion); return ` // ${assertion}\n ${assertionCode}`; }).join('\n\n')} }, `; template.content += testFunction; }); // Close the tests object template.content += `}; // Stand-alone test functions for direct usage with Selenium `; // Generate standalone functions for each test scenario scenarios.forEach((scenario, index) => { const testName = scenario.id || `test${index + 1}`; const standaloneTest = `async function ${testName}() { const driver = await new Builder().forBrowser('chrome').build(); try { // Test steps ${scenario.steps.map(step => ` // ${step}`).join('\n')} // Assertions ${scenario.assertions.map(assertion => { // Convert plain text assertions to code const assertionCode = convertAssertionToCode(assertion); return ` // ${assertion}\n ${assertionCode}`; }).join('\n\n')} } finally { await driver.quit(); } } // Run standalone test when this script is called directly if (require.main === module) { ${testName}().catch(console.error); } `; template.content += standaloneTest; }); return template; } /** * Convert a plain text assertion to Selenium code * Basic implementation - would need to be enhanced based on actual assertions */ function convertAssertionToCode(assertion) { // Simplistic conversion - would need more sophisticated parsing if (assertion.toLowerCase().includes('url')) { return `const url = await driver.getCurrentUrl();\nassert(url.includes('expected-url-part'), 'URL does not contain expected part');`; } else if (assertion.toLowerCase().includes('title')) { return `const title = await driver.getTitle();\nassert.strictEqual(title, 'Expected Page Title');`; } else if (assertion.toLowerCase().includes('visible') || assertion.toLowerCase().includes('see')) { return `const element = await driver.findElement(By.css('selector'));\nconst isDisplayed = await element.isDisplayed();\nassert(isDisplayed, 'Element should be visible');`; } else if (assertion.toLowerCase().includes('text')) { return `const element = await driver.findElement(By.css('selector'));\nconst text = await element.getText();\nassert(text.includes('expected text'), 'Text does not contain expected content');`; } else if (assertion.toLowerCase().includes('element')) { return `const elements = await driver.findElements(By.css('selector'));\nassert(elements.length > 0, 'Element should exist');`; } else { return `// TODO: Implement assertion for: ${assertion}`; } } //# sourceMappingURL=seleniumTemplate.js.map