在docker容器中运行nodeJS app,selenium和webdriver.io测试

时间:2017-05-02 20:07:09

标签: node.js selenium docker selenium-webdriver

我正在尝试使用我的节点应用程序进行一些webdriver.io测试,这是一个docker镜像。

所以我到目前为止所做的是:

1)在我的ubuntu服务器上运行 selenium server

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

这为我提供了运行容器'ubuntu_selenium_1'($ docker ps

2)构建节点应用程序 docker镜像,在后台运行节点应用程序并运行e2e.js测试文件

在我的gitlab-ci.yml中我正在做

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

这不会给我任何输出。没有预期的标题和没有错误信息。

那么我做错了什么?有一个正在运行的selenium服务器,有一个在后台加载的节点应用程序,并且启动了e2e.js测试文件。

我错过了nodeJS app,webdriver和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

也许这个入口点脚本错了?

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

2 个答案:

答案 0 :(得分:8)

我做了你需要的东西,但我已将应用程序分离到自己的容器中。

您可以自己尝试使用我的示例:https://github.com/xbx/webdriverio-docker-example

这里有变化:

首先,在您的webdriverio实例中添加catch()

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

其次,使用chrome作为browserName(必须是,因为您使用了硒铬):

desiredCapabilities: {
                browserName: 'chrome'
            }

第三,正确指向您的应用:

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

了解容器的排列方式:

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

运行它: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

编辑:docker-compose版本1的简单更改:

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

答案 1 :(得分:1)

webdriverio找不到selenium

您正在调用webdriverio:

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

默认情况下,它会尝试在localhost:4444上使用selenium服务器。但是,这不会响应,因为每个容器都有自己的 localhost接口。您的selenium服务器正在另一个容器中运行。

您在问题中表明您的硒容器名称为ubuntu_selenium_1。所以你可以在那里指向webdriverio。 (容器名称可以像DNS主机名一样使用。)

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

如果您的selenium容器名称是其他名称,请将其替换为host参数。