Exemplo n.º 1
0
 def __check_dropdown__(self, text, by_value=False, by_index=False):
     """Internal funcionality for select/deselect methods"""
     if not self._element or not self._dropdown:
         self.reload(**self._settings)
     if self._dropdown is None:
         raise ControlException(MSG.CDD_BADTAG, info_bot=self._info_bot)
     if by_value and by_index:
         raise ControlException(MSG.CDD_BADPARAMS, info_bot=self._info_bot)
     if by_index and not isinstance(text, int):
         raise ControlException(
             MSG.CDD_BADINDEXTYPE, info_bot=self._info_bot)
Exemplo n.º 2
0
    def _add_rules(self, strict_rules, strict_mode):
        """Validate strict rules for each type

        Arguments:
            strict_rules {StrictRule} -- strict_rule
            strict_mode {bool} -- if enabled, load values for future checks
        """
        # validate rules and add object to respective lists
        for strict_rule in strict_rules:
            if strict_rule.strict_type == StrictType.TAG:
                self.strict_tags.append(strict_rule.enum_type)
            elif strict_rule.strict_type == StrictType.HTML_ATTR:
                self.strict_attrs.append(strict_rule.enum_type)
            elif strict_rule.strict_type == StrictType.CSS_PROP:
                self.strict_css_props.append(strict_rule.enum_type)
            elif strict_rule.strict_type == StrictType.JS_EVENT:
                raise NotImplementedError(
                    "Open an issue on github if raise here")
            elif strict_rule.strict_type == StrictType.BEHAVIOUR:
                raise NotImplementedError(
                    "Open an issue on github if raise here")
            elif strict_rule.strict_type == StrictType.USABILITY:
                raise NotImplementedError(
                    "Open an issue on github if raise here")
            elif strict_rule.strict_type == StrictType.SEO:
                raise NotImplementedError(
                    "Open an issue on github if raise here")
            else:
                raise ControlException(
                    message="bad param 'strict_type', invalid value")
Exemplo n.º 3
0
    def get_text(self, on_screen=True):
        """Get element content text.
            If the isDisplayed() method can sometimes trip over when
            the element is not really hidden but outside the viewport
            get_text() returns an empty string for such an element.

        Keyword Arguments:
            on_screen {bool} -- allow to obtain text if element
                it not displayed to this element before
                read text (default: {True})

        Returns:
            str -- Return element content text (innerText property)
        """
        method = self.bot.navigation.method_name()
        self._info_bot.update({"method": method})
        self.bot.log.debug(MSG.CB_GETTEXT_LOADING)
        self.__check_reload__()
        text = None
        try:
            if self.tag == 'input':
                text = self.get_attr_value('value')
            else:
                text = self.bot.navigation.ele_text(
                    self.element, on_screen=on_screen)
        except CoreException as err:
            self.bot.log.error(MSG.CB_GETTEXT_FAILED)
            self._info_bot.update({"err": err})
            raise ControlException(info_bot=self._info_bot)
        if text:
            self._text = text
            self.bot.log.debug(MSG.CB_GETTEXT_LOADED.format(text))
        return text
Exemplo n.º 4
0
 def __search__(self):
     """Load element searching at selenium WebDriver"""
     self.bot.log.debug(MSG.CB_SEARCH_LOADING)
     # Step 1 ensure web element
     is_element = isinstance(self._element, WebElement)
     nav = self.bot.navigation
     if self._element and not is_element:
         msg = "Child is not instance of WebElement"
         raise ControlException(msg, info_bot=self._info_bot)
     if self._element and is_element:
         # nothing to do
         self.bot.log.debug(MSG.CB_SEARCH_FOUND_CHILD)
         return
     try:
         self._element = nav.find_element(
             self._selector, locator=self._locator)
     except CoreException:
         self.bot.log.debug(MSG.CB_SEARCH_WAITING)
         self._element = nav.find_element_wait(
             self._selector, locator=self._locator)
     if self._element:
         self.bot.log.debug(MSG.CB_SEARCH_FOUND)
     # Step 2 load minimal properties defined by qacode
     self.bot.log.debug(MSG.CB_PROP_LOADING)
     self._text = self.get_text()
     self.bot.log.debug(MSG.CB_PROP_LOADED)
Exemplo n.º 5
0
 def __load__(self, **kwargs):
     """Allow to reinstance control properties"""
     self._settings = self.__settings_parse__(**kwargs)
     self._info_bot.update({"selector": self._settings.get("selector")})
     if self._element:
         # Control instanced from child
         self._bot.log.debug("ctl | Child instance...")
         if self._on_instance_search:
             msg = "ctl | Can't auto search when child element"
             raise ControlException(msg, info_bot=self._info_bot)
     if not self._on_instance_search:
         self.bot.log.debug(MSG.CB_SEARCH_DISABLED)
         self.bot.log.debug(MSG.CB_PROP_DISABLED)
         return
     self.__search__()
