Skip to content

Commit 4949b9e

Browse files
committed
Added_winston_logger
1 parent aa5d76c commit 4949b9e

File tree

5 files changed

+377
-21
lines changed

5 files changed

+377
-21
lines changed

CustomReporterConfig.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
import { Reporter, TestCase, TestError, TestResult, TestStep } from "@playwright/test/reporter";
2+
const winston = require(`winston`);
3+
4+
const console = new winston.transports.Console();
5+
const logger = winston.createLogger({
6+
level: 'info',
7+
format: winston.format.json(),
8+
transports: [
9+
// - Write all logs with importance level of `info` or less than it
10+
new winston.transports.File({ filename: 'logs/info.log', level: 'info' }),
11+
],
12+
});
13+
14+
// Writes logs to console
15+
logger.add(console);
216

317
export default class CustomReporterConfig implements Reporter {
418

519
onTestBegin(test: TestCase): void {
6-
console.log(`Test Case Started : ${test.title}`);
20+
logger.info(`Test Case Started : ${test.title}`);
721
}
822

923
onTestEnd(test: TestCase, result: TestResult): void {
10-
console.log(`Test Case Completed : ${test.title} Status : ${result.status}`);
24+
logger.info(`Test Case Completed : ${test.title} Status : ${result.status}`);
1125
}
1226

1327
onStepBegin(test: TestCase, result: TestResult, step: TestStep): void {
1428
if (step.category === `test.step`) {
15-
console.log(`Executing Step : ${step.title}`);
29+
logger.info(`Executing Step : ${step.title}`);
1630
}
1731
}
1832

1933
onError(error: TestError): void {
20-
console.log(error.message);
34+
logger.error(error.message);
2135
}
2236
}

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ await page.routeFromHAR('har/personalInfo.har',{update:false});
200200
```
201201
where `update:false` means to use the existing HAR from from the path given in first paraeter `har/personalInfo.har`, to see this in action you can turn off your internet and run the script, complete webpage is mocked up along with assertions on the browser of your choice this is done using the Network Replay feature and by using our recorded HAR file.
202202
We can use this feature when webpage is down for some reason and we want to test some scenarios.
203+
22. Logging is implemented in `CustomReporterConfig.ts` using winston logger.
204+
205+
First we have to create a logger object using winston.createLogger and then provid the configuration you need.
206+
First argument is "level" for which i have provided value as "info", in winston logger every logging level is provided with a numeric value, for info the numeric value is 2, so if we provide level as info then all the logs which are equal to or less than info level will be displayed. In our case logs with error(0) and warn(1) wil also be logged. For more info on logging refer below link
207+
`https://github.com/winstonjs/winston#logging`
208+
209+
Second we have to provide format for the log file generate I have provided format as json using ` winston.format.json()` because JSON is widely used. There are various levels like printf, simple, colorize which you can refer in below link
210+
`https://github.com/winstonjs/logform#formats`
211+
212+
Third part is transports which defines where the log file will be written. I have provided the location as `logs/info.log` in my case
213+
214+
Once logger object is created I have provided `logger.add(console);` which instructs logger to write the log files in console as well.
215+
216+
Once logger object is created you can use this instead of console.log in your framework and these logs will be written both in your console and log file.
203217

204218
## Reports
205219

lib/WebActions.ts

-15
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ export class WebActions {
2626
return CryptoJS.AES.decrypt(testConfig.password, key).toString(CryptoJS.enc.Utf8);
2727
}
2828

29-
async waitForElementAttached(locator: string): Promise<void> {
30-
await this.page.waitForSelector(locator);
31-
}
32-
3329
async waitForPageNavigation(event: string): Promise<void> {
3430
switch (event.toLowerCase()) {
3531
case `networkidle`:
@@ -50,12 +46,10 @@ export class WebActions {
5046
}
5147

5248
async clickElement(locator: string): Promise<void> {
53-
await this.waitForElementAttached(locator);
5449
await this.page.click(locator);
5550
}
5651

5752
async clickElementJS(locator: string): Promise<void> {
58-
await this.waitForElementAttached(locator);
5953
await this.page.$eval(locator, (element: HTMLElement) => element.click());
6054
}
6155

@@ -67,24 +61,19 @@ export class WebActions {
6761
}
6862

6963
async enterElementText(locator: string, text: string): Promise<void> {
70-
await this.waitForElementAttached(locator);
7164
await this.page.fill(locator, text);
7265
}
7366

7467
async dragAndDrop(dragElementLocator: string, dropElementLocator: string): Promise<void> {
75-
await this.waitForElementAttached(dragElementLocator);
76-
await this.waitForElementAttached(dropElementLocator);
7768
await this.page.dragAndDrop(dragElementLocator, dropElementLocator);
7869
}
7970

8071
async selectOptionFromDropdown(locator: string, option: string): Promise<void> {
81-
await this.waitForElementAttached(locator);
8272
const selectDropDownLocator = await this.page.$(locator);
8373
selectDropDownLocator.type(option);
8474
}
8575

8676
async getTextFromWebElements(locator: string): Promise<string[]> {
87-
await this.waitForElementAttached(locator);
8877
return this.page.$$eval(locator, elements => elements.map(item => item.textContent.trim()));
8978
}
9079

@@ -121,7 +110,6 @@ export class WebActions {
121110
}
122111

123112
async verifyElementText(locator: string, text: string): Promise<void> {
124-
await this.waitForElementAttached(locator);
125113
const textValue = await this.page.textContent(locator);
126114
expect(textValue.trim()).toBe(text);
127115
}
@@ -139,18 +127,15 @@ export class WebActions {
139127
}
140128

141129
async verifyElementContainsText(locator: string, text: string): Promise<void> {
142-
await this.waitForElementAttached(locator);
143130
await expect(this.page.locator(locator)).toContainText(text);
144131
}
145132

146133
async verifyJSElementValue(locator: string, text: string): Promise<void> {
147-
await this.waitForElementAttached(locator);
148134
const textValue = await this.page.$eval(locator, (element: HTMLInputElement) => element.value);
149135
expect(textValue.trim()).toBe(text);
150136
}
151137

152138
async verifyElementAttribute(locator: string, attribute: string, value: string): Promise<void> {
153-
await this.waitForElementAttached(locator);
154139
const textValue = await this.page.getAttribute(locator, attribute);
155140
expect(textValue.trim()).toBe(value);
156141
}

0 commit comments

Comments
 (0)