From fff45240cd4ebb1928d0abb9463ee945ca7e6aa6 Mon Sep 17 00:00:00 2001 From: netzulo Date: Fri, 19 Apr 2019 10:07:04 +0200 Subject: [PATCH] [qacode] some ControlTable fixes + complexity + #248 ControlTable + #265 Complexity --- qacode/core/webs/controls/control_table.py | 58 +++++++++++-------- .../001_functionals/suite_008_controltable.py | 50 +++++++++++----- 2 files changed, 70 insertions(+), 38 deletions(-) diff --git a/qacode/core/webs/controls/control_table.py b/qacode/core/webs/controls/control_table.py index d7dbcf1b..5ab32a01 100644 --- a/qacode/core/webs/controls/control_table.py +++ b/qacode/core/webs/controls/control_table.py @@ -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): @@ -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 (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__""" @@ -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 diff --git a/tests/001_functionals/suite_008_controltable.py b/tests/001_functionals/suite_008_controltable.py index 1fcfa1eb..073aa461 100644 --- a/tests/001_functionals/suite_008_controltable.py +++ b/tests/001_functionals/suite_008_controltable.py @@ -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""" @@ -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, @@ -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, @@ -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)