Beispiel #1
0
def get_element_count(locator, timeout=0, **kwargs):  # pylint: disable=unused-argument
    r"""Get count of appearances for certain web element.

    Keyword waits until timeout has passed. If timeout is not specified, it
    uses default timeout that can be adjusted with DefaultTimeout keyword.

    GetTextCount does not require for the text to be unique.

    Examples
    --------
    .. code-block:: robotframework

        ${COUNT}    GetElementCount       //*[@id\="Foo"]
        ${COUNT}    GetElementCount       Foo    tag=div

    Parameters
    ----------
    locator : str
        Xpath or some attribute value of element. When using XPaths, the equal sign "=" must be
        escaped with a "\".
    timeout : str | int
        How long we try to find text before failing. Default 10 (seconds)
    Accepted kwargs:
        tag=tagname: Needed when attribute value is used as a locator
    """
    kwargs['element_kw'] = True
    if 'tag' in kwargs:
        web_elements = element.get_elements_by_attributes(
            kwargs.get('tag'), locator, **kwargs)
    else:
        web_elements = element.get_webelements(locator, **kwargs)
    if web_elements:
        return len(web_elements)
    raise QWebElementNotFoundError('Webelements not found')
Beispiel #2
0
def hover_element(xpath, timeout=0, index=1, **kwargs):  # pylint: disable=unused-argument
    r"""Hover the element specified by the xpath selector.

    Examples
    --------
    .. code-block:: robotframework

        HoverElement          //*[@id\="hover_me"]
        HoverElement          xpath\=//*[@id="hover_me"]

    Parameters
    ----------
    xpath : str
        Xpath expression with or without xpath= prefix. The equal sign "=" must be escaped
        with a "\".
    timeout : int
        How long we wait before failing.
    index : int
        If multiple found, use index to pick correct one.
    kwargs :
        |  Accepted kwargs:
        |       tag : html tag of preferred element -
        |           If tag is used then element is found
        |           by some of it's attribute (xpath is not needed)
        |       partial_match: True. If element is found by it's attribute set partial_match
        |       to True to allow partial match
    """
    index = int(index) - 1
    kwargs['element_kw'] = True
    if 'tag' in kwargs:
        web_element = element.get_elements_by_attributes(
            kwargs.get('tag'), xpath, **kwargs)[index]
    else:
        web_element = element.get_unique_element_by_xpath(xpath)
    actions.hover_to(web_element, timeout=timeout)
Beispiel #3
0
def verify_no_element(xpath, timeout=0, **kwargs):  # pylint: disable=unused-argument
    r"""Wait element can not be found on the page.

    Examples
    --------
    .. code-block:: robotframework

        VerifyNoElement          //*[@id\="do_not_wait_me"]

    This keyword has timeout functionality. If the element is still visible after
    given timeout, error is raised.
    For example.

    .. code-block:: robotframework

       VerifyNoElement          //*[@id\="wait_me"]       20   #waits 20 seconds

    Parameters
    ----------
    xpath : str
        Xpath expression without xpath= prefix. The equal sign "=" must be escaped with a "\\".
    timeout : str | int
        Timeout for the element to disappear. If the element is visible after
        given timeout, error is raised. The str is converted to integer
        using robot.utils.timestr_to_secs. (default 10 seconds)
    kwargs :
        |  Accepted kwargs:
        |       tag : html tag of preferred elemenr -
        |           If tag is used then element is found
        |           by some of it's attribute (xpath is not needed)
        |       partial_match: True. If element is found by it's attribute set partial_match
        |       to True to allow partial match

    Raises
    ------
    NoSuchElementException
        Page did contain the element

    Related keywords
    ----------------
    \`VerifyElement\`
    """
    kwargs['element_kw'] = True
    if 'tag' in kwargs:
        web_elements = element.get_visible_elements_from_elements(
            element.get_elements_by_attributes(kwargs.get('tag'), xpath,
                                               **kwargs))
    else:
        web_elements = element.get_webelements(xpath)
    if not web_elements:
        return
    raise QWebValueError(
        'Page contained element with XPath "{}" after timeout'.format(xpath))