Exemplo n.º 6
0
 def load_element(self,
                  selector,
                  locator=By.CSS_SELECTOR,
                  wait_for_load=False):
     """
     Find element using bot with default By.CSS_SELECTOR
         strategy for internal element
     """
     self.bot.log.debug("load_element: selector={}".format(selector))
     try:
         if wait_for_load:
             return self.bot.navigation.find_element_wait(selector,
                                                          locator=locator)
         return self.bot.navigation.find_element(selector, locator=locator)
     except CoreException as err:
         raise ControlException(
             err, message='Element not found at load control_base')
Exemplo n.º 7
0
    def get_attr_value(self, attr_name):
        """Search and attribute name over self.element and get value,
        if attr_value is obtained, then compare and raise if not

        Arguments:
            attr_name {str} -- find an attribute on WebElement
                with this name

        Returns:
            str -- value of html attr_name
        """
        try:
            value = self.bot.navigation.ele_attribute(self.element, attr_name)
            self.bot.log.debug("get_attr : attr_name={}, value={}".format(
                attr_name, value))
            return value
        except CoreException as err:
            raise ControlException(err, message=err.message)
Exemplo n.º 8
0
    def set_css_value(self, prop_name, prop_value, css_important=True):
        """Set new value for given CSS property name
            on ControlBase selector

        Arguments:
            prop_name {str} -- CSS property name
            prop_value {str} -- CSS property value

        Keyword Arguments:
            css_important {bool} -- Allow to include '!important' to rule for
                overrite others values applied (default: {True})
        """
        self.bot.navigation.set_css_rule(self.selector,
                                         prop_name,
                                         prop_value,
                                         css_important=css_important)
        if self.selector is None:
            raise ControlException(message="Couldn't reload element")
        # reload WebElement
        self.element = self.load_element(self.selector)
Exemplo n.º 9
0
    def get_text(self, on_screen=True):
        """Get element content text.
            If the isDisplayed() method can sometimes trip over when
            the element is not really hidden but outside the viewport
            get_text() returns an empty string for such an element.

        Keyword Arguments:
            on_screen {bool} -- allow to obtain text if element
                it not displayed to this element before
                read text (default: {True})

        Returns:
            str -- Return element content text (innerText property)
        """
        try:
            return self.bot.navigation.ele_text(self.element,
                                                on_screen=on_screen)
        except CoreException as err:
            if isinstance(err, CoreException):
                raise ControlException(err, message=err.message)
            else:
                raise Exception(err, message=err.message)
Exemplo n.º 10
0
    def set_css_value(self, prop_name, prop_value, css_important=True):
        """Set new value for given CSS property name
            on ControlBase selector

        Arguments:
            prop_name {str} -- CSS property name
            prop_value {str} -- CSS property value

        Keyword Arguments:
            css_important {bool} -- Allow to include '!important' to rule for
                overrite others values applied (default: {True})
        """
        self.bot.log.debug(
            MSG.CB_SETCSSRULE_LOADING.format(prop_name, prop_value))
        self.__check_reload__()
        self.bot.navigation.set_css_rule(
            self.selector, prop_name, prop_value,
            css_important=css_important)
        if self.selector is None:
            self.bot.log.error(MSG.CB_SETCSSRULE_FAILED)
            raise ControlException(
                "Couldn't reload element", info_bot=self._info_bot)
        self.reload(**self.settings)
Exemplo n.º 11
0
    def get_attr_value(self, attr_name):
        """Search and attribute name over self.element and get value,
        if attr_value is obtained, then compare and raise if not

        Arguments:
            attr_name {str} -- find an attribute on WebElement
                with this name

        Returns:
            str -- value of html attr_name
        """
        method = self.bot.navigation.method_name()
        self.bot.log.debug(MSG.CB_GETATTRVALUE_LOADING.format(attr_name))
        self.__check_reload__()
        try:
            value = self.bot.navigation.ele_attribute(
                self.element, attr_name)
            self.bot.log.debug(
                MSG.CB_GETATTRVALUE_LOADED.format(attr_name, value))
            return str(value)
        except CoreException as err:
            self.bot.log.error(MSG.CB_GETATTRVALUE_FAILED)
            self._info_bot.update({"err": err, "method": method})
            raise ControlException(info_bot=self._info_bot)
