10

I would like to know if it's possible to set the base_url via the command line. Example

bin/behat --base_url=http://google.fr

I would like to avoid creating a new profile and passing it via the command line each time I have to test a new url, for flexibility purpose.

Is there any trick here to do this ?

Thank you.

George Brighton
  • 5,131
  • 9
  • 27
  • 36
lnki
  • 411
  • 1
  • 4
  • 9
  • It is a bit puzzling why you don't mind passing the base_url on the command line, but passing the profile (which is less characters) is not ok. :) – aderuwe Nov 21 '13 at 18:11
  • 1
    aderuwe, I had the same question as original poster because we spin up new servers at will for testing branches. The URL is different for each instance. Storing these urls in behat.yml is not ideal. – bitlather Feb 14 '14 at 17:25
  • Totally forgot to validate the answer. @bitlather you can use my solution – lnki Feb 15 '14 at 08:23

4 Answers4

13

I found the solution by myself.

Just pass the base_url in the BEHAT_PARAM environment variable.

export BEHAT_PARAMS="context[parameters][base_url]=http://google.fr"

Then run behat

bin/behat
lnki
  • 411
  • 1
  • 4
  • 9
  • 1
    Does not work at least with Behat 3.2 and ends up in `ConfigurationLoadingException: Environment variable `BEHAT_PARAMS` should contain a valid JSON` Solution is to use: `export BEHAT_PARAMS='{"extensions" : {"Behat\\MinkExtension" : {"base_url" : "http://google.fr/"}}}'` see: http://behat.org/en/latest/user_guide/configuration.html#environment-variable-behat-params – stmllr Jan 17 '17 at 20:24
13

Alternatively if you are using Mink you could define a profile in behat.yml

# behat.yml
default:
    extensions:
        Behat\MinkExtension\Extension:
            base_url: http://local.mysite.com
            goutte: ~
            selenium2: ~

dev:
    extensions:
        Behat\MinkExtension\Extension:
            base_url: http://dev.mysite.com

And then you can run your tests against local.mysite.com by default with

$ behat

Or against dev.mysite.com with

$ behat --profile=dev
5

In Behat 3, this parameter seems to have become JSON, so it works like:

export BEHAT_PARAMS='{"extensions":{"Behat\\MinkExtension":{"base_url":"https://google.com/"}}}'

Important these params are not overrides, they are defaults, and get over-written by behat.yml. So you must UNSET the base_url in behat.yml for this CLI setting to work.

extensions:
  Behat\MinkExtension:
    # YOU MUST EDIT THIS YOURSELF!
    # Either set this here, or set the base_url via BEHAT_PARAMS
    # base_url: 'https://my.livesite.com/'

(Refs: feature-request, clue)

dman
  • 1,089
  • 13
  • 9
4

The shorter way of achieving the same as per your answer would be just to pass the environment variables before the command itself:

BEHAT_PARAMS="context[parameters][base_url]=http://google.fr" bin/behat
Community
  • 1
  • 1
Ian Bytchek
  • 8,804
  • 6
  • 46
  • 72