Skip to content

Commit

Permalink
feat: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkenos committed Dec 6, 2023
1 parent c813018 commit d3396b5
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ BROWSER=chromium
TIMEOUT=8000
PARALLEL=5
HEADLESS=true
LOG_LEVEL=info
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"HEADLESS": "false",
"PARALLEL": "${input:parallel}",
"TAGS": "${input:tags}",
"PATHS": "${file}"
"PATHS": "${file}",
"LOG_LEVEL": "debug"
}
}
],
Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"@cucumber/cucumber": "^10.0.1",
"@playwright/test": "^1.40.0",
"@types/fs-extra": "^11.0.4",
"@wdio/logger": "^7.26.0",
"callsites": "^3.1.0",
"change-case": "^4.1.2",
"dotenv": "^16.3.1",
Expand Down
3 changes: 2 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function configure(overrides) {
const debug = process.env.DEBUG === "true" || overrides?.debug || false;
const downloadsDir = path.join(baseDir, process.env.DOWNLOADS_DIR || overrides?.downloadsDir || "downloads/");
const headless = process.env.HEADLESS === "true" || overrides?.headless || false;
const logLevel = process.env.LOG_LEVEL || overrides?.logLevel || "error";
const pages = (overrides?.pages || ["fixtures/pages/**/*.page.ts"]).map(i => path.join(baseDir, i));
const resultsDir = path.join(baseDir, process.env.RESULTS_DIR || overrides?.resultsDir || "results/");
const snapshotsDir = path.join(baseDir, process.env.SNAPSHOTS_DIR || overrides?.snapshotsDir || "snapshots/");
Expand All @@ -43,7 +44,7 @@ function configure(overrides) {
fs.removeSync(snapshots[key].diffDir);
fs.mkdirsSync(snapshots[key].expectedDir);
});
const custom = { baseDir, baseURL, browser, browserOptions, contextOptions, debug, downloadsDir, headless, pages, resultsDir, snapshots, timeout };
const custom = { baseDir, baseURL, browser, browserOptions, contextOptions, debug, downloadsDir, headless, logLevel, pages, resultsDir, snapshots, timeout };

// cucumber options defaults
const config = {
Expand Down
2 changes: 2 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface Config extends IConfiguration {
/** Custom: Directory to store browser downloads in, relative to the config file */
downloadsDir: string;
headless: boolean;
/** Custom: Level of logging verbosity */
logLevel: "trace" | "debug" | "info" | "warn" | "error" | "silent";
/** Custom: Array of globs pointing to your page object files, relative to the config file */
pages: string[];
/** Custom: Directory to store the reports in, relative to the config file */
Expand Down
8 changes: 8 additions & 0 deletions src/core/gherkin/world.def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import {
After,
AfterStep,
Before,
BeforeStep,
ITestStepHookParameter,
setDefaultTimeout,
setWorldConstructor,
Status
} from "@cucumber/cucumber";
import { World as This } from "../world";

import chalk from "chalk";

setDefaultTimeout(process.env.DEBUG === "true" ? -1 : undefined);
setWorldConstructor(This);

Expand All @@ -20,6 +23,11 @@ Before({}, async function(this: This) {
this.page = await this.context.newPage();
});

BeforeStep({}, async function(this: This, params: ITestStepHookParameter) {
const { pickleStep } = params;
this.logger.debug(`${chalk.yellow("GHERKIN")} ${chalk.green.dim.bold(`${pickleStep.type}: `)}${chalk.green.dim(pickleStep.text)}`);
});

AfterStep({}, async function(this: This, params: ITestStepHookParameter) {
const { result, testStepId } = params;

Expand Down
2 changes: 2 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import log from "@wdio/logger";
export * from "./page-object";
export * from "./world";
export const logger = log("kyoko");
10 changes: 6 additions & 4 deletions src/core/page-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import type { World } from "./world";
import type { BrowserContext, Page } from "@commands/types";

export class PageObject<ParametersType = any> {
readonly attach: World["attach"];
parameters: ParametersType;
context: BrowserContext;
page: Page;
protected readonly attach: World["attach"];
protected readonly logger: World["logger"];
protected parameters: ParametersType;
protected context: BrowserContext;
protected page: Page;
url: string;
title: string;

constructor(world: World) {
this.attach = world.attach;
this.logger = world.logger;
this.parameters = world.parameters;
this.context = world.context;
this.page = world.page;
Expand Down
12 changes: 12 additions & 0 deletions src/core/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { World as CucumberWorld } from "@cucumber/cucumber";
import { BrowserContext as BrowserContextClass } from "@commands/context/context";
import { PageObject } from "./page-object";

import log from "@wdio/logger";

import type { IWorldOptions } from "@cucumber/cucumber";
import type { BrowserContext, Locator, Page } from "@commands/types";
import type { Config } from "@config/types";
Expand All @@ -16,6 +18,7 @@ export abstract class World<ParametersType = any> extends CucumberWorld<Paramete
private pageObjects: string[];
private pageObject: PageObject;
context: BrowserContext;
logger: ReturnType<typeof log>;
page: Page;
config: Config;

Expand All @@ -24,10 +27,18 @@ export abstract class World<ParametersType = any> extends CucumberWorld<Paramete
const { config, ...parameters } = options.parameters;
super({ ...options, parameters });
this.config = config;
this.loadLogger();
this.loadPageObjects();
this.loadCommands();
}

private loadLogger() {
const suffix = process.env.CUCUMBER_PARALLEL === "true" ? `[${process.env.CUCUMBER_WORKER_ID}]` : "";
this.logger = log(`kyoko${suffix}`);
this.logger.setLevel(this.config.logLevel);
this.logger.info("Instatiating cucumber world class...");
}

private loadPageObjects() {
this.pageObjects = files.fromGlob(this.config.pages);
}
Expand All @@ -36,6 +47,7 @@ export abstract class World<ParametersType = any> extends CucumberWorld<Paramete
files.fromGlob([path.join(path.dirname(__dirname), "commands/**/command/*.js")]).filter(Boolean).forEach(file => require(file));
}


findPageObject(page: string, persist = false) {
const file = this.pageObjects.find(i => path.basename(i).split(".")[0].toLowerCase() === page.toLowerCase());

Expand Down

0 comments on commit d3396b5

Please sign in to comment.