Skip to content

Commit

Permalink
[qacode] more WIP changes for control_table #248
Browse files Browse the repository at this point in the history
+ added new property rows allow to acces to tr tags as rows
    at list of ControlBase array + tests
  • Loading branch information
netzulo committed Apr 14, 2019
1 parent bc2a2ea commit 55398a4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
12 changes: 7 additions & 5 deletions qacode/core/loggers/logger_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <select>" # noqa:E501
CDD_BADTAG = "Can't use this for not <select> 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 <select>" # noqa:E501
CDD_BADTAG = "ctl_dd | Can't use this for not <select> tag element" # noqa:E501
# ControlTable
CT_BADTAG = "Can't use this for not <table> tag element" # noqa:E501
CT_BADTAG = "ctl_tb | Can't use this for not <table> tag element" # noqa:E501
CT_LOADED = "ctl_tb | ctl.table property for <table>" # noqa:E501
CT_TBLNOTCHILD = "ctl_tb | this table haven't got '{}' selector" # noqa:E501
68 changes: 62 additions & 6 deletions qacode/core/webs/controls/control_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 <caption> element
Examples:
Use case 1. TABLE > (TR > TH)+(TR > TD)
Use case 2. TABLE > (THEAD > (TR > TH))+(TBODY > (TR > TH))
Expand All @@ -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
Expand Down Expand Up @@ -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
10 changes: 8 additions & 2 deletions tests/001_functionals/suite_008_controltable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down Expand Up @@ -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")

0 comments on commit 55398a4

Please sign in to comment.