Beispiel #4
0
def verify_element(xpath, timeout=0, **kwargs):  # pylint: disable=unused-argument
    r"""Verify that element can be found on the page and it is visible.

    Examples
    --------
    .. code-block:: robotframework

       VerifyElement          //*[@id\="wait_me"]

    This keyword has timeout functionality. If the element is not visible after
    given timeout, error is raised.
    For example.

    .. code-block:: robotframework

        VerifyElement          //*[@id\="wait_me"]       20   #waits 20 seconds

    Parameters
    ----------
    xpath : str
        Xpath expression without xpath= prefix. The equal sign "=" must be escaped with a "\\".
    timeout : str | int
        Timeout for finding the element. If the element is not visible after
        given timeout, error is raised. The str is converted to integer
        using robot.utils.timestr_to_secs. (default 10 seconds)
    kwargs :
        |  Accepted kwargs:
        |       tag : html tag of preferred elemenr -
        |           If tag is used then element is found
        |           by some of it's attribute (xpath is not needed)
        |       partial_match: True. If element is found by it's attribute set partial_match
        |       to True to allow partial match

    Raises
    ------
    QWebElementNotFoundError
        Page did not contain element

    Related keywords
    ----------------
    \`ClickElement\`, \`GetAttribute\`, \`GetWebElement\`, \`GetElementCount\`,
    \`VerifyItem\`, \`VerifyText\`
    """
    kwargs['element_kw'] = True
    if 'tag' in kwargs:
        web_elements = element.get_visible_elements_from_elements(
            element.get_elements_by_attributes(kwargs.get('tag'), xpath,
                                               **kwargs))
    else:
        web_elements = element.get_webelements(xpath, **kwargs)
    if web_elements:
        return
    raise QWebElementNotFoundError('No matching element found')
Beispiel #5
0
def _get_item_by_css(text, **kwargs):
    """
    Allows partial match. Anchor has to be number.
    :param text: str
        Attribute value of the element
    :return:
        webelement that containing attribute with given value
    """
    if 'partial_match' not in kwargs:
        kwargs['partial_match'] = True
    css = 'a, span, img, li, h1, h2, h3, h4, h5, h6, div, svg, p, button, input' \
          ':not([type="text"]):not([type="password"]):not([type="email"])'
    full, partial = element.get_elements_by_attributes(css, text, **kwargs)
    web_elements = element.get_visible_elements_from_elements(full + partial, **kwargs)
    if web_elements:
        return web_elements
    return None
Beispiel #6
0
def hover_element(xpath, timeout=0, index=1, **kwargs):  # pylint: disable=unused-argument
    r"""Hover the element specified by the xpath selector.

    Examples
    --------
    .. code-block:: robotframework

        HoverElement          //*[@id\="hover_me"]
        HoverElement          xpath\=//*[@id="hover_me"]

    Parameters
    ----------
    xpath : str | selenium.webdriver.remote.webelement.WebElement
        Xpath expression with or without xpath= prefix. The equal sign "=" must be escaped
        with a "\".
        Can also be a WebElement instance returned by GetWebElement keyword or javascript.
    timeout : int
        How long we wait before failing.
    index : int
        If multiple found, use index to pick correct one.
    kwargs :
        |  Accepted kwargs:
        |       tag : html tag of preferred element -
        |           If tag is used then element is found
        |           by some of it's attribute (xpath is not needed)
        |       partial_match: True. If element is found by it's attribute set partial_match
        |       to True to allow partial match

    Related keywords
    ----------------
    \`HoverItem\`, \`HoverText\`, \`HoverTo\`, \`Scroll\`,
    \`ScrollText\`, \`ScrollTo\`
    """
    if isinstance(xpath, WebElement):
        web_element = xpath
    else:
        index = int(index) - 1
        kwargs['element_kw'] = True
        if 'tag' in kwargs:
            web_element = element.get_elements_by_attributes(
                kwargs.get('tag'), xpath, **kwargs)[index]
        else:
            web_element = element.get_unique_element_by_xpath(xpath)

    actions.hover_to(web_element, timeout=timeout)
Beispiel #7
0
def right_click(xpath, timeout=0, index=1, **kwargs):  # pylint: disable=unused-argument
    r"""Right clicks the element.

    Examples
    --------
    .. code-block:: robotframework

        RightClick          click_me      tag=button
        RightClick          //*[@id\="click_me"]
        RightClick          xpath\=//*[@id\="click_me"]

    Parameters
    ----------
    xpath : str
        Xpath expression with or without xpath= prefix. The equal sign "=" must be escaped
        with a "\".
    timeout : int
        How long we wait before failing.
    index : int
        If multiple found, use index to pick correct one.
    kwargs :
        |  Accepted kwargs:
        |       tag : html tag of preferred element -
        |           If tag is used then element is found
        |           by some of it's attribute (xpath is not needed)
        |       partial_match: True. If element is found by it's attribute set partial_match
        |       to True to allow partial match

    Related keywords
    ----------------
    \`ClickElement\`, \`ClickItem\`, \`ClickText\`
    """
    index = int(index) - 1
    kwargs['element_kw'] = True
    if 'tag' in kwargs:
        web_element = element.get_elements_by_attributes(
            kwargs.get('tag'), xpath, **kwargs)[index]
    else:
        web_element = element.get_unique_element_by_xpath(xpath)
    if actions.right_click(web_element):
        return
