node.js - 在 docker 容器中运行 nodeJS 应用程序、selenium 和 webdriver.io 测试

标签 node.js selenium docker selenium-webdriver

我正在尝试对我的 Node 应用程序(一个 docker 镜像)进行一些 webdriver.io 测试。

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

1) 通过在我的 ubuntu 服务器上运行它来获取 selenium 服务器:

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

这给了我正在运行的容器 'ubuntu_selenium_1' ($ docker ps)

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

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

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

这并没有给我任何输出。没有预期的标题,也没有错误消息。

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

我缺少 nodeJS 应用、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()

最佳答案

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

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

这里的变化:

首先,将 catch() 添加到您的 webdriverio 实例:

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(必须是,因为您使用的是 selenium-chromium):

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

关于node.js - 在 docker 容器中运行 nodeJS 应用程序、selenium 和 webdriver.io 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43746360/

相关文章:

javascript - Sequelize.js 删除查询?

node.js - 如何防止 Electron 中出现多个 showOpenDialog 窗口

javascript - 如何使用 phantomjs 和 php-webdriver 跟踪页面重定向并持续设置常量浏览器用户代理

docker - 如何在构建期间将凭据带入 Docker 容器

javascript - 同步调用函数并返回值

node.js - 无法安装 Node 包

selenium - 使用 docker 和 selenium 读取本地 URL

python - 通过selenium动态创建一个新元素

docker - Docker-compose PS未显示任何输出

docker - 如何为Docker配置Polipo的HTTP代理以部署Kubernetes