fix(automation): wire up real browser control and add stop functionality - Add initializeBrowserConnection() to DIContainer for CDP connection before automation - Implement stop-automation IPC handler with session state update - Add stopAutomation API to preload and stop button to UI - Implement executeStep() in BrowserDevToolsAdapter with all 18 workflow steps - Add getStepSelectors() and getStepName() to IRacingSelectorMap - Update MockAutomationEngineAdapter to use typed executeStep interface - Step 18 (TRACK_CONDITIONS) has safety stop - does NOT click checkout button

This commit is contained in:
2025-11-21 21:40:34 +01:00
parent a980a288ea
commit 92db83a3c4
7 changed files with 621 additions and 6 deletions

View File

@@ -7,8 +7,9 @@ import {
ClickResult,
WaitResult,
ModalResult,
AutomationResult,
} from '../../../packages/application/ports/AutomationResults';
import { IRacingSelectorMap, getStepSelectors, getStepName } from './selectors/IRacingSelectorMap';
import { IRacingSelectorMap, getStepSelectors, getStepName, isModalStep } from './selectors/IRacingSelectorMap';
/**
* Configuration for connecting to browser via Chrome DevTools Protocol
@@ -459,4 +460,514 @@ export class BrowserDevToolsAdapter implements IBrowserAutomation {
this.ensureConnected();
return await this.page!.evaluate(fn);
}
// ============== Step Execution ==============
/**
* Execute a complete workflow step with all required browser operations.
* Uses IRacingSelectorMap to locate elements and performs appropriate actions.
*
* Step workflow:
* 1. LOGIN - Skip (user pre-authenticated)
* 2. HOSTED_RACING - Navigate to hosted racing page
* 3. CREATE_RACE - Click create race button
* 4. RACE_INFORMATION - Fill session name, password, description
* 5. SERVER_DETAILS - Select server region, launch time
* 6. SET_ADMINS - Add admins (modal step)
* 7. TIME_LIMITS - Set practice/qualify/race times
* 8. SET_CARS - Configure car selection
* 9. ADD_CAR - Add cars (modal step)
* 10. SET_CAR_CLASSES - Configure car classes
* 11. SET_TRACK - Select track
* 12. ADD_TRACK - Add track (modal step)
* 13. TRACK_OPTIONS - Track configuration
* 14. TIME_OF_DAY - Set time of day
* 15. WEATHER - Weather settings
* 16. RACE_OPTIONS - Race rules and options
* 17. TEAM_DRIVING - Team settings
* 18. TRACK_CONDITIONS - Final review (SAFETY STOP - no checkout)
*
* @param stepId - The step to execute (1-18)
* @param config - Session configuration with form field values
* @returns AutomationResult with success/failure and metadata
*/
async executeStep(stepId: StepId, config: Record<string, unknown>): Promise<AutomationResult> {
this.ensureConnected();
const stepNumber = stepId.value;
const stepSelectors = getStepSelectors(stepNumber);
const stepName = getStepName(stepNumber);
try {
switch (stepNumber) {
case 1: // LOGIN - Skip, user already authenticated
return {
success: true,
metadata: {
skipped: true,
reason: 'User pre-authenticated',
step: stepName
}
};
case 2: // HOSTED_RACING - Navigate to hosted racing page
return await this.executeHostedRacingStep();
case 3: // CREATE_RACE - Click create race button
return await this.executeCreateRaceStep(stepSelectors);
case 4: // RACE_INFORMATION - Fill session details
return await this.executeRaceInformationStep(stepSelectors, config);
case 5: // SERVER_DETAILS - Configure server settings
return await this.executeServerDetailsStep(stepSelectors, config);
case 6: // SET_ADMINS - Add admins (modal step)
return await this.executeSetAdminsStep(stepSelectors, config);
case 7: // TIME_LIMITS - Configure time limits
return await this.executeTimeLimitsStep(stepSelectors, config);
case 8: // SET_CARS - Configure car selection
return await this.executeSetCarsStep(stepSelectors);
case 9: // ADD_CAR - Add cars (modal step)
return await this.executeAddCarStep(stepSelectors, config);
case 10: // SET_CAR_CLASSES - Configure car classes
return await this.executeSetCarClassesStep(stepSelectors, config);
case 11: // SET_TRACK - Select track
return await this.executeSetTrackStep(stepSelectors);
case 12: // ADD_TRACK - Add track (modal step)
return await this.executeAddTrackStep(stepSelectors, config);
case 13: // TRACK_OPTIONS - Track configuration
return await this.executeTrackOptionsStep(stepSelectors, config);
case 14: // TIME_OF_DAY - Set time of day
return await this.executeTimeOfDayStep(stepSelectors, config);
case 15: // WEATHER - Weather settings
return await this.executeWeatherStep(stepSelectors, config);
case 16: // RACE_OPTIONS - Race rules and options
return await this.executeRaceOptionsStep(stepSelectors, config);
case 17: // TEAM_DRIVING - Team settings
return await this.executeTeamDrivingStep(stepSelectors, config);
case 18: // TRACK_CONDITIONS - Final review (SAFETY STOP)
return await this.executeTrackConditionsStep(stepSelectors, config);
default:
return {
success: false,
error: `Unknown step: ${stepNumber}`,
metadata: { step: stepName }
};
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
success: false,
error: errorMessage,
metadata: { step: stepName }
};
}
}
// ============== Individual Step Implementations ==============
private async executeHostedRacingStep(): Promise<AutomationResult> {
const navResult = await this.navigateToPage(IRacingSelectorMap.urls.hostedRacing);
if (!navResult.success) {
return { success: false, error: navResult.error, metadata: { step: 'HOSTED_RACING' } };
}
// Wait for page to be ready
const stepSelectors = getStepSelectors(2);
if (stepSelectors?.container) {
await this.waitForElement(stepSelectors.container, this.config.defaultTimeout);
}
return {
success: true,
metadata: { step: 'HOSTED_RACING', url: IRacingSelectorMap.urls.hostedRacing }
};
}
private async executeCreateRaceStep(stepSelectors: ReturnType<typeof getStepSelectors>): Promise<AutomationResult> {
if (!stepSelectors?.buttons?.createRace) {
return { success: false, error: 'Create race button selector not defined', metadata: { step: 'CREATE_RACE' } };
}
const clickResult = await this.clickElement(stepSelectors.buttons.createRace);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'CREATE_RACE' } };
}
// Wait for wizard modal to appear
const waitResult = await this.waitForElement(IRacingSelectorMap.common.wizardContainer, this.config.defaultTimeout);
if (!waitResult.success) {
return { success: false, error: 'Wizard did not open', metadata: { step: 'CREATE_RACE' } };
}
return { success: true, metadata: { step: 'CREATE_RACE' } };
}
private async executeRaceInformationStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Fill session name if provided
if (config.sessionName && stepSelectors?.fields?.sessionName) {
const fillResult = await this.fillFormField(stepSelectors.fields.sessionName, config.sessionName as string);
if (!fillResult.success) {
return { success: false, error: fillResult.error, metadata: { step: 'RACE_INFORMATION', field: 'sessionName' } };
}
}
// Fill password if provided
if (config.password && stepSelectors?.fields?.password) {
const fillResult = await this.fillFormField(stepSelectors.fields.password, config.password as string);
if (!fillResult.success) {
return { success: false, error: fillResult.error, metadata: { step: 'RACE_INFORMATION', field: 'password' } };
}
}
// Fill description if provided
if (config.description && stepSelectors?.fields?.description) {
const fillResult = await this.fillFormField(stepSelectors.fields.description, config.description as string);
if (!fillResult.success) {
return { success: false, error: fillResult.error, metadata: { step: 'RACE_INFORMATION', field: 'description' } };
}
}
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'RACE_INFORMATION', action: 'next' } };
}
}
return { success: true, metadata: { step: 'RACE_INFORMATION' } };
}
private async executeServerDetailsStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Server region selection would require dropdown interaction
// For now, accept defaults unless specific configuration is provided
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'SERVER_DETAILS', action: 'next' } };
}
}
return { success: true, metadata: { step: 'SERVER_DETAILS' } };
}
private async executeSetAdminsStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Admin step is a modal step - check if we need to add admins
const adminIds = config.adminIds as string[] | undefined;
if (adminIds && adminIds.length > 0 && stepSelectors?.modal) {
// Open admin modal
if (stepSelectors.buttons?.addAdmin) {
const clickResult = await this.clickElement(stepSelectors.buttons.addAdmin);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'SET_ADMINS', action: 'openModal' } };
}
// Wait for modal to appear
await this.waitForElement(stepSelectors.modal.container, this.config.defaultTimeout);
// Search and select admins would require more complex interaction
// For now, close the modal
if (stepSelectors.modal.closeButton) {
await this.clickElement(stepSelectors.modal.closeButton);
}
}
}
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'SET_ADMINS', action: 'next' } };
}
}
return { success: true, metadata: { step: 'SET_ADMINS', isModalStep: true } };
}
private async executeTimeLimitsStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Fill practice length if provided
if (config.practiceLength && stepSelectors?.fields?.practiceLength) {
await this.fillFormField(stepSelectors.fields.practiceLength, String(config.practiceLength));
}
// Fill qualify length if provided
if (config.qualifyingLength && stepSelectors?.fields?.qualifyLength) {
await this.fillFormField(stepSelectors.fields.qualifyLength, String(config.qualifyingLength));
}
// Fill race length if provided
if (config.raceLength && stepSelectors?.fields?.raceLength) {
await this.fillFormField(stepSelectors.fields.raceLength, String(config.raceLength));
}
// Fill warmup length if provided
if (config.warmupLength && stepSelectors?.fields?.warmupLength) {
await this.fillFormField(stepSelectors.fields.warmupLength, String(config.warmupLength));
}
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'TIME_LIMITS', action: 'next' } };
}
}
return { success: true, metadata: { step: 'TIME_LIMITS' } };
}
private async executeSetCarsStep(stepSelectors: ReturnType<typeof getStepSelectors>): Promise<AutomationResult> {
// This step shows the car selection overview
// Actual car addition happens in step 9 (ADD_CAR modal)
// Click next button to proceed to track selection
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'SET_CARS', action: 'next' } };
}
}
return { success: true, metadata: { step: 'SET_CARS' } };
}
private async executeAddCarStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Add car is a modal step
const carIds = config.carIds as string[] | undefined;
if (carIds && carIds.length > 0 && stepSelectors?.modal) {
// Click add car button to open modal
const step8Selectors = getStepSelectors(8);
if (step8Selectors?.buttons?.addCar) {
const clickResult = await this.clickElement(step8Selectors.buttons.addCar);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'ADD_CAR', action: 'openModal' } };
}
// Wait for modal to appear
await this.waitForElement(stepSelectors.modal.container, this.config.defaultTimeout);
// Search for car would require typing in search field
// For each car, we would search and select
// For now, this is a placeholder for more complex car selection logic
// Close modal after selection (or if no action needed)
if (stepSelectors.modal.closeButton) {
await this.clickElement(stepSelectors.modal.closeButton);
}
}
}
return { success: true, metadata: { step: 'ADD_CAR', isModalStep: true, carCount: carIds?.length ?? 0 } };
}
private async executeSetCarClassesStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Car classes configuration - usually auto-configured based on selected cars
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'SET_CAR_CLASSES', action: 'next' } };
}
}
return { success: true, metadata: { step: 'SET_CAR_CLASSES' } };
}
private async executeSetTrackStep(stepSelectors: ReturnType<typeof getStepSelectors>): Promise<AutomationResult> {
// This step shows the track selection overview
// Actual track selection happens in step 12 (ADD_TRACK modal)
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'SET_TRACK', action: 'next' } };
}
}
return { success: true, metadata: { step: 'SET_TRACK' } };
}
private async executeAddTrackStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Add track is a modal step
const trackId = config.trackId as string | undefined;
if (trackId && stepSelectors?.modal) {
// Click add track button to open modal
const step11Selectors = getStepSelectors(11);
if (step11Selectors?.buttons?.addTrack) {
const clickResult = await this.clickElement(step11Selectors.buttons.addTrack);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'ADD_TRACK', action: 'openModal' } };
}
// Wait for modal to appear
await this.waitForElement(stepSelectors.modal.container, this.config.defaultTimeout);
// Search for track would require typing in search field
// For now, this is a placeholder for more complex track selection logic
// Close modal after selection (or if no action needed)
if (stepSelectors.modal.closeButton) {
await this.clickElement(stepSelectors.modal.closeButton);
}
}
}
return { success: true, metadata: { step: 'ADD_TRACK', isModalStep: true, trackId } };
}
private async executeTrackOptionsStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Track options like configuration, pit stalls etc.
// Accept defaults for now
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'TRACK_OPTIONS', action: 'next' } };
}
}
return { success: true, metadata: { step: 'TRACK_OPTIONS' } };
}
private async executeTimeOfDayStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Time of day configuration
// Accept defaults for now - time sliders are complex to interact with
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'TIME_OF_DAY', action: 'next' } };
}
}
return { success: true, metadata: { step: 'TIME_OF_DAY' } };
}
private async executeWeatherStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Weather configuration
// Accept defaults for now
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'WEATHER', action: 'next' } };
}
}
return { success: true, metadata: { step: 'WEATHER' } };
}
private async executeRaceOptionsStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Race options like max drivers, hardcore incidents, etc.
// Fill max drivers if provided
if (config.maxDrivers && stepSelectors?.fields?.maxDrivers) {
await this.fillFormField(stepSelectors.fields.maxDrivers, String(config.maxDrivers));
}
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'RACE_OPTIONS', action: 'next' } };
}
}
return { success: true, metadata: { step: 'RACE_OPTIONS' } };
}
private async executeTeamDrivingStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// Team driving configuration
// Accept defaults for now (usually disabled)
// Click next button to proceed
if (stepSelectors?.buttons?.next) {
const clickResult = await this.clickElement(stepSelectors.buttons.next);
if (!clickResult.success) {
return { success: false, error: clickResult.error, metadata: { step: 'TEAM_DRIVING', action: 'next' } };
}
}
return { success: true, metadata: { step: 'TEAM_DRIVING' } };
}
private async executeTrackConditionsStep(
stepSelectors: ReturnType<typeof getStepSelectors>,
config: Record<string, unknown>
): Promise<AutomationResult> {
// FINAL STEP - SAFETY STOP
// We fill track conditions but DO NOT click checkout button
// Track state selection would require dropdown interaction
// For now, accept defaults
return {
success: true,
metadata: {
step: 'TRACK_CONDITIONS',
safetyStop: true,
message: 'Automation stopped at final step. User must review configuration and click checkout manually.',
checkoutButtonSelector: IRacingSelectorMap.common.checkoutButton
}
};
}
}

