Python多線程應(yīng)用于自動(dòng)化測(cè)試操作示例
本文實(shí)例講述了Python多線程應(yīng)用于自動(dòng)化測(cè)試操作。分享給大家供大家參考,具體如下:
多線程執(zhí)行測(cè)試用例
實(shí)例:
import threading
from time import sleep,ctime
from selenium import webdriver
#測(cè)試用例1
def test_baidu(browser,search):
print("開始,現(xiàn)在時(shí)間是%s"%ctime())
print("瀏覽器是%s"%browser)
if browser =="ff":
driver = webdriver.Firefox()
elif browser =="chrome":
driver = webdriver.Chrome()
elif browser =="ie":
driver =webdriver.Ie()
else:
print("瀏覽器輸入錯(cuò)誤!")
driver.get(r"http://www.baidu.com")
driver.implicitly_wait(5)
driver.find_element_by_xpath("http://*[@id='kw']").send_keys(search)
driver.find_element_by_id("su").click()
sleep(2)
driver.quit()
if __name__ =="__main__":
#定義字典存放test_baidu函數(shù)的參數(shù)
dicts = {"ff": "python", "chrome": "selenium", "ie": "unittest"}
#創(chuàng)建空列表存放線程
threads =[]
#遍歷字典,并把線程append進(jìn)threads
for browser,search in dicts.items():
t = threading.Thread(target=test_baidu,args=(browser,search))
threads.append(t)
#把字典內(nèi)容索引替代
file = range(len(dicts))
#啟動(dòng)線程
for i in file:
threads[i].start()
#守護(hù)線程
for i in file:
threads[i].join()
測(cè)試結(jié)果:

