Skip to content

Commit

Permalink
[qacode] some ControlTable fixes + complexity
Browse files Browse the repository at this point in the history
+ #248 ControlTable
+ #265 Complexity
  • Loading branch information
netzulo committed Apr 19, 2019
1 parent 1bea511 commit fff4524
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
58 changes: 35 additions & 23 deletions qacode/core/webs/controls/control_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from qacode.core.webs.controls.control_base import ControlBase
from qacode.core.webs.controls.control_form import ControlForm
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.remote.webelement import WebElement


class ControlTable(ControlForm):
Expand Down Expand Up @@ -54,29 +55,39 @@ def __load_table__(self, element=None):
"selector": self.selector,
"element": element})
# Preload
self.tbodies = self.__try__("find_children", "tbodies")
self.tbodies = self.__try__("find_children", "tbody")
if bool(self.tbodies):
# table html5 structure
self.caption = self.__try__("find_child", "caption")
self.thead = self.__try__("find_child", "thead")
self.tfoot = self.__try__("find_child", "tfoot")
if len(self.tbodies) > 1:
raise ControlException(MSG.CT_TBL2ORMORETBODIES)
rows = []
rows.append(self.__get_row__(self.thead.find_child("tr"), "th"))
for ctl_row in self.tbodies[0].find_children("tr"):
rows.append(self.__get_row__(ctl_row, "td"))
self._rows = rows
self._rows = self.__load_table_html5__()
else:
# table <html5 structure
rows = []
ctls_rows = self._table.find_children("tr")
for index, ctl_row in enumerate(ctls_rows):
if index == 0:
rows.append(self.__get_row__(ctl_row, "th"))
else:
rows.append(self.__get_row__(ctl_row, "td"))
self._rows = rows
self._rows = self.__load_table_html4__()

def __load_table_html4__(self):
"""Allow to load table with this structure
TABLE > (TR > TH)+(TR > TD)
"""
rows = []
ctls_rows = self._table.find_children("tr")
for index, ctl_row in enumerate(ctls_rows):
if index == 0:
rows.append(self.__get_row__(ctl_row, "th"))
else:
rows.append(self.__get_row__(ctl_row, "td"))
return rows

def __load_table_html5__(self):
"""Allow to load table with this structure
TABLE > (THEAD > (TR > TH))+(TBODY > (TR > TH))
"""
self.caption = self.__try__("find_child", "caption")
self.thead = self.__try__("find_child", "thead")
self.tfoot = self.__try__("find_child", "tfoot")
if len(self.tbodies) > 1:
raise ControlException(MSG.CT_TBL2ORMORETBODIES)
rows = []
rows.append(self.__get_row__(self.thead.find_child("tr"), "th"))
for ctl_row in self.tbodies[0].find_children("tr"):
rows.append(self.__get_row__(ctl_row, "td"))
return rows

def __get_row__(self, ctl_row, selector):
"""WARNING: this method just can be used from __load_table__"""
Expand Down Expand Up @@ -122,11 +133,12 @@ def table(self):
@table.setter
def table(self, value):
"""SETTER for 'table' property"""
if value is None or not isinstance(value, ControlBase):
raise ControlException("Can't set not 'Control' instance")
if value is None or not isinstance(value, WebElement):
raise ControlException("Can't set not 'WebElement' instance")
self.__load_table__(element=value)

@property
def rows(self):
"""GETTER for 'rows' property"""
self.__check_reload__form__()
return self._rows
50 changes: 35 additions & 15 deletions tests/001_functionals/suite_008_controltable.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ def setup_class(cls, **kwargs):
cls.add_property(
'dd_menu_data_lists', cls.settings_control('dd_menu_data_lists'))
cls.add_property('tbl_ok', cls.settings_control('tbl_ok'))
cls.add_property('tbl_html5_ok', cls.settings_control('tbl_html5_ok'))