View File

@@ -3,6 +3,7 @@ import { HostedSessionConfig } from '../../../packages/domain/entities/HostedSes
import { StepId } from '../../../packages/domain/value-objects/StepId';
import { IBrowserAutomation } from '../../../packages/application/ports/IBrowserAutomation';
import { ISessionRepository } from '../../../packages/application/ports/ISessionRepository';
import { getStepName } from './selectors/IRacingSelectorMap';
export class MockAutomationEngineAdapter implements IAutomationEngine {
private automationInterval: NodeJS.Timeout | null = null;
@@ -58,11 +59,16 @@ export class MockAutomationEngineAdapter implements IAutomationEngine {
const currentStep = session.currentStep;
// Execute current step (simulate browser automation)
if (typeof (this.browserAutomation as any).executeStep === 'function') {
await (this.browserAutomation as any).executeStep(currentStep, config);
// Execute current step using the browser automation
if (this.browserAutomation.executeStep) {
// Use real workflow automation with IRacingSelectorMap
const result = await this.browserAutomation.executeStep(currentStep, config as Record<string, unknown>);
if (!result.success) {
console.error(`Step ${currentStep.value} (${getStepName(currentStep.value)}) failed:`, result.error);
// Continue anyway for now - in production we might want to pause or retry
}
} else {
// Fallback to basic operations
// Fallback for adapters without executeStep (e.g., MockBrowserAutomationAdapter)
await this.browserAutomation.navigateToPage(`step-${currentStep.value}`);
}