#coding =utf-8from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common import keysfrom selenium.webdriver.support import selectfrom selenium.common import exceptionsimport unittest,time,reclass Baidu (unittest. TestCase): #setUp used to set the initialization part, the function in this method will be called first before the test case executes. This places the invocation of the browser and the access of the URL into the initialization section. # def setup (self): Self.driver=webdriver. Firefox () self.driver.implicitly_wait () self.base_url= ' http://www.baidu.com/' self.verificationErrors=[] #脚本运行时, the wrong information will be printed to this list # self.accept_next_alert=True #是否继续接受下一个警告 # #百度搜索用例 &nbSp;def test_baidu_serch (self): driver=self.driver 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.quit () #百度设置用例 def test_baidu_set (self): driver=self.driver #进入搜索设置页 Driver.get (self.base_url+ ' gaoji/preferences.html ') M=driver.find_element_by_name (' NR ') #设置每页搜索结果为100条, first find id= Nr label, and then find the option tag under the Id=nr tabA Select button with value equal to 100 m.find_element_by_xpath ("//option[@value = ' 100 ' ]. Click () time.sleep (2) #保存设置的信息 driver.find_element_by_xpath ("//input[@ Value= ' Save Settings ']. Click () time.sleep (2) n=driver.switch_to_alert () n.accept () def teardown (self): Self.driver.quit () self.assertequal ([],self.verificationerrors) ' teardown The method is called after each test method executes, and this place does all the cleanup work, such as exiting the browser. self.assertequal ([], self.verificationerrors)   is a difficult point, compare the list obtained by the previous Verificationerrors method If the list of verificationerrors is not empty, the error message in the output list. "' if __name__==" __main__ ": unittest.main () #执行用例 # ' execution results are as follows:ran 2 tests in 30.719s executes all test cases, with 70.719sok no problem if you deliberately set an error in the use case, for example: Baidu set up the use case to add such a line of code, Find the name of the DR label and click it, because this is no, cannot find this tag, the system will error, Driver.find_element_by_name (' DR '). Click () The execution results are shown below:ran 2 tests in 70.719sfailed (Errors=1) "
This article is from the "Cheng Technology blog" blog, make sure to keep this source http://zhengshuheng.blog.51cto.com/1439780/1574827
Python Automation Test Example--a simple automated test case script--batch execution of test cases