Skip to content

Commit

Permalink
Add config-parser tests to check parser errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed Apr 9, 2021
1 parent 3315b3e commit 404d5d9
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 29 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.rst
@@ -1,6 +1,13 @@
Toolium Changelog
=================

v1.9.2
------

*Release date: 2021-04-09*

- Fix error in *deepcopy* method of *ExtendedConfigParser* class when two config properties have colon in name

v1.9.1
------

Expand All @@ -18,7 +25,7 @@ v1.9.0

- Added utilities to download files
- Get text for InputText element in mobile tests
- Add *translate_config_variables* method to config parser class to translate config variables in a string
- Add *translate_config_variables* method to *ExtendedConfigParser* class to translate config variables in a string
- Add dataset utilities
- Manage multiples webviews for mobile tests

Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.9.1
1.9.2
2 changes: 1 addition & 1 deletion docs/bdd_integration.rst
Expand Up @@ -66,7 +66,7 @@ command line instead of using the driver type defined in properties.cfg:
Behave tags
-----------

Toolium defines two tags to configure driver:
Toolium defines three tags to configure driver:

* :code:`@reuse_driver`: feature tag to indicate that all scenarios in this feature should share the driver. The browser will not be closed between tests.
* :code:`@reset_driver`: identifies a scenario that should not reuse the driver. The browser will be closed and reopen before this test.
Expand Down
30 changes: 30 additions & 0 deletions toolium/config_parser.py
Expand Up @@ -140,3 +140,33 @@ def get_config_from_file(conf_properties_files):
raise Exception(message)

return config

# Overwrite ConfigParser methods to allow colon in options names
# To set a config property with colon in name
# goog:loggingPrefs = "{'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}"
# configure properties.cfg with:
# goog___loggingPrefs: {'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}

def _encode_option(self, option):
return option.replace(':', '___')

def _decode_option(self, option):
return option.replace('___', ':')

def get(self, section, option, *args, **kwargs):
return configparser.ConfigParser.get(self, section, self._encode_option(option), *args, **kwargs)

def set(self, section, option, *args):
configparser.ConfigParser.set(self, section, self._encode_option(option), *args)

def options(self, section):
return [self._decode_option(option) for option in configparser.ConfigParser.options(self, section)]

def has_option(self, section, option):
return configparser.ConfigParser.has_option(self, section, self._encode_option(option))

def remove_option(self, section, option):
return configparser.ConfigParser.remove_option(self, section, self._encode_option(option))

