Python多线程应用于自动化测试操作示例
作者:oOo右右 时间:2021-04-09 19:20:58
本文实例讲述了Python多线程应用于自动化测试操作。分享给大家供大家参考,具体如下:
多线程执行测试用例
实例:
import threading
from time import sleep,ctime
from selenium import webdriver
#测试用例1
def test_baidu(browser,search):
print("开始,现在时间是%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("//*[@id='kw']").send_keys(search)
driver.find_element_by_id("su").click()
sleep(2)
driver.quit()
if __name__ =="__main__":
#定义字典存放test_baidu函数的参数
dicts = {"ff": "python", "chrome": "selenium", "ie": "unittest"}
#创建空列表存放线程
threads =[]
#遍历字典,并把线程append进threads
for browser,search in dicts.items():
t = threading.Thread(target=test_baidu,args=(browser,search))
threads.append(t)
#把字典内容索引替代
file = range(len(dicts))
#启动线程
for i in file:
threads[i].start()
#守护线程
for i in file:
threads[i].join()
测试结果:
由测试结果得到:多线程同时开始执行测试用例,大大缩小测试总时间。
多线程结合分布式执行测试用例
Selenium Girl 只是提供了多操作系统、多浏览器的执行环境,但不提供并行执行测试用例,Selenium Girl只能现在一个系统或浏览器上执行完测试用例外再从另一个操作系统或浏览器上执行测试用例。
启动Selenium server
在本机启动一个主hub和一个node节点:本机IP地址(192.168.0.101),节点端口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
测试脚本:
'''
在不同主机、不同浏览器同时执行测试用例,多线程;
利用Selenium Girl提供分布式执行测试用例;
先启动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__':
#启动参数,指定运行主机和浏览器
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' #远程节点node
}
threads =[]
files = range(len(lists))
#创建线程,并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程序设计有所帮助。
来源:https://blog.csdn.net/JOJOY_tester/article/details/54236473
标签:Python,多线程,自动化测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中的并发编程asyncio库入门使用
2021-02-13 05:03:52
详解pytorch 0.4.0迁移指南
2023-04-10 08:02:14
![](https://img.aspxhome.com/file/2023/0/84920_0s.jpg)
python识别图像并提取文字的实现方法
2023-06-07 18:36:36
![](https://img.aspxhome.com/file/2023/3/85383_0s.jpg)
Anaconda+pycharm安装及环境配置全过程
2023-05-20 09:40:14
![](https://img.aspxhome.com/file/2023/3/95703_0s.png)
Django实现auth模块下的登录注册与注销功能
2022-03-06 17:49:15
![](https://img.aspxhome.com/file/2023/4/85124_0s.jpg)
登录与注册两者的距离
2009-01-02 16:48:00
![](https://img.aspxhome.com/file/UploadPic/20091/2/xanyou1-36s.png)
python中itertools模块使用小结
2023-08-07 18:44:32
TensorFlow深度学习之卷积神经网络CNN
2022-07-03 17:43:15
![](https://img.aspxhome.com/file/2023/5/91885_0s.jpg)
Oracle收购TimesTen 提高数据库软件性能
2010-07-21 13:03:00
Python中最大递归深度值的探讨
2021-01-16 14:21:21
![](https://img.aspxhome.com/file/2023/8/80608_0s.png)
使用Filter实现信息的二次检索
2007-10-08 19:19:00
![](https://img.aspxhome.com/file/UploadPic/200710/8/2007108192555888s.gif)
python开发中两个list之间传值示例
2022-06-07 03:44:54
![](https://img.aspxhome.com/file/2023/7/87877_0s.jpg)
用户分类浅谈
2009-09-27 12:14:00
![](https://img.aspxhome.com/file/UploadPic/20099/27/about_users_cat-banner-14s.jpg)
设计角色的分工和配合
2008-10-20 12:08:00
![](https://img.aspxhome.com/file/UploadPic/200810/20/2008102012945335s.jpg)
常用正则表达式例子及评注说明
2008-01-03 13:04:00
如何从Python字符串中删除最后一个分号或者逗号
2023-12-22 07:29:53
![](https://img.aspxhome.com/file/2023/0/79470_0s.png)
Django实现文章详情页面跳转代码实例
2023-08-29 05:41:47
![](https://img.aspxhome.com/file/2023/7/62547_0s.png)
PHP字符串前后字符或空格删除方法介绍
2023-05-29 05:47:56
Oracle也有注入漏洞
2010-07-23 13:03:00
python非标准时间的转换
2022-04-12 17:37:52