Skip to content

Commit

Permalink
Fix output log name when Config_environment is used
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed Jan 13, 2018
1 parent 8655b00 commit 5726a65
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 104 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -11,6 +11,7 @@ v1.4.0
- Rename config property 'restart_driver_fail' in [Driver] section to 'restart_driver_after_failure'
- Add @no_driver feature or scenario tag to do not start the driver in these tests
- Fix output folder names when driver type is empty
- Fix output log name when `Config_environment` is used
- Fix Chrome options using remote drivers with Selenium >= 3.6.0

v1.3.0
Expand Down
12 changes: 11 additions & 1 deletion docs/driver_configuration.rst
Expand Up @@ -13,7 +13,7 @@ The following example shows how to choose Firefox::
[Driver]
type: firefox

If driver is not needed, typically in API tests, disable it using an empty string, :code:`api` or code:`no_driver`::
If driver is not needed, typically in API tests, disable it using an empty string, :code:`api` or :code:`no_driver`::

[Driver]
type: api
Expand All @@ -24,10 +24,20 @@ different properties files are used for different environments, they can be sele
will be loaded from :code:`conf/properties.cfg`, :code:`conf/android-properties.cfg` and
:code:`local-android-properties.cfg` files:

Nose:

.. code:: console
$ Config_environment=android nosetests web/tests/test_web.py
Py.test:

.. code:: console
$ Config_environment=android py.test web_pytest/tests/test_web_pytest.py
Behave:

.. code:: console
$ behave -D Config_environment=android
Expand Down
3 changes: 2 additions & 1 deletion toolium/behave/environment.py
Expand Up @@ -44,6 +44,7 @@ def before_all(context):

if not hasattr(context, 'config_files'):
context.config_files = ConfigFiles()
context.config_files = DriverWrappersPool.initialize_config_files(context.config_files)

# By default config directory is located in environment path
if not context.config_files.config_directory:
Expand Down Expand Up @@ -158,7 +159,7 @@ def create_and_configure_wrapper(context_or_world):
behave_properties = None

# Configure wrapper
context_or_world.driver_wrapper.configure(True, context_or_world.config_files, behave_properties)
context_or_world.driver_wrapper.configure(context_or_world.config_files, behave_properties=behave_properties)

# Copy config object
context_or_world.toolium_config = context_or_world.driver_wrapper.config
Expand Down
38 changes: 2 additions & 36 deletions toolium/driver_wrapper.py
Expand Up @@ -170,16 +170,13 @@ def update_visual_baseline(self):
self.visual_baseline_directory = os.path.join(DriverWrappersPool.visual_baseline_directory,
self.baseline_name)

def configure(self, is_selenium_test=True, tc_config_files=None, behave_properties=None):
def configure(self, tc_config_files, is_selenium_test=True, behave_properties=None):
"""Configure initial selenium instance using logging and properties files for Selenium or Appium tests
:param is_selenium_test: true if test is a selenium or appium test case
:param tc_config_files: test case specific config files
:param is_selenium_test: true if test is a selenium or appium test case
:param behave_properties: dict with behave user data properties
"""
# Initialize config files
tc_config_files = self._initialize_config_files(tc_config_files)

# Configure config and output directories
DriverWrappersPool.configure_common_directories(tc_config_files)

