| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
# -- FILE: features/steps/my_steps.py |
| 3 |
|
| 4 |
from __future__ import print_function |
| 5 |
from behave import when |
| 6 |
import os |
| 7 |
import sys |
| 8 |
|
| 9 |
# -- VARIANT 1: |
| 10 |
@when(u'I click on ${environment_variable:w}') |
| 11 |
def step_impl(context, environment_variable): |
| 12 |
env_value = os.environ.get(environment_variable, None) |
| 13 |
if env_value is None: |
| 14 |
raise LookupError("Environment variable '%s' is undefined" % environment_variable) |
| 15 |
print("USE ENVIRONMENT-VAR: %s = %s (variant 1)" % (environment_variable, env_value)) |
| 16 |
|
| 17 |
|
| 18 |
# -- VARIANT 2: Use type converter |
| 19 |
from behave import register_type |
| 20 |
import parse |
| 21 |
|
| 22 |
@parse.with_pattern(r"\$\w+") # -- ONLY FOR: $WORD |
| 23 |
def parse_environment_var(text): |
| 24 |
assert text.startswith("$") |
| 25 |
env_name = text[1:] |
| 26 |
env_value = os.environ.get(env_name, None) |
| 27 |
return (env_name, env_value) |
| 28 |
|
| 29 |
register_type(EnvironmentVar=parse_environment_var) |
| 30 |
|
| 31 |
@when(u'I use the environment variable {environment_variable:EnvironmentVar}') |
| 32 |
def step_impl(context, environment_variable): |
| 33 |
env_name, env_value = environment_variable |
| 34 |
if env_value is None: |
| 35 |
raise LookupError("Environment variable '%s' is undefined" % env_name) |
| 36 |
print("USE ENVIRONMENT-VAR: %s = %s (variant 2)" \ |
| 37 |
% (env_name, env_value)) |
| 38 |
|