robotframework を使用して、テキスト フィールドに存在するプレースホルダー テキストを確認したいと考えています。
私はさまざまな Selenium2Library キーワードを使用しましたが、どれも私が望んでいることを正確に行うものではありません。
私のテスト内からこの機能を取得する方法はありますか?
robotframework を使用して、テキスト フィールドに存在するプレースホルダー テキストを確認したいと考えています。
私はさまざまな Selenium2Library キーワードを使用しましたが、どれも私が望んでいることを正確に行うものではありません。
私のテスト内からこの機能を取得する方法はありますか?
コマンドを実行できますgetAttribute(input_field_locator@placeholder)
。これにより、必要なテキストが返され、それをアサートできます。
探している機能に REPL からアクセスできたとしても、robotframework 経由ではアクセスできない場合があります。追加機能で拡張する Selenium2Library のラッパーを作成することで、新しいキーワードを公開できます。取り組んでいます。
この例では、代わりにこのクラスをインポートする場合に、Selenium2Library のキーワードの上に robotframework に 2 つのキーワードを追加するだけです (Get Text と Get HTML は検証に役立ちます)。
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'
>>>