Python+selenium之带unittest的脚本分析
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 from selenium.webdriver.common.keys import Keys 4 from selenium.webdriver.support.ui import Select 5 from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException 6 import unittest, time, re 7 8 class Baidu(unittest.TestCase): 9 def setUp(self): 10 self.driver = webdriver.Firefox() 11 self.driver.implicitly_wait(30) 12 self.base_url = "http://www.baidu.com/" 13 self.verificationErrors = [] 14 self.accept_next_alert = True 15 16 def test_baidu(self): 17 driver = self.driver 18 driver.get(self.base_url + "/") 19 driver.find_element_by_id("kw").send_keys("selenium webdriver") 20 driver.find_element_by_id("su").click() 21 driver.close() 22 23 def is_element_present(self, how, what): 24 try: 25 self.driver.find_element(by=how, value=what) 26 except NoSuchElementException as e: 27 return False 28 return True 29 30 def is_alert_present(self): 31 try: 32 self.driver.switch_to_alert() 33 except NoAlertPresentException as e: 34 return False 35 return True 36 37 def close_alert_and_get_its_text(self): 38 try: 39 alert = self.driver.switch_to_alert() 40 alert_text = alert.text 41 if self.accept_next_alert: 42 alert.accept() 43 else: 44 alert.dismiss() 45 return alert_text 46 finally: 47 self.accept_next_alert = True 48 49 def tearDown(self): 50 self.driver.quit() 51 self.assertEqual([], self.verificationErrors) 52 53 if __name__ == "__main__": 54 unittest.main()
****注:以上代码适用与Python2.7版本,Python3.0版本会报错
import unittest 1.首先导入unittest模块 class BaiduTest(unittest.TestCase) 2.BaiduTest类继承unittest框架的TestCase类成为标准测试类
def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "http://www.baidu.com/" self.verificationErrors = [] self.accept_next_alert = True 1.setUp用于设置初始化工作,在执行每一测试用例前先被执行,它与tearDown方法相呼应,后者在每一个测试用例执行后被执行。初始化工作定义了浏览器启动和基础URL地址。 2.implicitly_wait()设置页面上元素的隐性等待时间为30s 3.verificationErrors数组,脚本运行时的错误信息将被记录到这个数组中。 4.定义accept_next_alert变量,表示是否继续接受下一个警告,初始状态为True
def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except NoSuchElementException as e: return False return True 1.is_element_present方法用于查找页面元素是否存在,通过find_element()来接收元素的定位方法(how)和定位值(what)。
如果定位到元素则返回True。否则爆出异常并返回False。try.....except....为Python语言的异常处理。
def is_alert_present(self): try: self.driver.switch_to_alert() except NoAlertPresentException as e: return False return True 1.is_alert_prent()方法用于判断当前页面是否存在警告框,利用WebDriver提供的switch_to_alert()方法来捕捉页面上的警告框。 如果捕捉到警告框则返回True。否者跑输NoAlertPrentException类型的异常,并返回False 2.鉴于不管页面是否出现警告框,返回结果都为True。所以可以通过dirver.switch_to_alert().text,用于获取当前页面上的警告提示信息。可以获取到就返回True,获取不到则返回False。
def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True 1.close_alert_and_get_its_text()关闭警告并获得警告信息。 2.通过switch_to_alert()获得警告,通过text获得警告框的信息。 3.然后通过if语句判断accept_naxt_alert的状态,在setUp()中已经初始化状态为True,如果为True,则通过accept()接受警告,否者dismiss()忽略警告。
def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) 1.tearDown()方法在每个测试方法执行后调用,这个方法用于测试用例执行后的清理工作。如退出浏览器,关闭驱动,恢复用例执行状态。 2.在setUp()方法中定义了verificationErrors为空数组,这里通过assertEqual()比较其是否为空,如果为空则说明用例执行的过程中没有出现异常,否则将抛出AssertionErrory异常。
if __name__ == "__main__": unittest.main() 通过unittest。main()方法来运行当前文件中的测试方法,其默认匹配并运行以test开头的方法。
**********************************************************************************************************************************************************************************************************************************
以上代码从《虫师》copy,但是会报如下的错误
解决的办法:注释掉driver.close()
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 from selenium.webdriver.common.keys import Keys 4 from selenium.webdriver.support.ui import Select 5 from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException 6 import unittest, time, re 7 8 9 class Baidu (unittest.TestCase): 10 def setUp(self): 11 self.driver = webdriver.Firefox () 12 self.driver.implicitly_wait (30) 13 self.base_url = "http://XXXXXXXX /oneCard/login" 14 self.verificationErrors = [] 15 self.accept_next_alert = True 16 17 def test_baidu(self): 18 driver = self.driver 19 driver.get (self.base_url + "/") 20 driver.find_element_by_name ("username").clear () 21 driver.find_element_by_name ("username").send_keys ("super") 22 driver.find_element_by_name ("password").clear () 23 driver.find_element_by_name ("password").send_keys ("XXXXXXX") 24 driver.find_element_by_xpath ("/html/body/div[1]/div/div/div/div[2]/form/fieldset/div[4]/button").click () 25 time.sleep (3) 26 27 # 进入卡入库界面 28 driver.find_element_by_xpath ("//*[@id='sidebar-menu']/div/ul/li[1]/a").click () 29 driver.find_element_by_xpath ("//*[@id='sidebar-menu']/div/ul/li[1]/ul/li[1]/a").click () 30 time.sleep (3) 31 32 # 点击卡入库,进入卡入库 33 driver.switch_to.frame (driver.find_element_by_xpath ("//*[@id='tabBody']/div[2]/div/iframe")) 34 driver.find_element_by_link_text ("入库").click () 35 time.sleep (3) 36 driver.switch_to.default_content () 37 driver.switch_to.frame (driver.find_element_by_xpath ("//*[@id='tabBody']/div[3]/div/iframe")) 38 driver.find_element_by_id ("cardNo").clear () 39 driver.find_element_by_id ("cardNo").send_keys ("12345617") 40 41 time.sleep (3) 42 driver.find_element_by_id ("cardChipId").clear () 43 driver.find_element_by_id ("cardChipId").send_keys ("12345617") 44 # 输入外部成本 和内部成本 45 driver.find_element_by_id ("costInternal").clear () 46 driver.find_element_by_id ("costInternal").send_keys (1) 47 time.sleep (2) 48 driver.find_element_by_id ("costExternal").clear () 49 driver.find_element_by_id ("costExternal").send_keys (10) 50 # 提交 51 driver.find_element_by_xpath ("//*[@id='btnSubmit']").click () 52 53 def is_element_present(self, how, what): 54 try: 55 self.driver.find_element (by=how, value=what) 56 except NoSuchElementException as e: 57 return False 58 return True 59 60 def is_alert_present(self): 61 try: 62 self.driver.switch_to_alert () 63 except NoAlertPresentException as e: 64 return False 65 return True 66 67 def close_alert_and_get_its_text(self): 68 try: 69 alert = self.driver.switch_to_alert () 70 alert_text = alert.text 71 print(alert_text) 72 if self.accept_next_alert: 73 alert.accept () 74 else: 75 alert.dismiss () 76 return alert_text 77 finally: 78 self.accept_next_alert = True 79 80 def tearDown(self): 81 self.driver.quit () 82 self.assertEqual ([], self.verificationErrors) 83 84 85 if __name__ == "__main__": 86 unittest.main ()