Understand Playwright Config File
Playwright Testing

Understand Playwright Config File

Playwright is nothing without playwright.config file, this config file contains all the configurations to drive the complete playwright test development and test execution.


playwright by testers talk


Here is a sample playwright.config file, let's explore each configuration.


// @ts-check
const { defineConfig, devices } = require('@playwright/test');

// Read environment variables from file.



/**
 * @see more at https://bit.ly/playwright-tutorial-automation-testing
 */
module.exports = defineConfig({
  // test timeout
  timeout: 5 * 60 * 1000,
  expect: {
    timeout: 2 * 60 * 1000
  },
  testDir: './tests',
  /* Run tests in files in parallel */
  fullyParallel: false,
  /* Fail the build on CI if you accidentally left test.only in the source code. */
  forbidOnly: !!process.env.CI,
  /* Retry on CI only */
  retries: process.env.CI ? 2 : 0,
  /* Opt out of parallel tests on CI. */
  workers: process.env.CI ? 1 : 1,
  // Reporter
  reporter:[
    ['html'],
   // ['allure-playwright'],
    ['junit', { outputFile: 'test-results/e2e-junit-results.xml' }],
    ],

  use: {
    /* Base URL to use in actions like `await page.goto('/')`. */
    // baseURL: 'https://127.0.0.1:3000',

    // launchOptions: {
    //   args: ["--start-fullscreen"]
    // },

    // video, screenshot, headless mode
    video:'on',
    screenshot: 'on',
    headless : true,

    // custom attribute
    testIdAttribute: 'autocomplete',

    // Collect trace when retrying the failed test
    trace: 'on',
  },

  /* Configure projects for major browsers */
  projects: [
    // {
    //   name: 'chromium',
    //   use: { ...devices['Desktop Chrome'] },
    // },

    // {
    //   name: 'firefox',
    //   use: { ...devices['Desktop Firefox'] },
    // },

    // {
    //   name: 'webkit',
    //   use: { ...devices['Desktop Safari'] },
    // },

    /* Test against mobile viewports. */
    // {
    //   name: 'Mobile Chrome',
    //   use: { ...devices['Pixel 5'] },
    // },
    // {
    //   name: 'Mobile Safari',
    //   use: { ...devices['iPhone 12'] },
    // },

    /* Test against branded browsers. */
    // {
    //   name: 'Microsoft Edge',
    //   use: { ...devices['Desktop Edge'], channel: 'msedge' },
    // },
    {
      name: 'Google Chrome',
      use: { ...devices['Desktop Chrome'], channel: 'chrome',
     },
    },
  ],

  /* Run your local dev server before starting the tests */
  // webServer: {
  //   command: 'npm run start',
  //   url: 'https://127.0.0.1:3000',
  //   reuseExistingServer: !process.env.CI,
  // },
});        


  1. Below statement helps to read environment variables from .env file, before using below line of code you need to install pluging using command - npm install dotenv --save

require('dotenv').config();        

2. Test execution timeout

 timeout: 5 * 60 * 1000,        

3. Pooling for webpage element

expect: {
    timeout: 2 * 60 * 1000
  },        

4. Playwright runs all the tests inside below folder.

  testDir: './tests',        

5. Run tests in files in parallel

   fullyParallel: false        

6. Retry test execution on CI only

Here 2 times retries for rerun automatically & 0 times in local test execution

retries: process.env.CI ? 2 : 0        

7. Based on below config runs parallel execution,

2 tests at a time it runs.

workers: process.env.CI ? 2 : 2

8. Reports

Default html report will be generated. if you want to other types reports we have to pass below configuration.

 reporter:[
    ['html'],
   // ['allure-playwright'],
    ['junit', { outputFile: 'test-results/e2e-junit-results.xml' }],
    ],        

9. If you add baseURL, browser opens up with specified URL before each test execution

     baseURL: 'https://127.0.0.1:3000'        

10. Video will be attached to the test report

     video:'on',        

11. Screeshnot will be attached to the test report

     screenshot: 'on',            

12. Runs all tests in headless

 headless : true,        

13. Custom attribute if any attribute is common across the project

 testIdAttribute: 'autocomplete',           

14. Collect trace when retrying the failed test & attaches it for playwright test report

 trace: 'on',        

15. At last, we have browsers to be selected if you are running in only on type browser otherwise need to pass project parameter while running test from command prompt.

Reference: https://playwright.dev/docs


===== Playwright Automation Full Courses =====

-> Playwright Full Course - https://youtube.com/playlist?list=PLUeDIlio4THEgPRVJRqZRS8uw8hhVNQCM&si=65JRAAG-SkkfphDm

-> Playwright API Testing Crash Course - https://youtube.com/playlist?list=PLUeDIlio4THF3rnYZ63qkbHwMTXiG67vz&si=hNd_ji_kXVmIAr_R

-> Playwright with Azure DeveOps Pipeline - https://youtube.com/playlist?list=PLUeDIlio4THG8irTXJn-Z02nYzmi6dUOF&si=3b4G5tvemlPy0--j

-> Playwright with CRM application testing - https://youtu.be/WwovRRp0f4o?si=w0rzJlUI9BhfP4KS


===== Playwright GitHub Repositories =====

-> Playwright Full Course - https://github.com/BakkappaN/PlaywrightTutorialFullCourse

-> Playwright API Testing Crash Course - https://github.com/BakkappaN/PlaywrightAPITestingTutorial

-> Playwright with Azure DeveOps Pipeline - https://github.com/BakkappaN/PlaywrightAzureDevopsPipeline

-> Playwright with CRM application testing -https://github.com/BakkappaN/MicrosoftD365CRMPlaywrightFramework

-> Playwright with JavaScript Framework [UI + API] - https://github.com/BakkappaN/PlaywrightBaseAutomationFramework

-> Playwright with TypeScript Framework [UI + API]- https://github.com/BakkappaN/Playwright-TypeScript-Framework

#playwright #apitesting #qa #softwaretesting #crm #javascript #typescript #ado #azuredeveops #framework #fullcourse #codegen #test #e2e #testing #automation #testerstalk #bakkappan #git #github



Nagaraj Ukkali

Mobile App Automation | Web automation | Playwright |Selenium | Core Java | Appium | Cucumber| CI-CD | Jenkins|Postman | TestCafe | Azure Pipeline|

7 个月

Good stuff

要查看或添加评论,请登录

Bakkappa N的更多文章