Skip to content

Commit

Permalink
update import
Browse files Browse the repository at this point in the history
  • Loading branch information
vid committed Jan 17, 2025
1 parent 8dddda8 commit 9d7b0a1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 105 deletions.
14 changes: 7 additions & 7 deletions modules/web-playwright/src/web-playwright.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Page, Response, Download } from 'playwright';

import { IHasOptions, OK, TNamed, TStepResult, AStepper, TWorld, TVStep, TAnyFixme } from '@haibun/core/build/lib/defs.js';
import { IHasOptions, OK, TNamed, TStepResult, AStepper, TWorld, TFeatureStep, TAnyFixme } from '@haibun/core/build/lib/defs.js';
import { WEB_PAGE, WEB_CONTROL } from '@haibun/core/build/lib/domain-types.js';
import { BrowserFactory, TBrowserFactoryOptions, TBrowserTypes } from './BrowserFactory.js';
import { actionNotOK, getStepperOption, boolOrError, intOrError, stringOrError, findStepperFromOption, sleep } from '@haibun/core/build/lib/util/index.js';
Expand Down Expand Up @@ -125,14 +125,14 @@ const WebPlaywright = class WebPlaywright extends AStepper implements IHasOption
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async onFailure(result: TStepResult, step?: TVStep): Promise<void | TTraceMessageContext> {
async onFailure(result: TStepResult, step?: TFeatureStep): Promise<void | TTraceMessageContext> {
if (this.bf?.hasPage(this.getWorld().tag, this.tab)) {
await this.captureFailureScreenshot('failure', 'onFailure', step);
}
}

// FIXME currently not executed
async nextStep(step: TVStep) {
async nextStep(step: TFeatureStep) {
const captureScreenshot = getStepperOption(this, 'STEP_CAPTURE_SCREENSHOT', this.getWorld().moduleOptions);
if (captureScreenshot) {
await this.captureRequestScreenshot('request', 'nextStep', step.seq);
Expand Down Expand Up @@ -479,7 +479,7 @@ const WebPlaywright = class WebPlaywright extends AStepper implements IHasOption
clickLink: {
// TODO: generalize modifier
gwta: 'click( with alt)? the link {name}',
action: async ({ name }: TNamed, featureStep: TVStep) => {
action: async ({ name }: TNamed, featureStep: TFeatureStep) => {
const modifier = featureStep.in.match(/ with alt /) ? { modifiers: ['Alt'] } : {};
const field = this.getWorld().shared.get(name) || name;
await this.withPage(async (page: Page) => await page.click(field, <TAnyFixme>modifier));
Expand Down Expand Up @@ -611,7 +611,7 @@ const WebPlaywright = class WebPlaywright extends AStepper implements IHasOption
},
takeScreenshot: {
gwta: 'take a screenshot',
action: async (notUsed, featureStep: TVStep) => {
action: async (notUsed, featureStep: TFeatureStep) => {
await this.captureScreenshot('request', 'action', featureStep);
return OK;
},
Expand Down Expand Up @@ -650,14 +650,14 @@ const WebPlaywright = class WebPlaywright extends AStepper implements IHasOption
newTab() {
this.tab = this.tab + 1;
}
async captureFailureScreenshot(event: 'failure', stage: TActionStage, step: TVStep) {
async captureFailureScreenshot(event: 'failure', stage: TActionStage, step: TFeatureStep) {
return await this.captureScreenshot(event, stage, { step });
}
async captureRequestScreenshot(event: 'request', stage: TActionStage, seq: number) {
return await this.captureScreenshot(event, stage, { seq });
}

async captureScreenshot(event: 'failure' | 'request', stage: TActionStage, details: { seq?: number, step?: TVStep }) {
async captureScreenshot(event: 'failure' | 'request', stage: TActionStage, details: { seq?: number, step?: TFeatureStep }) {
const loc = await this.getCaptureDir('image');
// FIXME shouldn't be fs dependant
const path = resolve(this.storage.fromLocation(EMediaTypes.image, loc, `${event}-${Date.now()}.png`));
Expand Down
196 changes: 98 additions & 98 deletions modules/web-server-express/src/web-server-stepper.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,115 @@
import { IHasOptions, OK, TWorld, TNamed, TOptions, AStepper, TVStep, } from '@haibun/core/build/lib/defs.js';
import { IHasOptions, OK, TWorld, TNamed, TOptions, AStepper, TFeatureStep } from '@haibun/core/build/lib/defs.js';
import { actionNotOK, getFromRuntime, getStepperOption, intOrError } from '@haibun/core/build/lib/util/index.js';
import { IWebServer, WEBSERVER, } from './defs.js';
import { IWebServer, WEBSERVER } from './defs.js';
import { ServerExpress, DEFAULT_PORT } from './server-express.js';
import { WEB_PAGE } from '@haibun/core/build/lib/domain-types.js';
import path from 'path';

const WebServerStepper = class WebServerStepper extends AStepper implements IHasOptions {
webserver: ServerExpress | undefined;
webserver: ServerExpress | undefined;

options = {
PORT: {
desc: `change web server port from ${DEFAULT_PORT}`,
parse: (port: string) => intOrError(port)
},
};
options = {
PORT: {
desc: `change web server port from ${DEFAULT_PORT}`,
parse: (port: string) => intOrError(port),
},
};

async setWorld(world: TWorld, steppers: AStepper[]) {
await super.setWorld(world, steppers);
// this.world.runtime[CHECK_LISTENER] = WebServerStepper.checkListener;
const port = parseInt(getStepperOption(this, 'PORT', world.moduleOptions)) || DEFAULT_PORT;
this.webserver = new ServerExpress(world.logger, path.join([process.cwd(), 'files'].join('/')), port);
world.runtime[WEBSERVER] = this.webserver;
}
async setWorld(world: TWorld, steppers: AStepper[]) {
await super.setWorld(world, steppers);
// this.world.runtime[CHECK_LISTENER] = WebServerStepper.checkListener;
const port = parseInt(getStepperOption(this, 'PORT', world.moduleOptions)) || DEFAULT_PORT;
this.webserver = new ServerExpress(world.logger, path.join([process.cwd(), 'files'].join('/')), port);
world.runtime[WEBSERVER] = this.webserver;
}

async close() {
await this.webserver?.close();
}
async close() {
await this.webserver?.close();
}

steps = {
thisURI: {
gwta: `a ${WEB_PAGE} at {where}`,
action: async ({ where }: TNamed, featureStep: TVStep) => {
const page = featureStep.source.name;
steps = {
thisURI: {
gwta: `a ${WEB_PAGE} at {where}`,
action: async ({ where }: TNamed, featureStep: TFeatureStep) => {
const page = featureStep.source.name;

const webserver = <IWebServer>getFromRuntime(this.getWorld().runtime, WEBSERVER);
await webserver.checkAddStaticFolder(page, where);
return OK;
},
},
/// generator
isListening: {
gwta: 'webserver is listening',
action: async () => {
await this.listen();
return OK;
},
},
showMounts: {
gwta: 'show mounts',
action: async () => {
const webserver = <IWebServer>getFromRuntime(this.getWorld().runtime, WEBSERVER);
const mounts = webserver.mounted;
this.getWorld().logger.info(`mounts: ${JSON.stringify(mounts, null, 2)}`);
return OK;
},
},
serveFilesAt: {
gwta: 'serve files at {where} from {loc}',
action: async ({ where, loc }: TNamed) => {
return await this.doServeFiles(where, loc).catch((e) => actionNotOK(e));
},
},
serveFiles: {
gwta: 'serve files from {loc}',
action: async ({ loc }: TNamed) => {
const r = await this.doServeFiles('/', loc).catch((e) => actionNotOK(e));
return r;
}
},
indexFiles: {
gwta: 'index files from {loc}',
action: async ({ loc }: TNamed) => {
const r = await this.doServeIndex('/', loc).catch((e) => actionNotOK(e));
return r;
}
},
indexFilesAt: {
gwta: 'index files at {where} from {loc}',
action: async ({ where, loc }: TNamed) => {
const r = await this.doServeIndex(where, loc).catch((e) => actionNotOK(e));
console.log('r', r)
return r;
}
}
}
async doServeIndex(where, loc) {
const ws: IWebServer = getFromRuntime(this.getWorld().runtime, WEBSERVER);
const res = ws.checkAddIndexFolder(loc, where);
if (res) {
return actionNotOK(`failed to add index folder ${loc} at ${where}: ${res}`);
}
await this.listen();
return OK;
}
async doServeFiles(where, loc) {
const ws: IWebServer = getFromRuntime(this.getWorld().runtime, WEBSERVER);
const res = ws.checkAddStaticFolder(loc, where);
if (res) {
return actionNotOK(`failed to add static folder ${loc} at ${where}: ${res}`, { topics: { failure: { summary: res } } });
}
await this.listen();
return OK;
}
async listen() {
await this.webserver.listen();
}
const webserver = <IWebServer>getFromRuntime(this.getWorld().runtime, WEBSERVER);
await webserver.checkAddStaticFolder(page, where);
return OK;
},
},
/// generator
isListening: {
gwta: 'webserver is listening',
action: async () => {
await this.listen();
return OK;
},
},
showMounts: {
gwta: 'show mounts',
action: async () => {
const webserver = <IWebServer>getFromRuntime(this.getWorld().runtime, WEBSERVER);
const mounts = webserver.mounted;
this.getWorld().logger.info(`mounts: ${JSON.stringify(mounts, null, 2)}`);
return OK;
},
},
serveFilesAt: {
gwta: 'serve files at {where} from {loc}',
action: async ({ where, loc }: TNamed) => {
return await this.doServeFiles(where, loc).catch((e) => actionNotOK(e));
},
},
serveFiles: {
gwta: 'serve files from {loc}',
action: async ({ loc }: TNamed) => {
const r = await this.doServeFiles('/', loc).catch((e) => actionNotOK(e));
return r;
},
},
indexFiles: {
gwta: 'index files from {loc}',
action: async ({ loc }: TNamed) => {
const r = await this.doServeIndex('/', loc).catch((e) => actionNotOK(e));
return r;
},
},
indexFilesAt: {
gwta: 'index files at {where} from {loc}',
action: async ({ where, loc }: TNamed) => {
const r = await this.doServeIndex(where, loc).catch((e) => actionNotOK(e));
console.log('r', r);
return r;
},
},
};
async doServeIndex(where, loc) {
const ws: IWebServer = getFromRuntime(this.getWorld().runtime, WEBSERVER);
const res = ws.checkAddIndexFolder(loc, where);
if (res) {
return actionNotOK(`failed to add index folder ${loc} at ${where}: ${res}`);
}
await this.listen();
return OK;
}
async doServeFiles(where, loc) {
const ws: IWebServer = getFromRuntime(this.getWorld().runtime, WEBSERVER);
const res = ws.checkAddStaticFolder(loc, where);
if (res) {
return actionNotOK(`failed to add static folder ${loc} at ${where}: ${res}`, { topics: { failure: { summary: res } } });
}
await this.listen();
return OK;
}
async listen() {
await this.webserver.listen();
}
};
export default WebServerStepper;

export type ICheckListener = (options: TOptions, webserver: IWebServer) => void;
export interface IWebServerStepper {
webserver: IWebServer;
close: () => void;
webserver: IWebServer;
close: () => void;
}

0 comments on commit 9d7b0a1

Please sign in to comment.