Expand All @@ -195,37 +192,6 @@ def configure(self, is_selenium_test=True, tc_config_files=None, behave_properti
DriverWrappersPool.configure_visual_directories(driver_info)
self.configure_visual_baseline()

@staticmethod
def _initialize_config_files(tc_config_files=None):
"""Initialize config files and update config files names with the environment
:param tc_config_files: test case specific config files
:returns: initialized config files object
"""
# Initialize config files
if tc_config_files is None:
tc_config_files = ConfigFiles()

# Update properties and log file names if an environment is configured
env = DriverWrappersPool.get_configured_value('Config_environment', None, None)
if env:
# Update config properties filenames
prop_filenames = tc_config_files.config_properties_filenames
new_prop_filenames_list = prop_filenames.split(';') if prop_filenames else ['properties.cfg']
base = new_prop_filenames_list[0].split('.')[0]
ext = new_prop_filenames_list[0].split('.')[1]
new_prop_filenames_list.append('{}-{}.{}'.format(env, base, ext))
new_prop_filenames_list.append('local-{}-{}.{}'.format(env, base, ext))
tc_config_files.set_config_properties_filenames(*new_prop_filenames_list)

# Update output log filename
output_log_filename = tc_config_files.output_log_filename
base = output_log_filename.split('.')[0] if output_log_filename else 'toolium'
ext = output_log_filename.split('.')[1] if output_log_filename else 'log'
tc_config_files.set_output_log_filename('{}_{}.{}'.format(base, env, ext))

return tc_config_files

def connect(self, maximize=True):
"""Set up the selenium driver and connect to the server
Expand Down
33 changes: 32 additions & 1 deletion toolium/driver_wrappers_pool.py
Expand Up @@ -19,6 +19,7 @@
import datetime
import inspect
import os
from toolium.config_files import ConfigFiles


class DriverWrappersPool(object):
Expand Down Expand Up @@ -113,7 +114,8 @@ def connect_default_driver_wrapper(cls, config_files=None):
"""
driver_wrapper = cls.get_default_wrapper()
if not driver_wrapper.driver:
driver_wrapper.configure(tc_config_files=config_files)
config_files = DriverWrappersPool.initialize_config_files(config_files)
driver_wrapper.configure(config_files)
driver_wrapper.connect()
return driver_wrapper

Expand Down Expand Up @@ -289,6 +291,35 @@ def configure_visual_directories(cls, driver_info):
cls.visual_output_directory = os.path.join(cls.output_directory, 'visualtests', folder_name)
cls.visual_number = 1

@staticmethod
def initialize_config_files(tc_config_files=None):
"""Initialize config files and update config files names with the environment
:param tc_config_files: test case specific config files
:returns: initialized config files object
"""
# Initialize config files
if tc_config_files is None:
tc_config_files = ConfigFiles()

# Update properties and log file names if an environment is configured
env = DriverWrappersPool.get_configured_value('Config_environment', None, None)
if env:
# Update config properties filenames
prop_filenames = tc_config_files.config_properties_filenames
new_prop_filenames_list = prop_filenames.split(';') if prop_filenames else ['properties.cfg']
base, ext = os.path.splitext(new_prop_filenames_list[0])
new_prop_filenames_list.append('{}-{}{}'.format(env, base, ext))
new_prop_filenames_list.append('local-{}-{}{}'.format(env, base, ext))
tc_config_files.set_config_properties_filenames(*new_prop_filenames_list)

# Update output log filename
output_log_filename = tc_config_files.output_log_filename
base, ext = os.path.splitext(output_log_filename) if output_log_filename else ('toolium', '.log')
tc_config_files.set_output_log_filename('{}_{}{}'.format(base, env, ext))

return tc_config_files

@classmethod
def _empty_pool(cls):
cls.driver_wrappers = []
Expand Down
2 changes: 1 addition & 1 deletion toolium/test/pageobjects/test_mobile_page_object.py
Expand Up @@ -37,7 +37,7 @@ def driver_wrapper():
config_files.set_config_directory(os.path.join(root_path, 'conf'))
config_files.set_config_properties_filenames('properties.cfg')
driver_wrapper = DriverWrappersPool.get_default_wrapper()
driver_wrapper.configure(tc_config_files=config_files)
driver_wrapper.configure(config_files)

return driver_wrapper

Expand Down
66 changes: 6 additions & 60 deletions toolium/test/test_driver_wrapper.py
Expand Up @@ -83,8 +83,7 @@ def driver_wrapper():
config_files.set_config_directory(os.path.join(root_path, 'conf'))
config_files.set_output_directory(os.path.join(root_path, 'output'))
config_files.set_config_log_filename('logging.conf')
DriverWrappersPool.configure_common_directories(config_files)
new_driver_wrapper.configure()
new_driver_wrapper.configure(config_files)

return new_driver_wrapper

Expand Down Expand Up @@ -112,7 +111,7 @@ def test_configure_no_changes(driver_wrapper):
driver_wrapper.config.set('Driver', 'type', 'opera')

# Trying to configure again
driver_wrapper.configure()
driver_wrapper.configure(ConfigFiles())

# Configuration has not been initialized
assert driver_wrapper.config.get('Driver', 'type') == 'opera'
Expand All @@ -128,7 +127,7 @@ def test_configure_change_configuration_file(driver_wrapper):
# Change properties file and try to configure again
root_path = os.path.dirname(os.path.realpath(__file__))
os.environ["Config_prop_filenames"] = os.path.join(root_path, 'conf', 'android-properties.cfg')
driver_wrapper.configure()
driver_wrapper.configure(ConfigFiles())
del os.environ["Config_prop_filenames"]

# Check that configuration has been initialized
Expand All @@ -141,67 +140,14 @@ def test_configure_environment(driver_wrapper):

# Change environment and try to configure again
os.environ["Config_environment"] = 'android'
driver_wrapper.configure()
config_files = DriverWrappersPool.initialize_config_files(ConfigFiles())
driver_wrapper.configure(config_files)
del os.environ["Config_environment"]

# Check that configuration has been initialized
assert driver_wrapper.config.get('Driver', 'type') == 'android'


def test_initialize_config_files_new():
config_files = None

# Initialize config files
init_config_files = DriverWrapper._initialize_config_files(config_files)

# Check expected config files
assert init_config_files.config_properties_filenames is None
assert init_config_files.output_log_filename is None


def test_initialize_config_files_new_environment():
config_files = None
os.environ["Config_environment"] = 'android'

# Initialize config files
init_config_files = DriverWrapper._initialize_config_files(config_files)
del os.environ["Config_environment"]

# Check expected config files
expected_properties_filenames = 'properties.cfg;android-properties.cfg;local-android-properties.cfg'
assert init_config_files.config_properties_filenames == expected_properties_filenames
assert init_config_files.output_log_filename == 'toolium_android.log'


def test_initialize_config_files_configured():
config_files = ConfigFiles()
config_files.set_config_properties_filenames('test.conf', 'local-test.conf')
config_files.set_output_log_filename('test.log')

# Initialize config files
init_config_files = DriverWrapper._initialize_config_files(config_files)

# Check expected config files
assert init_config_files.config_properties_filenames == 'test.conf;local-test.conf'
assert init_config_files.output_log_filename == 'test.log'


def test_initialize_config_files_configured_environment():
config_files = ConfigFiles()
config_files.set_config_properties_filenames('test.conf', 'local-test.conf')
config_files.set_output_log_filename('test.log')
os.environ["Config_environment"] = 'android'

# Initialize config files
init_config_files = DriverWrapper._initialize_config_files(config_files)
del os.environ["Config_environment"]

# Check expected config files
expected_properties_filenames = 'test.conf;local-test.conf;android-test.conf;local-android-test.conf'
assert init_config_files.config_properties_filenames == expected_properties_filenames
assert init_config_files.output_log_filename == 'test_android.log'


@mock.patch('toolium.driver_wrapper.ConfigDriver.create_driver')
def test_connect(create_driver, driver_wrapper):
# Mock data
Expand Down Expand Up @@ -246,7 +192,7 @@ def test_connect_api_from_file(driver_wrapper):
# Change driver type to api and configure again
root_path = os.path.dirname(os.path.realpath(__file__))
os.environ["Config_prop_filenames"] = os.path.join(root_path, 'conf', 'api-properties.cfg')
driver_wrapper.configure()
driver_wrapper.configure(ConfigFiles())
del os.environ["Config_prop_filenames"]

# Connect and check that the returned driver is None
Expand Down
72 changes: 71 additions & 1 deletion toolium/test/test_driver_wrappers_pool.py
Expand Up @@ -39,7 +39,7 @@ def driver_wrapper():
root_path = os.path.dirname(os.path.realpath(__file__))
config_files.set_config_directory(os.path.join(root_path, 'conf'))
config_files.set_output_directory(os.path.join(root_path, 'output'))
driver_wrapper.configure(tc_config_files=config_files)
driver_wrapper.configure(config_files)

return driver_wrapper

Expand Down Expand Up @@ -144,3 +144,73 @@ def test_find_parent_directory_absolute_recursively():
expected_config_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'conf')