Beispiel #8
0
def click_element(xpath, timeout=0, js=False, index=1, **kwargs):
    r"""Click element specified by xpath.

    Examples
    --------
    .. code-block:: robotframework

        ClickElement          click_me      tag=button
        ClickElement          //*[@id\="click_me"]
        ClickElement          xpath\=//*[@id\="click_me"]

    Parameters
    ----------
    xpath : str
        Xpath expression with or without xpath= prefix. The equal sign "=" must be escaped
        with a "\".
    timeout : int
        How long we wait before failing.
    js : boolean
        If set to true, uses javascript click instead of Selenium.
    index : int
        If multiple found, use index to pick correct one.
    kwargs :
        |  Accepted kwargs:
        |       tag : html tag of preferred elemenr -
        |           If tag is used then element is found
        |           by some of it's attribute (xpath is not needed)
        |       partial_match: True. If element is found by it's attribute set partial_match
        |       to True to allow partial match
    """
    index = int(index) - 1
    kwargs['element_kw'] = True
    if 'tag' in kwargs:
        web_element = element.get_elements_by_attributes(
            kwargs.get('tag'), xpath, **kwargs)[index]
    else:
        web_element = element.get_unique_element_by_xpath(xpath)
    if actions.execute_click_and_verify_condition(web_element,
                                                  timeout=timeout,
                                                  js=js):
        return
Beispiel #9
0
def get_webelement(locator,
                   anchor='1',
                   element_type=None,
                   timeout=0,
                   **kwargs):
    r"""Get Webelement using any Paceword -stylish locator.

    Examples
    --------
    Using attributes or xpaths like with ClickElement etc. kw:s without specified
    element_type. If element_type is not specified end result is a type of list:

    .. code-block:: robotframework

        ${list of elems}    GetWebelement          click_me      tag=button
        ${list of elems}    GetWebelement          //*[@id\="click_me"]
        ${list of elems}    GetWebelement          xpath\=//*[@id\="click_me"]

    Get element using element_type attribute to locate element.
    Text elements works as ClickText, VerifyText, GetText etc.:

    .. code-block:: robotframework

        ${elem}      GetWebelement          Log In    element_type=text
        ${elem}      GetWebelement          Contact   element_type=text  anchor=Qentinel
        ${elem}      GetWebelement          Contact   parent=div

    Item, Input, Dropdown, Checkbox elements:

    .. code-block:: robotframework

        ${elem}      GetWebelement          Log In    element_type=item
        ${elem}      GetWebelement          Username  element_type=input
        ${elem}      GetWebelement          Country   element_type=dropdown
        ${elem}      GetWebelement          Gender    element_type=checkbox

    All flags are available for using (timeout, anchor, index, visibility, parent, child etc.).
    in same way as you are using those with Pacewords like ClickText/Item, TypeText, Dropdown etc.

    Parameters
    ----------
    locator : str
        Visible text, attribute value or Xpath expression with or without xpath= prefix.
        The equal sign "=" must be escaped with a "\\".
    anchor : int
        Used when element_type is defined. Default=1 (first match)
    element_type : string
        Define element type/preferred searching method
        (available types: text, input, checkbox, item, dropdown).
    timeout : int
        How long we wait element to appear. Default=10 sec
    kwargs :
        |  Accepted kwargs:
        |       Any available for picked searching method.
        |       See interacting with text, item, input etc. elements from
        |       documentation

    Related keywords
    ----------------
    \`ClickElement\`, \`HoverElement\`, \`TypeText\`
    """
    kwargs['index'] = kwargs.get('index', 1)
    kwargs['timeout'] = timeout
    if element_type:
        if element_type.lower() == 'text':
            return text.get_element_by_locator_text(locator, anchor, **kwargs)
        if element_type.lower() == 'item':
            return text.get_item_using_anchor(locator, anchor, **kwargs)
        if element_type.lower() == "dropdown":
            return dropdown.get_dd_elements_from_all_documents(
                locator, anchor, **kwargs)
        if element_type.lower() == "input":
            return input_.get_input_elements_from_all_documents(
                locator, anchor, **kwargs)
        if element_type.lower() == "checkbox":
            return checkbox.get_checkbox_elements_from_all_documents(
                locator, anchor, **kwargs)
    kwargs['element_kw'] = True
    if 'tag' in kwargs:
        web_elements = element.get_visible_elements_from_elements(
            element.get_elements_by_attributes(kwargs.get('tag'), locator,
                                               **kwargs))
    else:
        web_elements = element.get_webelements(locator)
    if web_elements:
        return web_elements
    raise QWebElementNotFoundError('No matching element found')
