selenium webdriver基于Python系列文章之 百度首頁(yè)搜索測(cè)試用例3

#-*- coding:utf-8 -*-

from selenium import webdriver
import unittest
import HTMLTestRunner  
import sys
from time import sleep
import xlrd

reload(sys)
sys.setdefaultencoding("utf-8")

class LoadBaiduSearchTestData:
    def __init__(self, path):
        self.path = path
    
    def load_data(self):
        # 打開(kāi)excel文件
        excel = xlrd.open_workbook(self.path)
        
        # 獲取第一個(gè)工作表
        table = excel.sheets()[0]
        
        # 獲取行數(shù)
        nrows = table.nrows
        
        # 從第二行開(kāi)始遍歷數(shù)據(jù)
        # 存入一個(gè)list中
        test_data = []
        for i in range(1, nrows):
            test_data.append(table.row_values(i))
        
        # 返回讀取的數(shù)據(jù)列表    
        return test_data

class BaiduTest(unittest.TestCase):
    """百度首頁(yè)搜索測(cè)試用例"""
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = u"http://www.baidu.com"
        self.path = u"baidu_search.xlsx"
        
    def test_baidu_search(self):
        driver = self.driver
        print u"開(kāi)始[case_0001]百度搜索"
        
        
        # 加載測(cè)試數(shù)據(jù)
        test_excel = LoadBaiduSearchTestData(self.path)
        data = test_excel.load_data()
        print data
        
        # 循環(huán)參數(shù)化
        for d in data:
            # 打開(kāi)百度首頁(yè)
            driver.get(self.base_url)
            
            # 驗(yàn)證標(biāo)題
            self.assertEqual(driver.title, u"百度一下,你就知道")
            sleep(1)
            
            driver.find_element_by_id("kw").clear()
            # 參數(shù)化 搜索詞
            driver.find_element_by_id("kw").send_keys(d[1])
            sleep(1)
            driver.find_element_by_id("su").click()
            sleep(1)
            
            # 驗(yàn)證搜索結(jié)果標(biāo)題
            self.assertEqual(driver.title, d[2])
            sleep(2)
            
        
    def tearDown(self):
        self.driver.quit()
        

if __name__ == '__main__':
    testunit = unittest.TestSuite()
    testunit.addTest(BaiduTest('test_baidu_search'))    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容