您好,您能给我一个在另一个类中重用setUp和tearDown的示例吗

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上运行它并查看其工作原理。谢谢

最佳答案

使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()

关于python - Python Webdriver:重用setUp和tearDown,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34107414/

10-12 02:02