DevExpress / testcafe-react-selectors

TestCafe selector extensions for React apps.
https://testcafe.io
MIT License
203 stars 43 forks source link

Use with testcafe docker image #28

Closed frostiebot closed 7 years ago

frostiebot commented 7 years ago

I am curious if there is any way to include this library when running testcafe from within docker?

I have attempted to create a new docker image based off testcafe/testcafe, but ran into permissions-related issues when attempting to install testcafe-react-selectors via npm from within my new Dockerfile.

Another approach I made that I really am not entirely keen on involved essentially copying the Dockerfile from the testcafe github and adding multiple steps to basically ADD the testcafe release tar.gz, install gulp, build testcafe, copy the required directories to /opt/testcafe within the image and then finally install testcafe-react-selectors seperately. Not fun.

It's entirely possible I'm missing something amazingly simple, but I would appreciate any advice you could give about being able to completely contain both testcafe and this library within a single-entrypoint docker image.

Thanks!

EDIT - Just to give you an example of the truly horrible hoops I jumped through...

FROM alpine:edge

RUN apk --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ add \
  nodejs nodejs-npm chromium firefox xwininfo xvfb dbus eudev ttf-freefont fluxbox

ADD https://github.com/DevExpress/testcafe/archive/v0.16.2.tar.gz /opt

RUN npm install -g gulp && \
  mkdir -p /opt/testcafe && \
  cd /opt/testcafe-0.16.2 && \
  npm install && \
  gulp build && \
  cp -r docker /opt/testcafe && \
  cp -r lib /opt/testcafe && \
  cp -r bin /opt/testcafe && \
  cp -r package.json /opt/testcafe && \
  cd /opt/testcafe && \
  npm install --production && \
  npm install testcafe-react-selectors && \
  npm cache clean && \
  rm -fr /tmp/* && \
  rm -fr /opt/testcafe-0.16.2 && \
  chmod +x /opt/testcafe/docker/testcafe-docker.sh && \
  adduser -D user

USER user
EXPOSE 1337 1338
ENTRYPOINT ["/opt/testcafe/docker/testcafe-docker.sh"]
AndreyBelym commented 7 years ago

Hello @frostiebot, you can use USER root directive to get elevated privileges, and then use USER user to swtich them back. Also /root/ directory is missing in the container's file system, it must be created, e.g.:

FROM testcafe/testcafe
USER root
RUN  mkdir /root && cd /opt/testcafe && npm install testcafe-vue-selectors
USER user
sobolevn commented 6 years ago

I had the similar problem. When trying @AndreyBelym answer I had an error Can not find 'testcafe-vue-selectors'.

This solved it:

FROM testcafe/testcafe

USER root

ENV NODE_PATH=/opt:/opt/testcafe/node_modules

# Installing custom dependencies:
RUN cd /opt/testcafe && npm install \
  testcafe-nuxt-selectors testcafe-vue-selectors

USER user

Link to the source: https://github.com/wemake-services/wemake-vue-template/blob/master/template/docker/testcafe/Dockerfile

AndreyBelym commented 6 years ago

@sobolevn, you're right, in this case NODE_PATH should be configured to include Node.js modules from /opt/testcafe.

remigarcia commented 5 years ago

I had to slightly edit @sobolevn solution to add the /usr/lib/node_modules to NODE_PATH. If not, I was running in the following error :

ERROR Cannot prepare tests due to an error.

Error: Cannot find module 'testcafe'
    at Object.<anonymous> (/opt/testcafe/node_modules/testcafe-react-selectors/lib/index.js:8:17)
    at Object.origExt [as .js] (/usr/lib/node_modules/testcafe/src/compiler/test-file/api-based.js:76:21)
    at Object.<anonymous> (/tests/homepage.test.js:2:1)
    at Function._compile [as _execAsModule] (/usr/lib/node_modules/testcafe/src/compiler/test-file/api-based.js:50:13)
    at ESNextTestFileCompiler._execAsModule [as compile] (/usr/lib/node_modules/testcafe/src/compiler/test-file/api-based.js:144:42)
    at compile (/usr/lib/node_modules/testcafe/src/compiler/index.js:50:42)

My resulting Dockerfile is the following :

FROM testcafe/testcafe

USER root

ENV NODE_PATH=/opt:/usr/lib/node_modules:/opt/testcafe/node_modules

# Installing custom dependencies:
RUN cd /opt/testcafe && npm install \
  testcafe-react-selectors

USER user
tfiechowski commented 5 years ago

Hi, my Dockerfile looks following:

FROM testcafe/testcafe

USER root

ENV NODE_PATH=/opt:/usr/lib/node_modules:/opt/testcafe/node_modules

RUN cd /opt/testcafe && npm install \
  tsyringe@3.2.0 testcafe-reporter-html@1.4.4

USER user

Which is basically the same as mentioned above, but still get:

/tests/pages/LoginPage.ts (1, 30): Cannot find module 'tsyringe'.

However, when I delete testcafe-reporter-html from dependencies, the TestCafe does not start and complain about missing HTML reporter. This package is clearly visible, while tsyringe is not.

Do you have any ideas why it's not working?

AndreyBelym commented 5 years ago

The TypeScript compiler ignores the NODE_PATH variable when searching for modules. Add tsyringe as a dependency to your test project instead of installing it in the TestCafe directory.

lgriotti commented 4 years ago

Hello @frostiebot, you can use USER root directive to get elevated privileges, and then use USER user to swtich them back. Also /root/ directory is missing in the container's file system, it must be created, e.g.:

FROM testcafe/testcafe
USER root
RUN  mkdir /root && cd /opt/testcafe && npm install testcafe-vue-selectors
USER user

if i want to install all dependecies from my package.json how that will be?

RUN cd /opt/testcafe && npm install

with this is enough?

Dmitry-Ostashev commented 4 years ago

@lgriotti The cd command should change the current directory to the directory containing your package.json.