assert expected_config_directory == DriverWrappersPool._find_parent_directory(directory, filename)


def test_initialize_config_files_new():
config_files = None

# Initialize config files
init_config_files = DriverWrappersPool.initialize_config_files(config_files)

# Check expected config files
assert init_config_files.config_properties_filenames is None
assert init_config_files.output_log_filename is None


def test_initialize_config_files_new_environment():
config_files = None
os.environ["Config_environment"] = 'android'

# Initialize config files
config_files = DriverWrappersPool.initialize_config_files(config_files)
del os.environ["Config_environment"]

# Check expected config files
expected_properties_filenames = 'properties.cfg;android-properties.cfg;local-android-properties.cfg'
assert config_files.config_properties_filenames == expected_properties_filenames
assert config_files.output_log_filename == 'toolium_android.log'


def test_initialize_config_files_configured():
config_files = ConfigFiles()
config_files.set_config_properties_filenames('test.conf', 'local-test.conf')
config_files.set_output_log_filename('test.log')

# Initialize config files
config_files = DriverWrappersPool.initialize_config_files(config_files)

# Check expected config files
assert config_files.config_properties_filenames == 'test.conf;local-test.conf'
assert config_files.output_log_filename == 'test.log'


def test_initialize_config_files_configured_environment():
config_files = ConfigFiles()
config_files.set_config_properties_filenames('test.conf', 'local-test.conf')
config_files.set_output_log_filename('test.log')
os.environ["Config_environment"] = 'android'

