user3142695
user3142695

Reputation: 17332

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:

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()

Upvotes: 9

Views: 6222

Answers (2)

Robert
Robert

Reputation: 36753

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
    ......
    ......

Upvotes: 9

Dan Lowe
Dan Lowe

Reputation: 56508

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.

Upvotes: 1

Related Questions