2017-10-10 119 views
1

我正在一个webdriverIO通过泊坞窗(https://github.com/hulilabs/webdriverio)测试脚本中使用:(?ENV)码头工人组成:如何设置环境变量

docker-compose run --rm webdriverio wdio 

现在我需要设置一个变量,此命令这可以在测试文件中使用。

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' } 
    ] 
} 

泊坞窗,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 
+0

你使用nodeJs吗? –

+0

是的,我愿意。但我认为这与此无关...... – user3142695

回答

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 

然后,你需要在你的应用程序才能阅读varuiable

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