Playwright on Github screenshots on failure not working

Viewed 1327

My playwright.config.ts includes:

use: {
  ...
  screenshot: 'only-on-failure',
}

and test failures result in screenshots being saved in /test-results when they fail locally. But when the tests fail when run in Github Actions, no screenshots are taken. So it's impossible for me to tell what's going wrong in my tests, which pass fine locally.

The only CI-specific parts of my config are:

/* 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,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: process.env.CI ? 'github' : 'list',

ETA: my action.yml attempts to upload the /test-results folder but it's always completely empty, as no screenshots were taken:

- uses: actions/upload-artifact@v2
  if: always()
  with:
    name: playwright-test-results
    path: test-results/
2 Answers

We can upload the test-results folder as a run artifact using GitHub's "upload-artifact" action post suite execution.

Alternatively, a step can be added in the workflow after the test run to upload the images to the desired location.

// playwright.config.ts
import { PlaywrightTestConfig, devices } from '@playwright/test';

const config: PlaywrightTestConfig = {
    forbidOnly: !!process.env.CI,
    retries: process.env.CI ? 2 : 1,
    use: {
        trace: 'on',
        screenshot: 'only-on-failure', // Capture screenshot after each test failure.
        video: 'retain-on-failure', //Record video only when retrying a test for the first time.
        headless: true,
        viewport: { width: 1280, height: 720 },
        baseURL: "https:xxxxx",
        launchOptions: {
            slowMo: 50,
            logger: {
                isEnabled: (name, severity) => name === 'browser',
                log: (name, severity, message, args) => console.log(`${name} ${message}`)
            }
        },
        actionTimeout: 10 * 1000,
        navigationTimeout: 30 * 1000
    },
    expect: {
        timeout: 10 * 1000
    },
    timeout: 90000,
    outputDir: `${__dirname}/build`,
    projects: [
        {
            name: 'chromium',
            use: { ...devices['Desktop Chrome'] }
        }
    ],
    reporter: [
        ['junit', { outputFile: 'build/results.xml' }],
        ['html', { outputFolder: 'build/html-report', open: 'never' }],
        ['list']
    ]
};
export default config;

// .gitlab-ci.yml

.e2e:
  stage: postdeploy
  image: xxxxx
  script:
    - pnpm xxx
    - pnpm nx test e2e // "test": "playwright test --output build --workers 2",
  after_script:
    - ls -l tests/e2e
    - cp -r tests/e2e/build ./results

  artifacts:
    when: always
    expire_in: never
    name: 'test-artifacts'
    paths:
      - results/html-report
    exclude:
      - results/results.xml
    reports:
      junit: results/results.xml

Related