microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.85k stars 3.66k forks source link

[BUG] PW print only logs on reporter listener file but not for others #14436

Closed hungdao-testing closed 2 years ago

hungdao-testing commented 2 years ago

Context:

Code Snippet log_utils.ts

import log4js from 'log4js';

log4js.configure({
  appenders: {
    console: {
      type: 'console',
      layout: {
        type: 'pattern',
        pattern: ' %d{yyyy-MM-dd hh:mm:ss} - %f{1} - %[ %m %]'
      }
    },
    app: { type: 'file', filename: 'application.log' }
  },
  categories: {
    default: { appenders: ['console'], level: 'trace', enableCallStack: true }
    // application: { appenders: ['app'], level: 'trace' }
  }
});

const logger =  log4js.getLogger("console");
export default logger;

test_listener.ts

import { FullConfig, Reporter, Suite, TestCase, TestResult, TestStep } from '@playwright/test/reporter';
import logger from "@utils/log_utils";

class TestListener implements Reporter {
  onBegin(config: FullConfig, suite: Suite) {
    logger.info(`Starting the run with ${suite.allTests().length} tests `);
  }

  onTestBegin(test: TestCase, result: TestResult) {
    logger.info(`[TEST_START] ${test.parent.title} - ${test.title}`);
  }

  onTestEnd(test: TestCase, result: TestResult) {
    logger.info(`[TEST_RESULT: ${result.status.toUpperCase()}] ${test.title}`);
  }
}
export default TestListener;

global_hook

import logger from "@utils/log_utils";

async function globalSetup() {
  // const logger = log4js.getLogger('console');
  logger.info(`Start setting up the ${process.env.NODE_ENV} environment`);
}

export default globalSetup;
import { pgPool } from '../../main/db/server';
import { FullConfig } from '@playwright/test';
import logger from "@utils/log_utils";

async function globalTeardown(config: FullConfig) {
  // const logger = log4js.getLogger('console');

  logger.info('Start tearing down the test setup');

  await pgPool.end().catch((e) => logger.error('Error: ', e));
  logger.info('Stop connecting to DB server');
}

export default globalTeardown;

test file

for (const eligbleData of enterprises) {
  test.describe.parallel('Onboarding Service', () => {
    const { title, enterprise, onboardingResult } = eligbleData;
    let requestCtx: APIRequestContext;
    let authenService: AuthenticationService;

    test.beforeEach(async ({ request }) => {
      authenService = new AuthenticationService(request, enterprise.uuid);
      authenService.loginToApp();

      logger.info("Before Each --- Authentication")
    });

    test.afterEach(async () => {
      await ActivationHandler.deletePendingRequest(requestCtx, id);
    });

    test(`Client could submit activation request in case ${title}`, async ({}) => {
      const clientHandler = ActivationHandler.doOnboardingBy('client', requestCtx, id);
    });
  });
}

playwright.config.ts

import { PlaywrightTestConfig } from "@playwright/test";

const config: PlaywrightTestConfig = {
  testMatch: /.*\.spec.ts/,
  globalSetup: './src/tests/hooks/global_setup',
  globalTeardown: './src/tests/hooks/global_teardown',
  workers: 2,
  reporter: [
    ['./src/tests/hooks/test_listener'],
  ],
  testDir: "./src/tests",
  projects: [
    {
      name: "api",
      testDir: "./src/tests/rest_api",
      timeout: 60000,
      use: {
      },
    },
    {
      name: "ui",
      testDir: "./src/tests/ui",
      timeout: 120000,
      use: {
      },
    }
  ],
};
export default config;

Describe the bug

When the test is triggered, only the defined log on the test_listener file and the global_hook files run, but not for the spec and the other files (as authentication file,...)

Here is the output, you guys could see there is no lines related to the test file and the others image

yury-s commented 2 years ago

You only see logs from the test runner process, all tests run in separate worker processes and if you want to see their output printed to the runner console you'll need to plumb it manually via onStdOut and onStdErr listener methods:

  onStdOut(chunk: string | Buffer, test: void | TestCase, result: void | TestResult): void {
    console.log(`onStdOut: ${chunk}`);
  }
  onStdErr(chunk: string | Buffer, test: void | TestCase, result: void | TestResult): void {      
    console.log(`onStdErr: ${chunk}`);
  }

File appender on the other hand should work just fine as both test runner and worker processes can write to it.