def setup_method(self, test_method):
"""Configure self.attribute"""
super(TestControlDropdown, self).setup_method(
test_method, config=settings(file_path="qacode/configs/"))
self.setup_login_to_data()

def setup_login_to_data(self):
"""Do login before to exec some testcases"""
Expand Down Expand Up @@ -93,11 +95,11 @@ def setup_login_to_data(self):
[{"tag": "table", "type": "tag", "severity": "hight"}],
[]
])
@pytest.mark.parametrize("ctl_name", ['tbl_ok', 'tbl_html5_ok'])
def test_controltable_instance(self, on_instance_search,
strict_rules, auto_reload):
strict_rules, auto_reload, ctl_name):
"""Testcase: test_controltable_instance"""
self.setup_login_to_data()
cfg = self.tbl_ok
cfg = getattr(self, ctl_name)
cfg.update({
"instance": "ControlTable",
"on_instance_search": on_instance_search,
Expand Down Expand Up @@ -125,20 +127,28 @@ def test_controltable_instance(self, on_instance_search,
self.assert_is_instance(ctl.table, ControlBase)
self.assert_is_instance(ctl.rows, list)
# Use case 1. not html5:: TABLE > (TR > TH)+(TR > TD)
self.assert_lower(len(ctl.rows), 3)
for row in ctl.rows:
self.assert_is_instance(row, list)
self.assert_lower(len(row), 2)
for cell in row:
self.assert_is_instance(cell, ControlBase)
if ctl_name == 'tbl_ok':
self.assert_lower(len(ctl.rows), 3)
for row in ctl.rows:
self.assert_is_instance(row, list)
self.assert_lower(len(row), 2)
for cell in row:
self.assert_is_instance(cell, ControlBase)
# Use case 2. html5:: TABLE > (THEAD > (TR > TH))+(TBODY > (TR > TH))
if ctl_name == 'tbl_html5_ok':
self.assert_lower(len(ctl.rows), 4)
for row in ctl.rows:
self.assert_is_instance(row, list)
self.assert_lower(len(row), 3)
for cell in row:
self.assert_is_instance(cell, ControlBase)

@pytest.mark.skipIf(SKIP_CONTROLS, SKIP_CONTROLS_MSG)
@pytest.mark.parametrize("strict_rules", [None])
def test_controltable_instance_raises(self, strict_rules):
@pytest.mark.parametrize("ctl_name", ['tbl_ok', 'tbl_html5_ok'])
def test_controltable_instance_raises(self, strict_rules, ctl_name):
"""Testcase: test_controltable_instance_raises"""
self.setup_login_to_data()
cfg = self.tbl_ok
cfg = getattr(self, ctl_name)
cfg.update({
"instance": "ControlTable",
"strict_rules": strict_rules,
Expand All @@ -153,8 +163,18 @@ def test_controltable_instance_raises(self, strict_rules):
self.assert_equals(ctl.locator, 'css selector')

@pytest.mark.skipIf(SKIP_CONTROLS, SKIP_CONTROLS_MSG)
def test_controltable_loadtable_ok(self):
"""Testcase: test_controltable_loadtable"""
self.setup_login_to_data()
def test_controltable_internals_ok(self):
"""Testcase: test_controltable_internals_ok"""
ctl = ControlTable(self.bot, **self.tbl_ok)
ctl.__load_table__()
ctl.__check_reload__form__()

@pytest.mark.skipIf(SKIP_CONTROLS, SKIP_CONTROLS_MSG)
def test_controltable_properties_ok(self):
"""Testcase: test_controltable_properties_ok"""
ctl = ControlTable(self.bot, **self.tbl_ok)
rows_before = len(ctl.rows)
ctl.table = ctl.element
rows_after = len(ctl.rows)
self.assert_is_instance(ctl.table, ControlBase)
self.assert_equals(rows_before, rows_after)

0 comments on commit fff4524

Please sign in to comment.