Docker撰写:如何设置要在脚本中使用的env变量

时间:2017-10-10 13:06:28

标签: javascript docker docker-compose webdriver-io

我正在通过docker运行webdriverIO(https://github.com/hulilabs/webdriverio)测试:

docker-compose run --rm webdriverio wdio

现在我需要使用此命令(ENV?)设置一个可以在测试文件中使用的变量。

describe('my awesome website', function () {
  it('should do some chai assertions', function () {
    browser.url(url) // <-- I need to set the variable (dev vs. prod)
    browser.getTitle().should.be.equal('Website title')
  })
})

我该怎么做?

配置

我的 wdio.conf.js

exports.config = {
  host: 'hub',
  port: 4444,
  specs: [
    './specs/**/*.js'
  ],
  capabilities: [
    { browserName: 'chrome' },
    { browserName: 'firefox' }
  ]
}

我的 docker-compose.yml 如下所示:

version: '2'
services:
    webdriverio:
        image: huli/webdriverio:latest
        depends_on:
            - chrome
            - firefox
            - hub
        environment:
            - HUB_PORT_4444_TCP_ADDR=hub
            - HUB_PORT_4444_TCP_PORT=4444
        volumes:
            - /app:/app

    hub:
        image: selenium/hub
        ports:
            - 4444:4444

    firefox:
        image: selenium/node-firefox
        ports:
            - 5900
        environment:
            - HUB_PORT_4444_TCP_ADDR=hub
            - HUB_PORT_4444_TCP_PORT=4444
        depends_on:
            - hub

    chrome:
        image: selenium/node-chrome
        ports:
            - 5900
        environment:
            - HUB_PORT_4444_TCP_ADDR=hub
            - HUB_PORT_4444_TCP_PORT=4444
        depends_on:
            - hub

1 个答案:

答案 0 :(得分:2)

首先,您需要将ENV变量设置为docker-compose.yml

services:
    webdriverio:
        image: huli/webdriverio:latest
        depends_on:
            - chrome
            - firefox
            - hub
        environment:
            - HUB_PORT_4444_TCP_ADDR=hub
            - HUB_PORT_4444_TCP_PORT=4444
            - APP_PROFILE=dev # <- here new variable
        volumes:
            - /app:/app

然后你需要在你的应用中阅读这个变量

describe('my awesome website', function () {
  it('should do some chai assertions', function () {
    browser.url(process.env.APP_PROFILE)
    browser.getTitle().should.be.equal('Website title')
  })
})

此外,在Dockerfile中,您可以将ENV变量设置为默认值:

ENV APP_PROFILE=prod
相关问题