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.53k stars 3.64k forks source link

[Question] Unable to run tests with videos enabled in docker using pwuser. #8275

Closed zac11 closed 3 years ago

zac11 commented 3 years ago

Context:

Code Snippet

Code that is used to launch Playwright


const {chromium, webkit, firefox} = require('playwright');
const options = {
  headless: false,
  slowMo: 100,
  setTimeout : 500000,
  // all args added to help run Chromium in headless mode
  args: [
    '--start-maximized',
    '--disable-gpu',       
    '--disable-dev-shm-usage',
    '--disable-setuid-sandbox',   
    '--no-sandbox',
    '--ignore-certificate-errors',
    '--ignore-certificate-errors-spki-list'
  ],
  //ignoreHTTPSErrors: true,
  video: "true"
};

(async () => {
  global.browser = await playwright['chromium'].launch(options);
 global.context = await global.browser.newContext({recordVideo:{dir:'videos/'}});
  // ...
})();

Dockerfile

FROM mcr.microsoft.com/playwright:focal
USER pwuser
WORKDIR /automation
COPY package.json ./
RUN npm install

COPY . ./

Docker-compose

version: "3.8"

services:
  playwright:
    container_name: playwright-test
    build:
      context: .
      dockerfile: Dockerfile
    command: npm run test

When trying to run tests with this configuration, I am trying to delete the videos folder before test run so that every run has a new videos folder created - I am using rimraf to delete the videos directory.

In the scripts section, I have the following commands -

"scripts": {
    "clean":"rimraf videos/",
    "cucumber-test":"./node_modules/.bin/cucumber-js --require cucumber.js --require step-definitions/**/*.js --require features/**/*.js --format html:./reports/cucumber_report.html --format summary --format @cucumber/pretty-formatter --no-strict --publish-quiet",
    "test": "npm run clean && npm run cucumber-test"

When I run the npm run test command, I get an EACCESS error when using the pwuser. If I use the root user, then the error doesn't come.

Is this because the pwuser doesn't has access to the wkdir /automation ? If this is the case, how can I give permission to pwuser for accessing the wkdir correctly?

Error message

Attaching to playwright-test
playwright-test | 
playwright-test | > playwright-cucumber-e2e-boilerplate@1.0.0 test /automation
playwright-test | > npm run clean && npm run cucumber-test
playwright-test | 
playwright-test | 
playwright-test | > playwright-cucumber-e2e-boilerplate@1.0.0 clean /automation
playwright-test | > rimraf videos/
playwright-test | 
playwright-test | /automation/node_modules/rimraf/bin.js:46
playwright-test |       throw er
playwright-test |       ^
playwright-test | 
playwright-test | [Error: EACCES: permission denied, unlink 'videos/.DS_Store'] {
playwright-test |   errno: -13,
playwright-test |   code: 'EACCES',
playwright-test |   syscall: 'unlink',
playwright-test |   path: 'videos/.DS_Store'
playwright-test | }
playwright-test | npm ERR! code ELIFECYCLE
playwright-test | npm ERR! errno 1
playwright-test | npm ERR! playwright-cucumber-e2e-boilerplate@1.0.0 clean: `rimraf videos/`
playwright-test | npm ERR! Exit status 1
playwright-test | npm ERR! 
playwright-test | npm ERR! Failed at the playwright-cucumber-e2e-boilerplate@1.0.0 clean script.
playwright-test | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
playwright-test | 
playwright-test | npm ERR! A complete log of this run can be found in:
playwright-test | npm ERR!     /home/pwuser/.npm/_logs/2021-08-18T07_10_42_051Z-debug.log
playwright-test | npm ERR! code ELIFECYCLE
playwright-test | npm ERR! errno 1
playwright-test | npm ERR! playwright-cucumber-e2e-boilerplate@1.0.0 test: `npm run clean && npm run cucumber-test`
playwright-test | npm ERR! Exit status 1
playwright-test | npm ERR! 
playwright-test | npm ERR! Failed at the playwright-cucumber-e2e-boilerplate@1.0.0 test script.
playwright-test | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
playwright-test | 
playwright-test | npm ERR! A complete log of this run can be found in:
playwright-test | npm ERR!     /home/pwuser/.npm/_logs/2021-08-18T07_10_42_074Z-debug.log
zac11 commented 3 years ago

Hi, I was working on this for the last couple of days and finally I've managed to fix this issue - however I still think there is a better way of solving this.

So, when running with pwuser the permissions issue happens due to with the pwuser cannot access the videos/ folder and the error message comes.

I modified my Dockerfile to something like this

FROM mcr.microsoft.com/playwright:focal
USER root
WORKDIR /automation

COPY package.json ./
RUN npm install

COPY . ./
RUN chown -R pwuser /automation
USER pwuser
CMD [ "npm","run","test" ]

And then I was able to run the tests with video enabled. However, this is not the greatest of approaches since it actually does the heavy lifting using the root user and then just changes ownership at the end.

yury-s commented 3 years ago

I your original docker file change copy to also set right owner:

COPY --chown=pwuser:pwuser . ./

See Docker COPY documentation.

Also it doesn't seem to be Playwright-specific issue, it's just rimraf videos/ fails for the copied dir.