Python用のセレン。 第6章ページオブジェクト

PythonのSelenium非公式ドキュメントの翻訳の継続。

オリジナルはこちらにあります



内容:



1. インストール

2. 最初のステップ

3. ナビゲーション

4. アイテムを検索する

5. 期待

6.ページオブジェクト

7. WebDriver API

8.付録:よくある質問



6.ページオブジェクト



このセクションは、ページオブジェクトのデザインパターンガイドの概要です。 ページオブジェクトは、テストがやり取りするWebアプリケーションのユーザーインターフェイスの領域です。



ページオブジェクトパターンを使用する長所:







6.1テストケース



以下は、python.orgで単語を検索し、何らかの結果が見つかったことを示すテストケースです。



import unittest from selenium import webdriver import page class PythonOrgSearch(unittest.TestCase): """A sample test class to show how page object works""" def setUp(self): self.driver = webdriver.Firefox() self.driver.get("http://www.python.org") def test_search_in_python_org(self): """ Tests python.org search feature. Searches for the word "pycon" then verified that some results show up. Note that it does not look for any particular text in search results page. This test verifies that the results were not empty. """ #Load the main page. In this case the home page of Python.og. main_page = page.MainPage(self.driver) #Checks if the word "Python" is in title assert main_page.is_title_matches(), "python.org title doesn't match." #Sets the text of search textbox to "pycon" main_page.search_text_element = "pycon" main_page.click_go_button() search_results_page = page.SearchResultsPage(self.driver) #Verifies that the results page is not empty assert search_results_page.is_results_found(), "No results found." def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()
      
      





6.2ページオブジェクトクラス



ページオブジェクトパターンは、各Webページのオブジェクトを作成することを目的としています。 この手法に従って、テストコードと技術的な実装の間に分離レイヤーが作成されます。



page.pyは次のようになります。



 from element import BasePageElement from locators import MainPageLocators class SearchTextElement(BasePageElement): """This class gets the search text from the specified locator""" #The locator for search box where search string is entered locator = 'q' class BasePage(object): """Base class to initialize the base page that will be called from all pages""" def __init__(self, driver): self.driver = driver class MainPage(BasePage): """Home page action methods come here. Ie Python.org""" #Declares a variable that will contain the retrieved text search_text_element = SearchTextElement() def is_title_matches(self): """Verifies that the hardcoded text "Python" appears in page title""" return "Python" in self.driver.title def click_go_button(self): """Triggers the search""" element = self.driver.find_element(*MainPageLocators.GO_BUTTON) element.click() class SearchResultsPage(BasePage): """Search results page action methods come here""" def is_results_found(self): # Probably should search for this text in the specific page # element, but as for now it works fine return "No results found." not in self.driver.page_source
      
      





6.3ページ要素



element.pyは次のようになります。



 from selenium.webdriver.support.ui import WebDriverWait class BasePageElement(object): """Base page class that is initialized on every page object class.""" def __set__(self, obj, value): """Sets the text to the value supplied""" driver = obj.driver WebDriverWait(driver, 100).until( lambda driver: driver.find_element_by_name(self.locator)) driver.find_element_by_name(self.locator).send_keys(value) def __get__(self, obj, owner): """Gets the text of the specified object""" driver = obj.driver WebDriverWait(driver, 100).until( lambda driver: driver.find_element_by_name(self.locator)) element = driver.find_element_by_name(self.locator) return element.get_attribute("value")
      
      





6.4ロケーター



1つの方法は、ロケーターラインを使用する場所から抽出することです。 この例では、同じページ上のロケーターは同じクラスに属します。



locators.pyは次のようになります。



 from selenium.webdriver.common.by import By class MainPageLocators(object): """A class for main page locators. All main page locators should come here""" GO_BUTTON = (By.ID, 'submit') class SearchResultsPageLocators(object): """A class for search results locators. All search results locators should come here""" pass
      
      






All Articles