我目前有一个unittest.TestCase的子类,用于运行我的selenium测试:
class FunctionalTest(unittest.TestCase):
@classmethod
def setUpClass(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(1)
@classmethod
def tearDownClass(self):
self.browser.close()
def method1(self):
#some code here
def method2(self):
#some code here
我想继承这个类(FunctionalTest),我希望更改setUpClass方法以禁用firefox浏览器的javascript,但仍然可以使用FunctionalTest的所有其他方法:
@classmethod
def setUpClass(self):
profile = webdriver.FirefoxProfile()
profile.set_preference("javascript.enabled", False);
self.browser = webdriver.Firefox(profile)
self.browser.implicitly_wait(1)
如下所示的子类不起作用:
FunctionalTestNoJS(FunctionalTest):
@classmethod
def setUpClass(self):
profile = webdriver.FirefoxProfile()
profile.set_preference("javascript.enabled", False);
self.browser = webdriver.Firefox(profile)
self.browser.implicitly_wait(1)
关于如何有效地解决这个问题的任何想法?
答案 0 :(得分:1)
这对我有用,我不确定@classmethod的原因,但是这里你去..
import unittest
from selenium import webdriver
from datetime import datetime
class FunctionalTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(1)
self.browser.maximize_window()
def tearDown(self):
self.browser.quit()
def test_method1(self):
self.browser.get("http://javatester.org/javascript.html")
now = datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f')
self.browser.get_screenshot_as_file('%s_%s.png' % (self.__class__.__name__, now))
class FunctionalTestNoJS(FunctionalTest):
def setUp(self):
profile = webdriver.FirefoxProfile()
profile.set_preference("javascript.enabled", False);
self.browser = webdriver.Firefox(profile)
self.browser.implicitly_wait(1)
self.browser.maximize_window()
if __name__ == '__main__':
unittest.main()