From 55398a476855ddf81357aabbc9e33b8443a1e558 Mon Sep 17 00:00:00 2001 From: netzulo Date: Sun, 14 Apr 2019 10:04:12 +0200 Subject: [PATCH] [qacode] more WIP changes for control_table #248 + added new property rows allow to acces to tr tags as rows at list of ControlBase array + tests --- qacode/core/loggers/logger_messages.py | 12 ++-- qacode/core/webs/controls/control_table.py | 68 +++++++++++++++++-- .../001_functionals/suite_008_controltable.py | 10 ++- 3 files changed, 77 insertions(+), 13 deletions(-) diff --git a/qacode/core/loggers/logger_messages.py b/qacode/core/loggers/logger_messages.py index 34625408..8f086861 100644 --- a/qacode/core/loggers/logger_messages.py +++ b/qacode/core/loggers/logger_messages.py @@ -68,9 +68,11 @@ CDD_SELECT_LOADED = "ctl_dd | select: selected" # noqa:E501 CDD_SELECT_LOADING = "ctl_dd | deselect: deselecting..." # noqa:E501 CDD_DESESELECT_LOADED = "ctl_dd | select: deselected" # noqa:E501 -CDD_DESELECTALL_LOADING = "ctl_form | dropdown_select: deselecting all..." # noqa:E501 -CDD_DESELECTALL_LOADED = "ctl_form | dropdown_select: deselected all" # noqa:E501 -CDD_LOADED = "ctl_form | ctl.dropdown property for tag element" # noqa:E501 +CDD_DESELECTALL_LOADING = "ctl_dd | dropdown_select: deselecting all..." # noqa:E501 +CDD_DESELECTALL_LOADED = "ctl_dd | dropdown_select: deselected all" # noqa:E501 +CDD_LOADED = "ctl_dd | ctl.dropdown property for tag element" # noqa:E501 # ControlTable -CT_BADTAG = "Can't use this for not tag element" # noqa:E501 +CT_BADTAG = "ctl_tb | Can't use this for not
tag element" # noqa:E501 +CT_LOADED = "ctl_tb | ctl.table property for
" # noqa:E501 +CT_TBLNOTCHILD = "ctl_tb | this table haven't got '{}' selector" # noqa:E501 diff --git a/qacode/core/webs/controls/control_table.py b/qacode/core/webs/controls/control_table.py index ac7f417e..6ac0c592 100644 --- a/qacode/core/webs/controls/control_table.py +++ b/qacode/core/webs/controls/control_table.py @@ -3,15 +3,25 @@ from qacode.core.exceptions.control_exception import ControlException +from qacode.core.exceptions.core_exception import CoreException from qacode.core.loggers import logger_messages as MSG from qacode.core.webs.controls.control_base import ControlBase from qacode.core.webs.controls.control_form import ControlForm +from selenium.common.exceptions import WebDriverException class ControlTable(ControlForm): """TODO: doc class""" _table = None + _rows = None + + # public properties + + caption = None + thead = None + tfoot = None + tbodies = None def __init__(self, bot, **kwargs): """Instance of ControlForm. Load properties from settings dict. @@ -25,11 +35,14 @@ def __init__(self, bot, **kwargs): super(ControlTable, self).__init__(bot, **kwargs) if not self.IS_TABLE and self.tag is not None: raise ControlException(msg=MSG.CT_BADTAG) - self.bot.log.debug(MSG.CDD_LOADED) + self.bot.log.debug(MSG.CT_LOADED) def __load_table__(self, element=None): """Allow to load all TR > TD items from a TABLE element + Before structure some checks are necessary for some children elements: + caption {ControlBase}-- optional
element + Examples: Use case 1. TABLE > (TR > TH)+(TR > TD) Use case 2. TABLE > (THEAD > (TR > TH))+(TBODY > (TR > TH)) @@ -39,13 +52,51 @@ def __load_table__(self, element=None): self._table = ControlBase(self.bot, **{ "selector": self.selector, "element": element}) - # Load rows - # thead = self._table.find_child("thead") - # tbody = self._table.find_child("tbody") - # Load columns - # Load cells + # Preload + self.tbodies = self.__try__("find_children", "tbodies") + is_html5 = False + if bool(self.tbodies): + is_html5 = True + self.caption = self.__try__("find_child", "caption") + self.thead = self.__try__("find_child", "thead") + self.tfoot = self.__try__("find_child", "tfoot") + # Load column headers + if not is_html5: + columns = self._table.find_children("tr :not(td)") # noqa + for column in columns: + column.name = column.text + 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 + else: + # is_hmtl5==True + # raise NotImplementedError("TODO: WIP zone") + pass # raise NotImplementedError("TODO: WIP zone") + def __get_row__(self, ctl_row, selector): + """WARNING: this method just can be used from __load_table__""" + row = [] + for cell in ctl_row.find_children(selector): + text = cell.get_text() + cell.settings.update({"name": text}) + cell.name = text + row.append(cell) + return row + + def __try__(self, method, selector): + """Allow to exec some method to handle exception""" + try: + return getattr(self._table, method)(selector) + except (ControlException, CoreException, WebDriverException): + self.bot.log.debug(MSG.CT_TBLNOTCHILD.format(selector)) + return None + def __check_reload__form__(self): """Allow to check before methods calls to ensure if it's neccessary reload element properties @@ -75,3 +126,8 @@ def table(self, value): if value is None or not isinstance(value, ControlBase): raise ControlException("Can't set not 'Control' instance") self.__load_table__(element=value) + + @property + def rows(self): + """GETTER for 'rows' property""" + return self._rows diff --git a/tests/001_functionals/suite_008_controltable.py b/tests/001_functionals/suite_008_controltable.py index d2a792b2..3529306b 100644 --- a/tests/001_functionals/suite_008_controltable.py +++ b/tests/001_functionals/suite_008_controltable.py @@ -3,13 +3,11 @@ import pytest -from qacode.core.exceptions.control_exception import ControlException from qacode.core.testing.test_info import TestInfoBotUnique from qacode.core.webs.controls.control_base import ControlBase from qacode.core.webs.controls.control_table import ControlTable from qautils.files import settings from selenium.webdriver.remote.webelement import WebElement -from selenium.webdriver.support.ui import Select SETTINGS = settings(file_path="qacode/configs/") @@ -123,3 +121,11 @@ def test_controltable_instance(self, on_instance_search, self.assert_none(ctl.table) ctl.reload(**ctl.settings) self.assert_is_instance(ctl.table, ControlBase) + self.assert_is_instance(ctl.rows, list) + # Use case 1. not html5:: TABLE > (TR > TH)+(TR > TD) + for row in ctl.rows: + self.assert_is_instance(row, list) + for cell in row: + self.assert_is_instance(cell, ControlBase) + # Use case 2. html5:: TABLE > (THEAD > (TR > TH))+(TBODY > (TR > TH)) + # raise NotImplementedError("TODO: WIP zone")