python
  • selenium
  • nose
  • 2016-06-17 2 views 3 likes 
    3

    最近私は新しいプロジェクトに切り替えました。私たちのすべてのセレニウムテストはPythonで書かれています。 click.view_buttonデコレータを使用したリファクタリングによるコード量の削減

    ような何かにその全体 self.find(self.view_button).click()を最小限にする方法がある私は考えている

    class BasePage(object): 
        view_button = ".//a[text()='View']" 
        create_button = ".//a[text()='Create']" 
        #some code here 
    
    class BaseTestCase(unittest.TestCase): 
        setUpclass(cls): 
        #code here 
    
        def find(cls,xpath): 
         return cls.driver.find_element_by_xpath(xpath) 
    
    
    class SomeTest(BaseTestCase): 
        def test_00_something(self): 
         self.find(self.view_button).click() 
    

    :私はデコレータ

    を使用して、コード量を減らすことができれば、私たちは今持っているかと思いました

    私はそれがデコレータを使って行うことができると聞きましたが、私はそれでほとんど成功しなかったJavaの人です。

    答えて

    2

    と同じように動作テストケースに

    class ViewButton(): 
    
        def __init__(self,driver): 
         self.driver = driver 
         self.locator = (".//a[text()='View']") 
    
        @property 
        def click(self): 
         return self.driver.find_element_by_xpath(self.locator).click() 
    
    class BaseTestCase(unittest.TestCase): 
        #some code here 
    
        @property 
        def view(self): 
         view = ViewButton(self.driver) 
         return view 
    

    下記参照します。新しいモジュールを作成する - とのnavigation.py:

    basetestcase.pyで
    class Button(): 
    
        def __init__(self,driver, locator): 
         self.driver = driver 
         self.locator = locator 
    
        @property 
        def click(self): 
         return self.driver.find_element_by_xpath(self.locator).click() 
    
    class Navigation(): 
    
        """NAVIGATION COMMANDS """ 
        def goTo(self): 
         #somethign 
    
        def previousPage(self): 
         #something 
    
        """ BUTTONS """ 
        @property 
        def view_button(self): 
         xpath = ".//a[text()='View']" 
         view = Button(self.driver,xpath) 
         return view 
    
        @property 
        def create_button(self): 
         xpath = ".//a[text()='Create']" 
         create = Button(self.driver,xpath) 
         return create 
    

    class BaseTestCase(unittest.TestCase, Navigation) 
    
         setUpClass(cls): 
         #somethign here 
    

    とテストケースは次のようになります。

    class TestSomething(BaseTestCase): 
    
        def test_99_somethign(self): 
         #finds .//a[text()='View'] and clicks 
         self.view.click 
    
         #create button 
         self.create_button.click 
    

    をこのように、必要になりますあなたのテストの中でナビゲーションクラスを使うことができます。さらに、すべてのナビゲーション要素を1か所にまとめることができます。

    1

    どうすればいいのか分かりません。click.view_buttonです。しかし、独自のクラスを要素として与えることができます。このようにしてview_button.click()のようなものを実現できます。私のプロジェクトでは、私のすべての要素に対してこれをやっています。私は解決策を見つけたRemcoWから投稿する

    class BaseElement: 
        def __init__(self, driver, locator): 
         self.driver = driver 
         self.locator = locator 
    
        def get(self): 
         self.driver.find_element(locator) 
    
        def click(): 
         self.get().click() 
    
    
    class ViewButton(BaseElement): 
        def __init__(self, driver): 
         locator = (By.XPATH, ".//a[text()='View']") 
         super(ViewButton, self).__init__(driver, locator) 
    
    
    class BasePage(object): 
        def __init__(self, driver): 
         self.view_button = ViewButton(driver) 
    
    
    class BaseTestCase(unittest.TestCase): 
        setUpclass(cls): 
         # driver stuff 
         self.page = BasePage(driver) 
    
    
    class SomeTest(BaseTestCase): 
        def test_00_something(self): 
         self.page.view_button.click() 
    
    1

    おかげで、それはあなたがまた、次の解決策を確認することができ、この

    class TestThign(BaseTestCase): 
    
        def test_00_something(self): 
         self.view.click 
    
    関連する問題