Running nodeJS app, selenium and webdriver.io tests in docker container

I am trying to do some webdriver.io tests with my node application, which is a docker image.

So what I did so far is:

  • ASP.NET docker image exposed ports are closed
  • Docker/nginx [windows10] - change page on linked folder and show changes directly in the browser
  • Can Jprofile connect to JVM running in docker
  • Kubernetes: Dynamically create configuration json files from j2 templates
  • Unable to connect to cassandra from Docker container using spring-data-cassandra: NoHostAvailableException: All host(s) tried for query failed
  • Capturing output of python script run inside a docker container
  • 1) Get selenium server by running this on my ubuntu server:

    $ docker run -p 4444:4444 selenium/standalone-chrome
    

    This gives me the running container ‘ubuntu_selenium_1’ ($ docker ps)

    2) Build node application docker image, running node application in background and running e2e.js test file

    In my gitlab-ci.yml I am doing

    - docker build -t core:test -f Dockerfile.testing .
    - docker run --rm core:test
    

    That doesn’t give me any output. No expected title and no error message.

    So what am I doing wrong? There is a running selenium server, there is the node application which is loaded in background and the e2e.js test file is started.

    I’m missing the connection of nodeJS app, webdriver and selenium…

    Dockerfile.testing

    FROM core:latest
    
    # Copy the test files
    COPY docker-entrypoint.sh /
    COPY e2e.js /
    
    # Get npm packages AND install test framework - mocha and webdriver
    RUN (cd programs/server && npm install --silent)
    RUN npm install -g mocha --silent
    RUN npm install chai webdriverio --silent
    RUN chmod +x /docker-entrypoint.sh
    
    # Run application and then e2e test
    ENTRYPOINT ["/docker-entrypoint.sh"]
    

    docker-entrypoint.sh

    #!/bin/bash
    node main.js & node e2e.js
    

    Maybe this entrypoint script is wrong??

    e2e.js

    var     webdriverio = require('webdriverio'),
            options = {
                desiredCapabilities: {
                    browserName: 'firefox'
                }
            }
    
    webdriverio
        .remote(options)
        .init()
        .url('http://localhost') // Which port do I have to use??
        .getTitle().then(function(title) {
            console.log('Title was: ' + title)
        })
        .end()
    

  • Error running commands with Dockerfile
  • Docker-compose volume mount before run
  • Deploy Java EE application to payara41 docker container using maven
  • Docker container can only access internet with --net=host
  • Docker: disable pulling from remote registry
  • docker stop spark container from exiting
  • 2 Solutions collect form web for “Running nodeJS app, selenium and webdriver.io tests in docker container”

    I did what you need but I’ve separated the application to its own container.

    You can try it yourself with my example here: https://github.com/xbx/webdriverio-docker-example

    Here the changes:

    First, add a catch() to your webdriverio instance:

    webdriverio
        .remote(options)
        .init()
        .url('http://app:3000')
        .getTitle().then(function(title) {
            console.log('Title was: ' + title)
        })
        .catch(function(e){
          console.log('Error!')
          console.log(e)
        })
        .end()
    

    Second, use chrome as browserName (must be, due to you’re using selenium-chromium):

    desiredCapabilities: {
                    browserName: 'chrome'
                }
    

    Third, point correctly to your app:

    .url('http://app:3000')
    

    See how the containers are arranged:

    version: "3"
    
    services:
      selenium:
        image: selenium/standalone-chrome
        ports:
          - 4444:4444
        links:
          - app
      app:
        build: .
        ports:
          - 3000:3000
      testing:
        build:
          context: .
          dockerfile: Dockerfile.testing
        command: /wait-for-it.sh selenium:4444 -- /wait-for-it.sh app:3000 -- node /e2e.js
        links:
          - app
          - selenium
        volumes:
          - ./wait-for-it.sh:/wait-for-it.sh
    

    Running it: docker-compose up --build

    Attaching to question_app_1, question_selenium_1, question_testing_1
    app_1       | Started app.
    selenium_1  | 12:19:45.516 INFO - Selenium build info: version: '3.4.0', revision: 'unknown'
    ...
    selenium_1  | 12:19:45.769 INFO - Selenium Server is up and running
    testing_1   | Starting testing.
    selenium_1  | 12:19:47.827 INFO - Executing: [get: http://app:3000])
    app_1       | Hit!
    selenium_1  | 12:19:48.210 INFO - Done: [get: http://app:3000]
    selenium_1  | 12:19:48.220 INFO - Executing: [get title])
    selenium_1  | 12:19:48.239 INFO - Done: [get title]
    testing_1   | Title was: Hi, this is the title
    

    Edit: simple change for docker-compose version 1:

    testing:
        build:.
        dockerfile: Dockerfile.testing
        ......
        ......
    

    webdriverio can’t find selenium

    You are invoking webdriverio like this:

    var     webdriverio = require('webdriverio'),
            options = {
                desiredCapabilities: {
                    browserName: 'firefox'
                }
            }
    

    By default, it will attempt to use the selenium server on localhost:4444. However, that won’t respond, because each container has its own localhost interface. Your selenium server is running in a different container.

    You indicated in your question that your selenium container’s name is ubuntu_selenium_1. So you can point webdriverio there. (container names can be used just like DNS hostnames.)

    options = {
        desiredCapabilities: {
            browserName: 'firefox'
        },
        host: 'ubuntu_selenium_1',
        port: 4444
    }
    

    If your selenium container’s name is something else, substitute that as the host parameter.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.