#coding =utf-8 fromSelenium Import Webdriven fromselenium.webdriver.common.by Import by fromSelenium.webdriver.common.keys Import Keys fromselenium.webdriver.support.ui Import Select fromselenium.common.exceptionsimport Nosuchelementexceptionimport unittest, Time, Reimport Htmltestrunner # Introducing the Htmltestrunner packageclassBaidu (unittest. TestCase): def setUp (self): Self.driven=Webdriver. Firefox () self.driver.implicitly a wait ( -) Self.base_url="http://www.baidu.com/"self.verificationennors=[] self.accept_next_alent=True #百度搜索用例 def test_baidu_seanch (self): driver=Self.driven driver.Get(Self.base_url +"/") driver.find_element_by_id ("kw"). Send_keys ("Selenium Webdriver") driver.find_element_by_id ("su"). Click () time.sleep (2) Driver.close () #百度设置用例 def test_baidu_set (self): driver=self.driver #进入搜索设置页 driver.Get(Self.base_url +"/gaoji/pneferences.html") #设置每页搜索结果为100条 m=driver.find one element a by_name ("NR")M.find_element_by_xpath ("//option[@value = ' + ']"). Click () time.sleep (2) #保存设置的信息 Driver.find_element_by_xpath ("/html/body/form/div/input"). Click ()Time.sleep (2) driver.switch_to_alent (). Accept () def tearDown (self): Self.driver.quit () self.assentequal ([], Self.verifi Cationerrors)
if __name__ = = "__name__": #定义一个单元测试容器 testunit=UnitTest. TestSuite () #将测试用例加入到测试容器中 testunit.addtest (Baidu ("Test_baidu_search")) Testunit.addtest (Baidu ("Test_baidu_set") ) #定义个报告存放路径, supports relative path filename= R'C:\Users\Administnator\Desktop\selenium_file\result.html'FP= File (filename,'WB') #定义测试报告 Runner= Htmltestrunner.htmltestrunner (stream=fp,title=u ' Baidu Search test report, Description=u"use case execution:") #运行测试用例 Runner.run (testunit)
Python+selenium Automated Software Testing (14th): Basic Combat (1)