Skip to content

Commit

Permalink
fix: fix code quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed Jan 5, 2022
1 parent a96bae1 commit 394bd9e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 66 deletions.
4 changes: 2 additions & 2 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ checks:
argument-count:
enabled: true
config:
threshold: 4
threshold: 5
complex-logic:
enabled: true
config:
Expand All @@ -16,7 +16,7 @@ checks:
method-complexity:
enabled: true
config:
threshold: 5
threshold: 10
method-count:
enabled: true
config:
Expand Down
36 changes: 19 additions & 17 deletions toolium/config_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,24 @@ def _get_capabilities_from_driver_type(driver_name):
:returns: capabilities dictionary
"""
if driver_name == 'firefox':
return DesiredCapabilities.FIREFOX.copy()
capabilities = DesiredCapabilities.FIREFOX.copy()
elif driver_name == 'chrome':
return DesiredCapabilities.CHROME.copy()
capabilities = DesiredCapabilities.CHROME.copy()
elif driver_name == 'safari':
return DesiredCapabilities.SAFARI.copy()
capabilities = DesiredCapabilities.SAFARI.copy()
elif driver_name == 'opera':
return DesiredCapabilities.OPERA.copy()
capabilities = DesiredCapabilities.OPERA.copy()
elif driver_name == 'iexplore':
return DesiredCapabilities.INTERNETEXPLORER.copy()
capabilities = DesiredCapabilities.INTERNETEXPLORER.copy()
elif driver_name == 'edge':
return DesiredCapabilities.EDGE.copy()
capabilities = DesiredCapabilities.EDGE.copy()
elif driver_name == 'phantomjs':
return DesiredCapabilities.PHANTOMJS.copy()
capabilities = DesiredCapabilities.PHANTOMJS.copy()
elif driver_name in ('android', 'ios', 'iphone'):
return {}
raise Exception('Unknown driver {0}'.format(driver_name))
capabilities = {}
else:
raise Exception('Unknown driver {0}'.format(driver_name))
return capabilities

def _add_capabilities_from_driver_type(self, capabilities):
"""Extract version and platform from driver type and add them to capabilities
Expand Down Expand Up @@ -291,18 +293,18 @@ def _convert_property_type(value):
:returns: boolean, integer or string value
"""
if value in ('true', 'True'):
return True
formatted_value = True
elif value in ('false', 'False'):
return False
elif str(value).startswith('{') and str(value).endswith('}'):
return ast.literal_eval(value)
elif str(value).startswith('[') and str(value).endswith(']'):
return ast.literal_eval(value)
formatted_value = False
elif ((str(value).startswith('{') and str(value).endswith('}'))
or (str(value).startswith('[') and str(value).endswith(']'))):
formatted_value = ast.literal_eval(value)
else:
try:
return int(value)
formatted_value = int(value)
except ValueError:
return value
formatted_value = value
return formatted_value

def _setup_chrome(self, capabilities):
"""Setup Chrome webdriver
Expand Down
53 changes: 27 additions & 26 deletions toolium/pageelements/page_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,56 +199,57 @@ def is_visible(self):
"""
return self.is_present() and self.web_element.is_displayed()

def wait_until_visible(self, timeout=None):
"""Search element and wait until it is visible
def _wait_until_condition(self, condition, timeout=None):
"""Search element and wait until it meets the condition
:param condition: name of condition that must meet the element (visible, not_visible, clickable)
:param timeout: max time to wait
:returns: page element instance
"""
try:
self.utils.wait_until_element_visible(self, timeout)
condition_msg = ''
if condition == 'visible':
condition_msg = 'not found or is not visible'
self.utils.wait_until_element_visible(self, timeout)
elif condition == 'not_visible':
condition_msg = 'is still visible'
self.utils.wait_until_element_not_visible(self, timeout)
elif condition == 'clickable':
condition_msg = 'not found or is not clickable'
self.utils.wait_until_element_clickable(self, timeout)
except TimeoutException as exception:
parent_msg = " and parent locator '{}'".format(self.parent) if self.parent else ''
msg = "Page element of type '%s' with locator %s%s not found or is not visible after %s seconds"
timeout = timeout if timeout else self.utils.get_explicitly_wait()
self.logger.error(msg, type(self).__name__, self.locator, parent_msg, timeout)
exception.msg += "\n {}".format(msg % (type(self).__name__, self.locator, parent_msg, timeout))
msg = "Page element of type '%s' with locator %s%s %s after %s seconds"
msg = msg % (type(self).__name__, self.locator, parent_msg, condition_msg, timeout)
self.logger.error(msg)
exception.msg += "\n {}".format(msg)
raise exception
return self

