#!-*- coding:utf-8 -*-
import unittest
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
'''斷言的測(cè)試應(yīng)用'''
from selenium import webdriver
class Assertions(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
self.driver.implicitly_wait(30) ?#隱式等待
self.driver.maximize_window() #全屏
self.base_url='http://www.baidu.com'
#腳本運(yùn)行時(shí),錯(cuò)誤的信息將打印到這個(gè)列表中
self.verificationErrors=[]
#是否接受下一個(gè)A警告
self.accept_next_alert=True
def testAssertion(self):
'''斷言的測(cè)試'''
driver=self.driver
driver.get(self.base_url+'/')
#斷言來(lái)判斷title是否正確
try:
self.assertEqual(u'百度一下惧笛,你就知道',driver.title)
except AssertionError as e:
self.verificationErrors.append(str(e))
'''測(cè)試錯(cuò)誤的截圖'''
def testImage(self):
'''錯(cuò)誤截圖的獲取'''
driver=self.driver
driver.get('http://www.baidu.com')
try:
driver.find_element_by_id('kw1ffg').send_keys('webdriver')
except:
driver.get_screenshot_as_file('這里添加存儲(chǔ)圖片文件本地路徑/image/error_png.png')
def tearDown(self):
driver=self.driver
driver.close()
self.assertEqual([],self.verificationErrors)
if __name__=='__main__':
#添加測(cè)試用例
suite.addTest(Assertions('testAssertion'))
#suite.addTest(Assertions('testImage'))
#unittest.makeSuite(Assertions,'test_case')
#執(zhí)行測(cè)試用例
runner=unittest.TextTestRunner()
#執(zhí)行用例
runner.run(suite)