这是我尝试过的以下脚本
import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
class HomePageTest (unittest.TestCase):
def setUpClass(self):
self.driver = webdriver.Firefox()
self.driver.get("http://magento-demo.lexiconn.com/")
self.driver.maximize_window()
def test_searchbox(self):
driver=self
driver.assertTrue (driver.is_element_present (By.ID,"search"))
driver.assertTrue (driver.driver.find_element_by_id("search").is_enabled())
def tearDownClass(self):
self.driver.quit()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
if __name__ == '__main__':
unittest.main(verbosity=2)
最佳答案
您需要使setUpClass-和tearDownClass-类方法:
@classmethod
def setUpClass(cls):
...
@classmethod
def tearDownClass(cls):
...
请参见the unittest docs。