blueimp / wdio

Docker setup for WebdriverIO with automatic screenshots, image diffing and screen recording support for containerized versions of Chrome and Firefox on Linux, mobile versions of Chrome and Firefox on Android as well as Safari on iOS, Safari on macOS and Edge on Windows.
https://hub.docker.com/r/blueimp/wdio
MIT License
296 stars 51 forks source link

What is the correct method to add and use a node module not included in repo? #3

Closed DanielDwyer closed 5 years ago

DanielDwyer commented 5 years ago

Greetings, this is more of a question than an issue (sorry if this is the incorrect location to ask). Let's say I wanted to add a node package manager module, such as request-promise, to be available when running tests in Firefox. How would I got about doing that?

I have tried adding the module to the list of modules to be installed globally, and require it in the test file (const rp = require('request-promise');), but then when I run tests in Firefox, I get the error: Cannot find module 'request-promise'.

Thanks in advance.

Screen Shot 2019-03-28 at 4 20 47 PM
blueimp commented 5 years ago

Hi @DanielDwyer,

there's three different options that come to my mind:

Maintaining your own Docker image based on alpine

you can add additional dependencies to the Dockerfile as long as you maintain your fork of the docker image, e.g. on your own repo on Docker hub.

To quickly test changes to the Dockerfile without having to change the image name, you can build the blueimp/wdio image locally:

docker build -t blueimp/wdio .

Maintaining your own Docker image based on blueimp/wdio

You could also use blueimp/wdio as base image and only add the additional dependencies, e.g. with the following Dockerfile:

FROM blueimp/wdio

RUN npm install -g \
    request-promise@^4.2.4 \
  # Clean up obsolete files:
  && rm -rf \
    /tmp/* \
    /root/.npm

Installing node_modules locally

An alternative way that doesn't require you to to build new Docker images is installing node_modules on your host machine. To do so, temporarily remove the read_only: true property from the wdio container and change its volumes definition to the single .:/opt host mount, with the following result:

  wdio:
    image: blueimp/wdio
    init: true
    tmpfs:
      - /tmp
      - /home/wdio/.android
    environment:
      - WAIT_FOR_HOSTS=
          chromedriver:4444
          geckodriver:4444
          example:8080
      - WINDOWS_HOST
      - ANDROID_SERIAL
    volumes:
      - .:/opt
    depends_on:
      - chromedriver
      - geckodriver
      - example

Next, simply run npm inside of the docker container:

docker-compose run --rm --entrypoint npm wdio install request-promise

This will install request-promise and its dependencies into a local node_modules folder, which will be picked up by WebdriverIO.

Recommendations

Now if you ask me what is the recommended way, I'd say: