Python多線程應用于自動化測試操作示例
本文實例講述了Python多線程應用于自動化測試操作。分享給大家供大家參考,具體如下:
多線程執(zhí)行測試用例
實例:
import threading
from time import sleep,ctime
from selenium import webdriver
#測試用例1
def test_baidu(browser,search):
print("開始,現(xiàn)在時間是%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("瀏覽器輸入錯誤!")
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進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))
#啟動線程
for i in file:
threads[i].start()
#守護線程
for i in file:
threads[i].join()
測試結果:

由測試結果得到:多線程同時開始執(zhí)行測試用例,大大縮小測試總時間。
多線程結合分布式執(zhí)行測試用例
Selenium Girl 只是提供了多操作系統(tǒng)、多瀏覽器的執(zhí)行環(huán)境,但不提供并行執(zhí)行測試用例,Selenium Girl只能現(xiàn)在一個系統(tǒng)或瀏覽器上執(zhí)行完測試用例外再從另一個操作系統(tǒng)或瀏覽器上執(zhí)行測試用例。
啟動Selenium server
在本機啟動一個主hub和一個node節(jié)點:本機IP地址(192.168.0.101),節(jié)點端口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
啟動一個遠程node:p(192.168.0.102)在虛擬機里面:端口是6666
java -jar selenium-server-standalone-2.48.0.jar -role node -port 6666 -hub http://192.168.0.101:4444/gird/register
測試腳本:
'''
在不同主機、不同瀏覽器同時執(zhí)行測試用例,多線程;
利用Selenium Girl提供分布式執(zhí)行測試用例;
先啟動Selenium server,這里分別在本地主機啟動一個hub和一個node,在其他主機(虛擬機)啟動一個node。
'''
from selenium.webdriver import Remote
import threading
from time import *
#測試用例
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__':
#啟動參數(shù),指定運行主機和瀏覽器
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' #遠程節(jié)點node
}
threads =[]
files = range(len(lists))
#創(chuàng)建線程,并append進線程組
for host,browser in lists.items():
t = threading.Thread(target=test_baidu,args=(host,browser))
threads.append(t)
#啟動每一個線程
for i in files:
threads[i].start()
#守護每一個線程
for i in files:
threads[i].join()
運行結果:

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設計入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
- 使用Python自動化Microsoft Excel和Word的操作方法
- python 自動化偷懶的四個實用操作
- python自動化辦公操作PPT的實現(xiàn)
- python+excel接口自動化獲取token并作為請求參數(shù)進行傳參操作
- Python word文本自動化操作實現(xiàn)方法解析
- Python自動化測試中yaml文件讀取操作
- Python自動化操作實現(xiàn)圖例繪制
- Python鍵鼠操作自動化庫PyAutoGUI簡介(小結)
- Python如何操作office實現(xiàn)自動化及win32com.client的運用
- 利用Python自動化操作AutoCAD的實現(xiàn)
- python+Selenium自動化測試——輸入,點擊操作
- python自動化測試之異常及日志操作實例分析
- Python自動化完成tb喵幣任務的操作方法
- python操作excel讓工作自動化
- Python自動化運維之Ansible定義主機與組規(guī)則操作詳解
- selenium+python自動化測試之使用webdriver操作瀏覽器的方法
- 十個Python自動化常用操作,即拿即用
相關文章
Python數(shù)據(jù)持久化shelve模塊用法分析
這篇文章主要介紹了Python數(shù)據(jù)持久化shelve模塊用法,結合實例形式較為詳細的總結分析了shelve模塊的功能、原理及簡單使用方法,需要的朋友可以參考下2018-06-06
Queue 實現(xiàn)生產(chǎn)者消費者模型(實例講解)
下面小編就為大家?guī)硪黄猀ueue 實現(xiàn)生產(chǎn)者消費者模型(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
python?plotly設置go.Scatter為實線實例
這篇文章主要為大家介紹了python?plotly設置go.Scatter為實線線條的樣式實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
Python實現(xiàn)Mysql數(shù)據(jù)庫連接池實例詳解
這篇文章主要介紹了Python實現(xiàn)Mysql數(shù)據(jù)庫連接池實例詳解的相關資料,需要的朋友可以參考下2017-04-04
Python雙向循環(huán)鏈表實現(xiàn)方法分析
這篇文章主要介紹了Python雙向循環(huán)鏈表,結合實例形式分析了Python雙向鏈表的定義、遍歷、添加、刪除、搜索等相關操作技巧,需要的朋友可以參考下2018-07-07