def wait_until_visible(self, timeout=None):
"""Search element and wait until it is visible
:param timeout: max time to wait
:returns: page element instance
"""
return self._wait_until_condition('visible', timeout)

def wait_until_not_visible(self, timeout=None):
"""Search element and wait until it is not visible
:param timeout: max time to wait
:returns: page element instance
"""
try:
self.utils.wait_until_element_not_visible(self, timeout)
except TimeoutException as exception:
parent_msg = " and parent locator '{}'".format(self.parent) if self.parent else ''
msg = "Page element of type '%s' with locator %s%s is still visible after %s seconds"
timeout = timeout if timeout else self.utils.get_explicitly_wait()
self.logger.error(msg, type(self).__name__, self.locator, parent_msg, timeout)
exception.msg += "\n {}".format(msg % (type(self).__name__, self.locator, parent_msg, timeout))
raise exception
return self
return self._wait_until_condition('not_visible', timeout)

def wait_until_clickable(self, timeout=None):
"""Search element and wait until it is clickable
:param timeout: max time to wait
:returns: page element instance
"""
try:
self.utils.wait_until_element_clickable(self, timeout)
except TimeoutException as exception:
parent_msg = " and parent locator '{}'".format(self.parent) if self.parent else ''
msg = "Page element of type '%s' with locator %s%s not found or is not clickable after %s seconds"
timeout = timeout if timeout else self.utils.get_explicitly_wait()
self.logger.error(msg, type(self).__name__, self.locator, parent_msg, timeout)
exception.msg += "\n {}".format(msg % (type(self).__name__, self.locator, parent_msg, timeout))
raise exception
return self
return self._wait_until_condition('clickable', timeout)

def assert_screenshot(self, filename, threshold=0, exclude_elements=[], force=False):
"""Assert that a screenshot of the element is the same as a screenshot on disk, within a given threshold.
Expand Down
4 changes: 2 additions & 2 deletions toolium/selenoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ def download_session_video(self, scenario_name, timeout=5):
def download_session_log(self, scenario_name, timeout=5):
"""
download the session log file from remote selenoid,
renaming the file to scenario name and removing the video file in the server.
renaming the file to scenario name and removing the log file in the server.
GGR request: http://<username>:<password>@<ggr_host>:<ggr_port>/logs/<ggr_session_id>
selenoid request: http://<username>:<password>@<ggr_host>:<ggr_port>/logs/<ggr_session_id>.log
:param scenario_name: scenario name
:param timeout: threshold until the video file is downloaded
:param timeout: threshold until the log file is downloaded
"""
# Download logs only in linux nodes with logs enabled
if (self.driver_wrapper.get_driver_platform().lower() != 'linux' or
Expand Down
42 changes: 23 additions & 19 deletions toolium/visual_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,26 +405,30 @@ def _get_diff_message(message, image_size):
:param image_size: number of pixels to convert absolute distances
:returns: formatted message
"""
diff_message = None
if message is None:
# Images are equal
return ''
diff_message = ''
elif message == '' or 'Image dimensions do not match' in message:
# Different sizes in pil (''), perceptualdiff or imagemagick engines
return 'Image dimensions do not match'

# Check pil engine message
m = re.search(r'\(by a distance of (.*)\)', message)
if m:
return 'Distance of %0.8f' % (float(m.group(1)) / image_size)

# Check perceptualdiff engine message
m = re.search(r'([0-9]*) pixels are different', message)
if m:
return 'Distance of %0.8f' % (float(m.group(1)) / image_size)

# Check imagemagick engine message
m = re.search(r':[\r\n](\d*\.?\d*) \((\d*\.?\d*)\) @', message)
if m:
return 'Distance of %0.8f' % float(m.group(2))

return message
diff_message = 'Image dimensions do not match'

if diff_message is None:
# Check pil engine message
m = re.search(r'\(by a distance of (.*)\)', message)
if m:
diff_message = 'Distance of %0.8f' % (float(m.group(1)) / image_size)
if diff_message is None:
# Check perceptualdiff engine message
m = re.search(r'([0-9]*) pixels are different', message)
if m:
diff_message = 'Distance of %0.8f' % (float(m.group(1)) / image_size)
if diff_message is None:
# Check imagemagick engine message
m = re.search(r':[\r\n](\d*\.?\d*) \((\d*\.?\d*)\) @', message)
if m:
diff_message = 'Distance of %0.8f' % float(m.group(2))
if diff_message is None:
diff_message = message

return diff_message

0 comments on commit 394bd9e

Please sign in to comment.