Beispiel #10
0
def click_element(xpath, timeout=0, js=False, index=1, **kwargs):
    r"""Click element specified by xpath.

    Examples
    --------
    .. code-block:: robotframework

        ClickElement          click_me      tag=button
        ClickElement          //*[@id\="click_me"]
        ClickElement          xpath\=//*[@id\="click_me"]
        # using WebElement instance
        ${elem}=              GetWebElement    Click Me    element_type=text
        ClickElement          ${elem}

    To double-click element, use argument **doubleclick=True**

    .. code-block:: robotframework

        ClickText             double_click_me       doubleclick=True

    Or use SetConfig

    .. code-block:: robotframework

        SetConfig             DoubleClick           On
        ClickElement          double_click_me       tag=button

    Parameters
    ----------
    xpath : str | selenium.webdriver.remote.webelement.WebElement
        Xpath expression with or without xpath= prefix. The equal sign "=" must be escaped
        with a "\".
        Can also be a WebElement instance returned by GetWebElement keyword or javascript.
    timeout : int
        How long we wait before failing.
    js : boolean
        If set to true, uses javascript click instead of Selenium.
    index : int
        If multiple found, use index to pick correct one.
    kwargs :
        |  Accepted kwargs:
        |       tag : html tag of preferred elemenr -
        |           If tag is used then element is found
        |           by some of it's attribute (xpath is not needed)
        |       partial_match: True. If element is found by it's attribute set partial_match
        |       to True to allow partial match

    Related keywords
    ----------------
    \`ClickCell\`, \`ClickCheckbox\`, \`ClickIcon\`, \`ClickItem\`, \`ClickList\`,
    \`ClickText\`, \`ClickUntil\`, \`ClickWhile\`, \`RightClick\`, \`VerifyElement\`
    """
    if isinstance(xpath, WebElement):
        web_element = xpath
    else:
        index = int(index) - 1
        kwargs['element_kw'] = True
        if 'tag' in kwargs:
            web_element = element.get_elements_by_attributes(
                kwargs.get('tag'), xpath, **kwargs)[index]
        else:
            web_element = element.get_unique_element_by_xpath(xpath)
    if actions.execute_click_and_verify_condition(web_element,
                                                  timeout=timeout,
                                                  js=js,
                                                  **kwargs):
        return
Beispiel #11
0
def get_input_element_by_css_selector(locator,
                                      anchor,
                                      index=0,
                                      enable_check=False,
                                      **kwargs):
    """Get input element using css selectors.
       Parameters
       ----------
       locator : str
           Label text or attribute that points to the input.
           Looking for placeholder and commonly used tooltip-attributes first.
           If locator is label text, finds input element by it's for attribute.
           if for attribute is not available, then finds element by doing some
           DOM traversing.
       anchor : str
           Text near the locator element or index. If placeholder or another
           direct attribute (title, tooltip, value) exists then anchor
           has to be index. Text is aloud when searching by label or some other
           text which is near to input element.
       index: int
            If multiple input elements is nested or in same table cell, index is needed.
       enable_check: bool
            If enable_check is set to true returns first match even if disabled one.
       Returns
       -------
       WebElement
       """
    partial_matches = []
    upload = kwargs.get('upload')
    if upload:
        try:
            index = int(locator) - 1
            kwargs['any_element'] = True
            input_elements = element.get_elements_by_attributes(**kwargs)
            if input_elements:
                return input_elements[index]
        except ValueError:
            logger.debug('locator was text')
    if 'qweb_old' not in kwargs:
        full_matches, partial_matches = element.get_elements_by_css(
            locator, **kwargs)
        if full_matches:
            input_element = element.get_visible_elements_from_elements(
                full_matches, **kwargs)
            if input_element and str(anchor) == '1':
                input_element = input_element[index]
                return input_element
            if input_element:
                input_element = text.get_element_using_anchor(
                    input_element, anchor, **kwargs)
                return input_element
    try:
        locator_element = text.get_text_using_anchor(locator, anchor, **kwargs)
        input_elements = list(
            dict.fromkeys(
                element.get_element_from_childnodes(locator_element, **kwargs)
                + partial_matches))
    except QWebElementNotFoundError:
        logger.trace(
            'Element not found by visible text. Trying with partial match')
        input_elements = partial_matches
    if input_elements:
        visibles = element.get_visible_elements_from_elements(
            input_elements, **kwargs)
        if visibles:
            if element.is_enabled(visibles[index]) or enable_check is True:
                return visibles[index]
    return None