Refactor test scripts by removing unused files, updating test commands, and adding new functionality for window switching and variable extraction
This commit is contained in:
102
src/executor.js
102
src/executor.js
@@ -15,6 +15,8 @@ class TestExecutor {
|
||||
this.disableAnimations = options.disableAnimations !== false; // Default to disable animations
|
||||
this.fullPageScreenshots = options.fullPageScreenshots || false; // Default to viewport screenshots
|
||||
this.enableScreenshots = options.enableScreenshots !== false; // Default to enable screenshots
|
||||
this.variables = {}; // Store extracted variables
|
||||
|
||||
this.profiles = {
|
||||
Chrome: {
|
||||
viewport: { width: 1280, height: 720 },
|
||||
@@ -148,7 +150,8 @@ class TestExecutor {
|
||||
break;
|
||||
|
||||
case 'open':
|
||||
await this.page.goto(command.url, { waitUntil: 'networkidle' });
|
||||
const resolvedUrl = this.resolveEnvironmentVariables(command.url);
|
||||
await this.page.goto(resolvedUrl, { waitUntil: 'networkidle' });
|
||||
// Small delay to ensure page is stable
|
||||
if (!this.headless) {
|
||||
await this.page.waitForTimeout(200);
|
||||
@@ -203,6 +206,18 @@ class TestExecutor {
|
||||
await this.sleep(command);
|
||||
break;
|
||||
|
||||
case 'switchToNewWindow':
|
||||
await this.switchToNewWindow();
|
||||
break;
|
||||
|
||||
case 'switchToTab':
|
||||
await this.switchToTab(command);
|
||||
break;
|
||||
|
||||
case 'extract':
|
||||
await this.extractToVariable(command);
|
||||
break;
|
||||
|
||||
default:
|
||||
console.warn(`Unknown command type: ${command.type}`);
|
||||
}
|
||||
@@ -648,6 +663,75 @@ class TestExecutor {
|
||||
});
|
||||
}
|
||||
|
||||
async switchToNewWindow() {
|
||||
console.log('🔄 Switching to new tab...');
|
||||
|
||||
// Get all current pages/tabs
|
||||
const pages = this.context.pages();
|
||||
|
||||
if (pages.length < 2) {
|
||||
throw new Error('No additional tabs found to switch to');
|
||||
}
|
||||
|
||||
// Switch to the last (most recently opened) tab
|
||||
const newTab = pages[pages.length - 1];
|
||||
|
||||
// Wait for the tab to be ready
|
||||
await newTab.waitForLoadState('networkidle');
|
||||
|
||||
// Switch to the new tab
|
||||
this.page = newTab;
|
||||
|
||||
console.log('🔄 Switched to new tab');
|
||||
}
|
||||
|
||||
async switchToTab(command) {
|
||||
const tabIndex = command.tabIndex;
|
||||
if (tabIndex === undefined || tabIndex < 0 || tabIndex >= this.context.pages().length) {
|
||||
throw new Error(`Invalid tab index: ${tabIndex}`);
|
||||
}
|
||||
|
||||
const targetPage = this.context.pages()[tabIndex];
|
||||
this.page = targetPage;
|
||||
console.log(`🔄 Switched to tab index ${tabIndex}`);
|
||||
}
|
||||
|
||||
async extractToVariable(command) {
|
||||
const selector = this.buildSelector(command);
|
||||
const attribute = command.attribute;
|
||||
const variableName = command.variableName;
|
||||
|
||||
if (!variableName) {
|
||||
throw new Error('extract command requires a variableName');
|
||||
}
|
||||
|
||||
let value = '';
|
||||
try {
|
||||
const element = await this.page.locator(selector).first();
|
||||
|
||||
if (attribute === 'text') {
|
||||
value = await element.textContent();
|
||||
} else if (attribute === 'innerHTML') {
|
||||
value = await element.innerHTML();
|
||||
} else {
|
||||
// Extract attribute value
|
||||
value = await element.getAttribute(attribute);
|
||||
}
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
throw new Error(`Attribute "${attribute}" not found on element`);
|
||||
}
|
||||
|
||||
console.log(`📋 Extracted ${attribute} from "${selector}" to variable "${variableName}": "${value}"`);
|
||||
} catch (e) {
|
||||
console.warn(`Could not extract ${attribute} from "${selector}" to variable "${variableName}":`, e.message);
|
||||
throw new Error(`Could not extract ${attribute} from "${selector}" to variable "${variableName}"`);
|
||||
}
|
||||
|
||||
this.variables[variableName] = value;
|
||||
console.log(`✅ Variable "${variableName}" set to: "${value}"`);
|
||||
}
|
||||
|
||||
formatCommandForOutput(command) {
|
||||
switch (command.type) {
|
||||
case 'use':
|
||||
@@ -671,6 +755,12 @@ class TestExecutor {
|
||||
return `break "${command.message}"`;
|
||||
case 'sleep':
|
||||
return command.message ? `sleep ${command.milliseconds} "${command.message}"` : `sleep ${command.milliseconds}`;
|
||||
case 'switchToNewWindow':
|
||||
return 'switchToNewWindow';
|
||||
case 'switchToTab':
|
||||
return `switchToTab tabIndex=${command.tabIndex}`;
|
||||
case 'extract':
|
||||
return `extract ${this.formatSelector(command)} variableName="${command.variableName}"`;
|
||||
default:
|
||||
return `${command.type} ${JSON.stringify(command)}`;
|
||||
}
|
||||
@@ -1141,11 +1231,17 @@ class TestExecutor {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Replace $VARIABLE or ${VARIABLE} with environment variable values
|
||||
// Replace $VARIABLE or ${VARIABLE} with environment variable values or stored variables
|
||||
return value.replace(/\$\{?([A-Z_][A-Z0-9_]*)\}?/g, (match, varName) => {
|
||||
// First check stored variables
|
||||
if (this.variables[varName] !== undefined) {
|
||||
return this.variables[varName];
|
||||
}
|
||||
|
||||
// Then check environment variables
|
||||
const envValue = process.env[varName];
|
||||
if (envValue === undefined) {
|
||||
console.warn(`Warning: Environment variable ${varName} is not defined`);
|
||||
console.warn(`Warning: Variable ${varName} is not defined in stored variables or environment`);
|
||||
return match; // Return original if not found
|
||||
}
|
||||
return envValue;
|
||||
|
||||
Reference in New Issue
Block a user