def items(self, section):
return [(self._decode_option(item[0]), item[1]) for item in configparser.ConfigParser.items(self, section)]
15 changes: 0 additions & 15 deletions toolium/driver_wrapper.py
Expand Up @@ -122,7 +122,6 @@ def configure_properties(self, tc_config_prop_filenames=None, behave_properties=
# Initialize the config object
self.config = ExtendedConfigParser.get_config_from_file(prop_filenames)
self.config_properties_filenames = prop_filenames
self.update_magic_config_names()

# Override properties with system properties
self.config.update_properties(os.environ)
Expand All @@ -131,20 +130,6 @@ def configure_properties(self, tc_config_prop_filenames=None, behave_properties=
if behave_properties:
self.config.update_properties(behave_properties)

def update_magic_config_names(self):
"""Replace '___' for ':' in options names as a workaround of a configparser limitation
To set a config property with : in name
goog:loggingPrefs = "{'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}"
Configure properties.cfg with:
goog___loggingPrefs: {'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}
"""
for section in self.config.sections():
for option in self.config.options(section):
if '___' in option:
option_value = self.config.get(section, option)
self.config.set(section, option.replace('___', ':'), option_value)
self.config.remove_option(section, option)

def configure_visual_baseline(self):
"""Configure baseline directory"""
# Get baseline name and translate config variables
Expand Down
4 changes: 4 additions & 0 deletions toolium/test/conf/properties.cfg
Expand Up @@ -40,6 +40,10 @@ complete_report: true
baseline_name: {Driver_type}
engine: pil

[Capabilities]
goog___loggingPrefs: {'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}
goog___chromeOptions: {​​​​​​​​'excludeSwitches': [ 'enable-automation' ],'useAutomationExtension': False}​​​​​​​​

[AppiumCapabilities]
automationName: Appium
platformName: Android
Expand Down
1 change: 1 addition & 0 deletions toolium/test/conf/special-properties.cfg
Expand Up @@ -3,3 +3,4 @@ type: google

[Capabilities]
goog___loggingPrefs: {'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}
goog___chromeOptions: {​​​​​​​​'excludeSwitches': [ 'enable-automation' ],'useAutomationExtension': False}​​​​​​​​
93 changes: 91 additions & 2 deletions toolium/test/test_config_parser.py
Expand Up @@ -17,7 +17,6 @@
"""

import os

import pytest

from toolium.config_parser import ExtendedConfigParser
Expand Down Expand Up @@ -66,6 +65,65 @@ def test_getboolean_optional(section, option, default, response, config):
assert response == config.getboolean_optional(section, option)


def test_get(config):
section = 'AppiumCapabilities'
option = 'automationName'
value = 'Appium'
assert value == config.get(section, option)


def test_get_with_colon_in_option(config):
section = 'Capabilities'
option = 'goog:loggingPrefs'
value = "{'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}"
assert value == config.get(section, option)


def test_set_with_colon_in_option(config):
section = 'Capabilities'
option = 'goog:loggingPrefs'
orig_value = "{'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}"
new_value = "{'performance': 'ALL', 'browser': 'ALL'}"

# Check previous value
assert orig_value == config.get(section, option)

# Modify property value and check new value
config.set(section, option, new_value)
assert new_value == config.get(section, option)


def test_options_with_colon_in_option(config):
section = 'Capabilities'
options = ['goog:loggingPrefs', 'goog:chromeOptions']
assert options == config.options(section)


def test_has_option_with_colon_in_option(config):
section = 'Capabilities'
option = 'goog:loggingPrefs'
wrong_option = 'goog:loggingPrefsWrong'
assert True == config.has_option(section, option)
assert False == config.has_option(section, wrong_option)


def test_remove_option_with_colon_in_option(config):
section = 'Capabilities'
option = 'goog:loggingPrefs'
wrong_option = 'goog:loggingPrefsWrong'
assert True == config.remove_option(section, option)
assert False == config.remove_option(section, wrong_option)
assert None == config.get_optional(section, option, default=None)


def test_items_with_colon_in_option(config):
section = 'Capabilities'
items = [('goog:loggingPrefs', "{'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}"),
('goog:chromeOptions',
"{​​​​​​​​'excludeSwitches': [ 'enable-automation' ],'useAutomationExtension': False}​​​​​​​​")]
assert items == config.items(section)


def test_deepcopy(config):
section = 'AppiumCapabilities'
option = 'automationName'
Expand All @@ -75,15 +133,46 @@ def test_deepcopy(config):
# Check previous value
assert orig_value == config.get(section, option)

# Copy config object and modify a property
# Copy config object
new_config = config.deepcopy()

# Check that both configs have the same property value
assert orig_value == config.get(section, option)
assert orig_value == new_config.get(section, option)

# Modify property value
new_config.set(section, option, new_value)

# Check that the value has no changed in original config
assert orig_value == config.get(section, option)
assert new_value == new_config.get(section, option)


def test_deepcopy_and_modify_option_with_colon(config):
section = 'Capabilities'
configured_option = 'goog___loggingPrefs'
option = 'goog:loggingPrefs'
orig_value = "{'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}"
new_value = "{'performance': 'ALL', 'browser': 'ALL'}"

# Check previous value
assert orig_value == config.get(section, option)

# Copy config object
new_config = config.deepcopy()

# Check that both configs have the same property value
assert orig_value == config.get(section, option)
assert orig_value == new_config.get(section, option)

# Modify property value
new_config.set(section, configured_option, new_value)

# Check that the value has no changed in original config
assert orig_value == config.get(section, option)
assert new_value == new_config.get(section, option)


def test_update_properties_environ(config):
section = 'AppiumCapabilities'
option = 'platformName'
Expand Down
9 changes: 0 additions & 9 deletions toolium/test/test_driver_wrapper_properties.py
Expand Up @@ -142,12 +142,3 @@ def test_configure_properties_change_configuration_file(driver_wrapper):

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


def test_configure_properties_colon_in_name(driver_wrapper):
os.environ["Config_prop_filenames"] = 'special-properties.cfg'
driver_wrapper.configure_properties()
# Property name is called goog___loggingPrefs in properties.cfg
logging_prefs = "{'performance': 'ALL', 'browser': 'ALL', 'driver': 'ALL'}"
assert driver_wrapper.config.get_optional('Capabilities', 'goog:loggingPrefs') == logging_prefs
assert driver_wrapper.config.get_optional('Capabilities', 'goog___loggingPrefs') is None

0 comments on commit 404d5d9

Please sign in to comment.