目錄unittest可以為webdriver增加setup、teardown、檢查應(yīng)用狀態(tài)、報(bào)告測試結(jié)果、數(shù)據(jù)驅(qū)動(dòng)等功能。主要內(nèi)容如下:
unittest庫此部分后面會(huì)與 python模塊介紹-unittest:單元測試框架 整合。鏈接中的地址為準(zhǔn)。 主要組成如下:
測試通常由3A組成:
其他框架有Pytest和Nose等。 TestCase類import unittest from selenium import webdriver class SearchTests(unittest.TestCase): def setUp(self): # create a new Firefox session self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.driver.maximize_window() # navigate to the application home page self.driver.get("http://demo./") def test_search_by_category(self): # get the search textbox self.search_field = self.driver.find_element_by_name("q") self.search_field.clear() # enter search keyword and submit self.search_field.send_keys("phones") self.search_field.submit() # get all the anchor elements which have product names displayed # currently on result page using find_elements_by_xpath method products = self.driver .find_elements_by_xpath("http://h2[@class='product-name']/a") self.assertEqual(2, len(products)) def test_search_by_name(self): # get the search textbox self.search_field = self.driver.find_element_by_name("q") self.search_field.clear() # enter search keyword and submit self.search_field.send_keys("salt shaker") self.search_field.submit() # get all the anchor elements which have product names displayed # currently on result page using find_elements_by_xpath method products = self.driver. find_elements_by_xpath("http://h2[@class='product-name']/a") self.assertEqual(1, len(products)) def tearDown(self): # close the browser window self.driver.quit() if __name__ == '__main__': unittest.main(verbosity=2) 需要選擇一個(gè)合適IDE,要求如下:
推薦:WingIDE,PyCharm,PyDev Eclipse plugin,PyScripter。相關(guān)下載地址如下: 本文地址
參考資料
|
|