Exemplo n.º 12
0
    def __init__(self,
                 bot,
                 selector='',
                 locator=By.CSS_SELECTOR,
                 element=None,
                 search=True,
                 wait_for_load=False):
        """Base class to manage web element through page system of qacode
            library

        Usage:
            ControlBase(bot, selector, locator)
            ControlBase(bot, element)
            ControlBase(bot, element, search=True)

        Arguments:
            bot {BotBase} -- qacode bot Class to manage control validations

        Keyword Arguments:
            selector {str} -- can be empty string to use element insteadof of
                params to load WebElement
            locator {By} -- selenium search strategy
                (default: {By.CSS_SELECTOR})
            element {WebElement} -- instanced WebElement class
                (default: {None})
            search {bool} -- [description] (default: {True})
            wait_for_load {bool} -- wait for expected condition from selenium
                before to load element (default: {False})

        Raises:
            CoreEx -- param 'bot' can't be None
            ControlException -- param 'selector' can't be None, don't use if
                want to instance with 'element'
            ControlException -- param 'element' can't be None, don't use if
                want to instance with 'selector'
            ControlException -- 'element' found isn't valid to use, check
                selector and element
        """
        message_errors = [
            "param 'bot' can't be None",
            ("param 'selector' can't be None, don't use if want to instance "
             "with 'element'"),
            ("param 'element' can't be None, don't use if want to instance "
             "with 'selector'"),
            ("'element' found isn't valid to use, check ur selector={} and "
             "element={}"),
        ]
        if bot is None:
            raise ControlException(message_errors[0])
        self.bot = bot
        if selector is None:
            raise ControlException(message_errors[1])
        self.selector = selector
        if element is not None:
            search = False
        if search:
            self.element = self.load_element(selector,
                                             locator=locator,
                                             wait_for_load=wait_for_load)
        else:
            self.element = element
        # noqa for this logic, yet
        if self.element is None:
            raise ControlException(message_errors[2].format(
                self.selector, self.element))
        self.tag = self.get_tag()
        self.text = self.get_text()
        self.is_displayed = self.bot.navigation.ele_is_displayed(self.element)
        self.is_enabled = self.bot.navigation.ele_is_enabled(self.element)
        self.is_selected = self.bot.navigation.ele_is_selected(self.element)
        self.attr_id = self.get_attr_value('id')
        self.attr_class = self.get_attr_value('class')
Exemplo n.º 13
0
    def __init__(self, bot, selector='', locator=By.CSS_SELECTOR, element=None,
                 search=True, wait_for_load=False,
                 strict_rules=None,
                 strict_mode=False):
        """Base class to manage form web element through page system of qacode
            library. Apply for elements tagged with
                <label> : must validate attrs= id, class, for
                <input> : must validate attrs= id, class
                <select> + <option> : must validate attrs= id, class
                <textarea> : must validate attrs= id, class
                <datalist> + <option> : must validate attrs= id, class
                <output> : must validate attrs= id, class
            Apply also on parent elements
                <form> : must validate attrs= id, class
                <legend> : must validate attrs= id, class
                <fieldset> : must validate attrs= id, class

        Usage:
            ControlBase(bot, selector, locator)
            ControlBase(bot, element)
            ControlBase(bot, element, search=True)

        Arguments:
            bot {BotBase} -- qacode bot Class to manage control validations

        Keyword Arguments:
            selector {str} -- can be empty string to use element insteadof of
                params to load WebElement
            locator {By} -- selenium search strategy
                (default: {By.CSS_SELECTOR})
            element {WebElement} -- instanced WebElement class
                (default: {None})
            search {bool} -- [description] (default: {True})
            wait_for_load {bool} -- wait for expected condition from selenium
                before to load element (default: {False})
            strict_rules {list(StrictRule)} -- a list of strict rules to be
                applied to a element
            strict_mode {bool} -- allows to raise validation when warning
                it's received

        Raises:
            CoreEx -- param 'bot' can't be None
            ControlException -- param 'selector' can't be None, don't use if
                want to instance with 'element'
            ControlException -- param 'element' can't be None, don't use if
                want to instance with 'selector'
            ControlException -- 'element' found isn't valid to use, check
                selector and element
        """
        super(ControlForm, self).__init__(
            bot,
            selector=selector,
            locator=locator,
            element=element,
            search=search,
            wait_for_load=wait_for_load)
        if strict_mode is None:
            raise ControlException(
                message="bad param 'strict_mode' is None")
        self.strict_mode = strict_mode
        if not strict_rules or not isinstance(strict_rules, (list, tuple)):
            strict_rules = []
        self.strict_rules = strict_rules
        self.strict_tags = []
        self.strict_attrs = []
        self.strict_css_props = []
        self._add_rules(self.strict_rules, strict_mode)
        if not self.is_strict_tags() and self.strict_mode:
            raise ControlException(
                message=("Tag obtained for this element html tag not in "
                         "strict_tags list"))
        if not self.is_strict_attrs() and self.strict_mode:
            raise ControlException(
                message=("Html attribute obtained for this element not in "
                         "strict_attrs list"))
        if not self.is_strict_css_props() and self.strict_mode:
            raise ControlException(
                message=("Css property obtained for this element not in "
                         "strict_css_props list"))