由測(cè)試結(jié)果得到:多線程同時(shí)開始執(zhí)行測(cè)試用例,大大縮小測(cè)試總時(shí)間。
多線程結(jié)合分布式執(zhí)行測(cè)試用例
Selenium Girl 只是提供了多操作系統(tǒng)、多瀏覽器的執(zhí)行環(huán)境,但不提供并行執(zhí)行測(cè)試用例,Selenium Girl只能現(xiàn)在一個(gè)系統(tǒng)或?yàn)g覽器上執(zhí)行完測(cè)試用例外再從另一個(gè)操作系統(tǒng)或?yàn)g覽器上執(zhí)行測(cè)試用例。
啟動(dòng)Selenium server
在本機(jī)啟動(dòng)一個(gè)主hub和一個(gè)node節(jié)點(diǎn):本機(jī)IP地址(192.168.0.101),節(jié)點(diǎn)端口4444/5555
java -jar selenium-server-standalone-2.48.0.jar -role hub java -jar selenium-server-standalone-2.48.0.jar -role node -port 5555
啟動(dòng)一個(gè)遠(yuǎn)程node:p(192.168.0.102)在虛擬機(jī)里面:端口是6666
java -jar selenium-server-standalone-2.48.0.jar -role node -port 6666 -hub http://192.168.0.101:4444/gird/register
測(cè)試腳本:
'''
在不同主機(jī)、不同瀏覽器同時(shí)執(zhí)行測(cè)試用例,多線程;
利用Selenium Girl提供分布式執(zhí)行測(cè)試用例;
先啟動(dòng)Selenium server,這里分別在本地主機(jī)啟動(dòng)一個(gè)hub和一個(gè)node,在其他主機(jī)(虛擬機(jī))啟動(dòng)一個(gè)node。
'''
from selenium.webdriver import Remote
import threading
from time import *
#測(cè)試用例
def test_baidu(host,browser):
print("開始:%s"%ctime())
print(host,browser)
dc ={'browserName':browser}
driver = Remote(
command_executor=host,
desired_capabilities=dc
)
driver.get(r'http://www.baidu.com')
driver.implicitly_wait(10)
driver.find_element_by_link_text("新聞").click()
sleep(2)
driver.get_screenshot_as_file(r'D:\testscreen\baidu.jpg')
driver.quit()
if __name__ =='__main__':
#啟動(dòng)參數(shù),指定運(yùn)行主機(jī)和瀏覽器
lists={'http://127.0.0.1:4444/wd/hub':'chrome',
'http://127.0.0.1:5555/wd/hub':'internet explorer',
'http://192.168.216.128:6666/wd/hub':'firefox' #遠(yuǎn)程節(jié)點(diǎn)node
}
threads =[]
files = range(len(lists))
#創(chuàng)建線程,并append進(jìn)線程組
for host,browser in lists.items():
t = threading.Thread(target=test_baidu,args=(host,browser))
threads.append(t)
#啟動(dòng)每一個(gè)線程
for i in files:
threads[i].start()
#守護(hù)每一個(gè)線程
for i in files:
threads[i].join()
運(yùn)行結(jié)果:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設(shè)計(jì)入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 使用Python自動(dòng)化Microsoft Excel和Word的操作方法
- python 自動(dòng)化偷懶的四個(gè)實(shí)用操作
- python自動(dòng)化辦公操作PPT的實(shí)現(xiàn)
- python+excel接口自動(dòng)化獲取token并作為請(qǐng)求參數(shù)進(jìn)行傳參操作
- Python word文本自動(dòng)化操作實(shí)現(xiàn)方法解析
- Python自動(dòng)化測(cè)試中yaml文件讀取操作
- Python自動(dòng)化操作實(shí)現(xiàn)圖例繪制
- Python鍵鼠操作自動(dòng)化庫PyAutoGUI簡(jiǎn)介(小結(jié))
- Python如何操作office實(shí)現(xiàn)自動(dòng)化及win32com.client的運(yùn)用
- 利用Python自動(dòng)化操作AutoCAD的實(shí)現(xiàn)
- python+Selenium自動(dòng)化測(cè)試——輸入,點(diǎn)擊操作
- python自動(dòng)化測(cè)試之異常及日志操作實(shí)例分析
- Python自動(dòng)化完成tb喵幣任務(wù)的操作方法
- python操作excel讓工作自動(dòng)化
- Python自動(dòng)化運(yùn)維之Ansible定義主機(jī)與組規(guī)則操作詳解
- selenium+python自動(dòng)化測(cè)試之使用webdriver操作瀏覽器的方法
- 十個(gè)Python自動(dòng)化常用操作,即拿即用
相關(guān)文章
使用pyinstaller打包django的方法實(shí)現(xiàn)
本文主要介紹了使用pyinstaller打包django的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Tensorflow設(shè)置顯存自適應(yīng),顯存比例的操作
今天小編就為大家分享一篇Tensorflow設(shè)置顯存自適應(yīng),顯存比例的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python中字節(jié)數(shù)組和16進(jìn)制字符串轉(zhuǎn)換方式
這篇文章主要介紹了Python中字節(jié)數(shù)組和16進(jìn)制字符串轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Python處理JSON時(shí)的值報(bào)錯(cuò)及編碼報(bào)錯(cuò)的兩則解決實(shí)錄
這篇文章主要介紹了Python處理JSON時(shí)的值報(bào)錯(cuò)及編碼報(bào)錯(cuò)的兩則解決實(shí)錄,在這里還是想建議一下使用Python 3.x版本,Python 3默認(rèn)的Unicode編碼能在實(shí)際使用中為我們省去不少問題,需要的朋友可以參考下2016-06-06
Python數(shù)據(jù)分析numpy文本數(shù)據(jù)讀取索引切片實(shí)例詳解
這篇文章主要為大家介紹了Python數(shù)據(jù)分析numpy文本數(shù)據(jù)讀取索引切片實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
python利用7z批量解壓rar的實(shí)現(xiàn)
這篇文章主要介紹了python利用7z批量解壓rar的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Django中session進(jìn)行權(quán)限管理的使用
本文主要介紹了Django中session進(jìn)行權(quán)限管理的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
python中以函數(shù)作為參數(shù)(回調(diào)函數(shù))的實(shí)現(xiàn)方法
這篇文章主要介紹了python中以函數(shù)作為參數(shù)(回調(diào)函數(shù))的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

