2015-12-05 54 views
1

你好,你可以給我舉例另一類重用的安裝和拆卸例如的Python的webdriver:重用的安裝和拆卸

base.py

from selenium import webdriver 
class Login(object): 
    def setUp(self): 
    # create a new Firefox session 
    self.driver = webdriver.Firefox() 
    self.driver.get("google.com") 
    self.driver.implicitly_wait(30) 
    self.driver.maximize_window() 

    def tearDown(self): 
    self.driver.quit() 

login.py

from selenium import webdriver 
import base 
import unittest 

class LoginPage(unittest.TestCase): 
...how to call setUp() and tearDown() from base.py... 
    def test_logn(self): 
     self.driver.find_element_by_id('username') 
    ...etc... 

你能提供例子請運行它在PC上,看看它是如何工作的。謝謝你

回答

1

使LoginPagebase.Login子類,因此它繼承Login.setUpLogin.tearDown

base.py

import unittest 
from selenium import webdriver 

class Login(unittest.TestCase): 

    def setUp(self): 
     # create a new Firefox session 
     self.driver = webdriver.Firefox() 
     self.driver.get("http://google.com") 
     self.driver.implicitly_wait(30) 
     self.driver.maximize_window() 

    def tearDown(self): 
     self.driver.quit() 

login.py

from selenium import webdriver 
import base 
import unittest 

class LoginPage(base.Login): 

    def test_logn(self): 
     elt = self.driver.find_element_by_name('q') 
     elt.send_keys('python') 
     elt.submit() 
     for result in self.driver.find_elements_by_class_name('g'): 
      print(result.text) 
      print('-' * 80) 

if __name__ == '__main__': 
    unittest.main() 
+0

當我寫蟒蛇login.py Firefox的犯規開始:( –

+0

你的'login.py'在最後有'unittest.main()'調用嗎? – unutbu

+0

它的工作原理謝謝 –