BitTigerio 2018-02-28
import unittest<br />from time import sleep<br /><br />import ddt<br /># 导入ddt模块<br />from selenium import webdriver<br /><br /><br />@ddt.ddt<br /># 声明使用ddt<br />class MyTestCase(unittest.TestCase):<br /><br /> def setUp(self):<br /> self.driver = webdriver.Firefox()<br /> self.base_url = "https://www.baidu.com/"<br /> self.driver.get(self.base_url)<br /> self.driver.maximize_window()<br /> sleep(2)<br /><br /> @ddt.data(["中国", "中国_百度百科"],<br /> ["美国", "美国_百度百科"],<br /> ["英国", "英国_百度百科"])<br /> # 测试数据<br /><br /> @ddt.unpack<br /> # 对测试数据进行解包并传递<br /> def test_something(self, searchTerm, searchResult):<br /> driver = self.driver<br /> driver.find_element_by_xpath(".//*[@id='kw']").send_keys(searchTerm)<br /> driver.find_element_by_xpath(".//*[@id='su']").click()<br /> sleep(2)<br /> responseText = driver.find_element_by_xpath(".//*[@id='1']/h3/a").text<br /> self.assertEqual(responseText, searchResult)<br /><br /> def tearDown(self):<br /> self.driver.close()<br /> self.driver.quit()<br /><br />if __name__ == '__main__':<br /> unittest.main()