# Initialize config files
config_files = DriverWrappersPool.initialize_config_files(config_files)
del os.environ["Config_environment"]

# Check expected config files
expected_properties_filenames = 'test.conf;local-test.conf;android-test.conf;local-android-test.conf'
assert config_files.config_properties_filenames == expected_properties_filenames
assert config_files.output_log_filename == 'test_android.log'


def test_initialize_config_files_configured_environment_with_points():
config_files = ConfigFiles()
config_files.set_config_properties_filenames('test.new.conf', 'local-test.new.conf')
config_files.set_output_log_filename('test.new.log')
os.environ["Config_environment"] = 'ios'

# Initialize config files
config_files = DriverWrappersPool.initialize_config_files(config_files)
del os.environ["Config_environment"]

# Check expected config files
expected_properties_filenames = 'test.new.conf;local-test.new.conf;ios-test.new.conf;local-ios-test.new.conf'
assert config_files.config_properties_filenames == expected_properties_filenames
assert config_files.output_log_filename == 'test.new_ios.log'
2 changes: 1 addition & 1 deletion toolium/test/test_utils.py
Expand Up @@ -73,7 +73,7 @@ def driver_wrapper():
config_files.set_config_directory(os.path.join(root_path, 'conf'))
config_files.set_config_properties_filenames('properties.cfg')
config_files.set_output_directory(os.path.join(root_path, 'output'))
driver_wrapper.configure(tc_config_files=config_files)
driver_wrapper.configure(config_files)

yield driver_wrapper

Expand Down
2 changes: 1 addition & 1 deletion toolium/test/test_visual_test.py
Expand Up @@ -64,7 +64,7 @@ def driver_wrapper():
config_files.set_config_directory(os.path.join(root_path, 'conf'))
config_files.set_config_properties_filenames('properties.cfg')
config_files.set_output_directory(os.path.join(root_path, 'output'))
driver_wrapper.configure(tc_config_files=config_files)
driver_wrapper.configure(config_files)
driver_wrapper.config.set('VisualTests', 'enabled', 'true')

yield driver_wrapper
Expand Down
3 changes: 2 additions & 1 deletion toolium/test_cases.py
Expand Up @@ -56,7 +56,8 @@ def setUp(self):
self.config_files.set_config_directory(DriverWrappersPool.get_default_config_directory())

self.driver_wrapper = DriverWrappersPool.get_default_wrapper()
self.driver_wrapper.configure(False, self.config_files)
self.config_files = DriverWrappersPool.initialize_config_files(self.config_files)
self.driver_wrapper.configure(self.config_files, is_selenium_test=False)
# Get config and logger instances
self.config = self.driver_wrapper.config
self.logger = logging.getLogger(__name__)
Expand Down

0 comments on commit 5726a65

Please sign in to comment.