Esempio n. 1
0
def test_xpath_validator():
    xpaths = [
        "xpath=//div[@bar='bar']", "//div[@bar='bar']", "/html/body/table[1]"
    ]
    for x in xpaths:
        assert xpath_validator(x) is True
    assert xpath_validator("div[@bar='bar']") is False
Esempio n. 2
0
def capture_icon(locator,
                 folder='screenshots',
                 filename='screenshot_{}.png',
                 timeout=0,
                 **kwargs):  # pylint: disable=unused-argument
    r"""Take a screenshot of an element.

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

        ${some_xpath}=       //*[@value\="Button3"]
        CaptureIcon          ${some_xpath}

        CaptureIcon          Button3
        CaptureIcon          Button3    custom_screenshot_name_123.png
        CaptureIcon          Button3    custom_screenshot_name_123.png      C:/custom/folder/path

    Parameters
    ----------
    locator : str
        Locator for the element we are trying to capture, XPath or attribute value. When using
        XPaths, the equal sign "=" must be escaped with a "\\".
    folder : str
        Optional folder path. Default value is the screenshots folder.
    filename : str
        Optional filename.
    timeout : int
        How long we try to find the element for.

    Related keywords
    ----------------
    \`ClickIcon\`, \`IsIcon\`, \`VerifyIcon\`
    """
    if util.xpath_validator(locator):
        web_element = element.get_unique_element_by_xpath(locator)
    else:
        web_element = text.get_item_using_anchor(locator, anchor='1', **kwargs)
    img = Image.open(io.BytesIO(web_element.screenshot_as_png))
    filepath = os.path.join(screenshot.save_screenshot(filename, folder))
    logger.info('Screenshot path: {}'.format(filepath.replace('\\', '/')),
                also_console=True)
    img.save(filepath)
    screenshot.log_screenshot_file(filepath)
Esempio n. 3
0
 def get_table_element(self, locator, anchor):
     if util.xpath_validator(locator):
         table_element = element.get_unique_element_by_xpath(locator)
     else:  # Search using text
         table_xpath = "//*[text()= '{0}']/ancestor::table".format(locator)
         table_elements = element.get_webelements_in_active_area(
             table_xpath)
         if table_elements and len(table_elements) == 1:
             table_element = table_elements[0]
         elif not table_elements:  # Find table element using locator
             locator_element = text.get_text_using_anchor(locator, anchor)
             table_elements = self._get_all_table_elements()
             table_element = element.get_closest_element(
                 locator_element, table_elements)
         else:  # Found many
             table_element = text.get_element_using_anchor(
                 table_elements, anchor)
     if table_element:
         return table_element
     raise QWebElementNotFoundError(
         'Table element not found by locator {}'.format(locator))
Esempio n. 4
0
    def from_table_instance(cls,
                            locator,
                            anchor,
                            parent=None,
                            child=None,
                            level=1,
                            index=1):
        """Create table instance by finding table based on locator

        Parameters
        ----------
        locator : str
            Text that locates the table. The table that is closest
            to the text is selected. Also one can use xpath by adding xpath= prefix
            and then the xpath. Error is raised if the xpath matches to multiple
            elements.
        anchor : str
            Text near the table's locator element. If the page contains
            many places where the locator is then anchor is used to get the
            one that is closest to it.
        """
        if CONFIG["CssSelectors"] and not util.xpath_validator(locator):
            table_element = cls.get_table_element_by_css(locator, anchor)
        else:
            table_element = cls.get_table_element(cls, locator, anchor)
        if parent is None and child is None:
            if CONFIG['SearchMode']:
                element.draw_borders(table_element)
            return Table(table_element, locator, anchor, parent, child, level,
                         index)
        table_element = cls.get_table_by_locator_table(table_element, parent,
                                                       child, level, index)
        if CONFIG['SearchMode']:
            element.draw_borders(table_element)
        return Table(table_element, locator, anchor, parent, child, level,
                     index)