1

我想使用机器人框架验证文本字段中存在的占位符文本。

用户名

我使用了不同的 Selenium2Library 关键字,但它们都没有完全符合我的要求。

有没有人有办法从我的测试中获得这个功能?

4

3 回答 3

0

您可以运行命令getAttribute(input_field_locator@placeholder)。这将返回您想要的文本,然后您可以对其进行断言。

于 2013-01-08T10:25:33.630 回答
0

一旦您设法从 REPL 访问您正在寻找的功能,您可能会发现它无法通过机器人框架访问。您可以通过为 Selenium2Library 创建一个包装器来公开新关键字,该包装器使用额外的功能对其进行扩展 - 例如,请参阅教程中的https://github.com/alistair-broomhead/scalable-robotframework-example/blob/master/TestLibraries/Selenium2Custom我正在努力。

如果您改为导入此类(获取文本和获取 HTML,这对验证很有用),则此示例只是在 Selenium2Library 中的关键字之上添加两个关键字到robotframework:

from Selenium2Library import Selenium2Library
class Selenium2Custom(Selenium2Library):
    """
    Custom wrapper for robotframework Selenium2Library to add extra functionality
    """
    def get_text(self, locator):
        """
        Returns the text of element identified by `locator`.

        See `introduction` for details about locating elements.
        """
        return self._get_text(locator)
    def get_html(self, id=None):
        """
        Get the current document as an XML accessor object.
        """
        from lxml import html
        src = self.get_source().encode('ascii', 'xmlcharrefreplace')
        page = html.fromstring(src)
        element = page.get_element_by_id(id) if id is not None else page
        return html.tostring(element)

因此,做这样的事情是微不足道的:

from Selenium2Library import Selenium2Library
class Selenium2Custom(Selenium2Library):
    """
    Custom wrapper for robotframework Selenium2Library to add extra functionality
    """
    def get_placeholder(self, locator):
        """
        Returns the placeholder text of element identified by `locator`.
        """
        element = self._element_find(locator, True, False)
        return element.get_attribute("@placeholder")

现在我不知道这肯定对你有用,但对我来说它是这样工作的:

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Selenium2Library import Selenium2Library
>>> def get_placeholder(self, locator):
...     element = self._element_find(locator, True, False)
...     return element.get_attribute("placeholder")
... 
>>> Selenium2Library.get_placeholder = get_placeholder
>>> session = Selenium2Library()
>>> session.open_browser("http://www.wikipedia.org/wiki/Main_Page",
                         remote_url="http://127.0.0.1:4444/wd/hub")
1
>>> session.get_placeholder("search")
u'Search'
>>> 
于 2013-01-10T16:33:32.240 回答
0

我还想检查占位符文本,来到这里阅读评论后,我得到了一些线索。现在,棘手的部分是如何在机器人框架中做到这一点,在做了一些数学之后,我能够很容易地做到这一点。这是我的两行答案:

${webelement}=  Get WebElement  locator

${placeholder}=  Call Method  ${webelement}  get_attribute  placeholder
于 2020-08-24T11:14:02.530 回答