当前位置:网站首页>阿里p8手把手教你,自动化测试应该如何实现多线程?赶紧码住
阿里p8手把手教你,自动化测试应该如何实现多线程?赶紧码住
2022-07-07 06:12:00 【软件测试呀】
进程
进程就是一个程序在一个数据集上的一次动态执行过程,我们编写的程序用来描述进程要完成哪些功能以及如何完成。
线程
线程页脚轻量级进程,他是一个基本的CPU执行单元,是进程中的实现,线程的出现是为了降低上下文切换的小号,提高系统的并发性。
线程与进程
一个线程只能属于一个进程,而一个进程可以有多个线程。
但是少有一个线程资源分配给进程,同一进程的所有线程共享该进程的所有资源。
CPU分给线程,即真正在CPU上运行的线程。
多线程原理应用
1、并行与并发
并行:每个线程分配给独立的核心,线程同时运行。
并发:多个线程在单个核心运行,统一时间一个线程运行,系统不停切换线程,看起来像是同时运行,实际上是线程不停切换。
2、Python的多线程
GIL 全局计时器锁:摸个线程想要执行必须先拿到GIL。
Python的多线程:其实同一时间只能运行一个线程,但是能实现并发。
3、Python多线程应用
不同代码运行效率不一样,我们可以通过多线程,形成并发,实现提高效率。
案例:Web自动化,其实CPU执行完一次命令,大部分时间是在等待,那么这段时间,CPU会限制或者做其他进程的任务,因此我们可以使用多线程,实现多浏览器自动化同时运行,从而实现高效率。
from appium import webdriver
from appium.webdriver.extensions.android.nativekey import AndroidKey
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait
import time
from ddt import ddt,data,unpack
import threading
data_sum = [
{'sum_a': 2, 'sum_b': 3},
{'sum_a': 2, 'sum_b': 3},
{'sum_a': 5, 'sum_b': 10}
]
def run_first():
desired_caps = {
'platformName': 'Android', # 被测手机是安卓
'platformVersion': '7.1.2', # 手机安卓版本
'deviceName': 'xiaomi_mix', # 设备名,安卓手机可以随意填写
'appPackage': 'com.chinatower.fghd.customer', # 启动APP Package名称
'appActivity': 'com.ckd.fgbattery.activity.User_Login_Activity', # 启动Activity名称
'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True
'resetKeyboard': True, # 执行完程序恢复原来输入法
'noReset': True, # 不要重置App
'newCommandTimeout': 5000,
'UDID':'127.0.0.1:62001',
'automationName': 'uiAutomator2'
# 'app': r'd:\apk\bili.apk',
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
#login_pwd(1,1)
pwd = driver.find_element_by_xpath('//*[@text = "密码登录"]')
print(pwd.get_attribute('text'))
TouchAction(driver).tap(pwd).perform()
pwd.click()
driver.find_element_by_id("user_phone_et").send_keys('1')
driver.find_element_by_id("sms_code_et").send_keys('1')
driver.find_element_by_id("btn_res").click()
# 登录错误时,无法跳转进入页面
try:
WebDriverWait(driver, 3, 1).until(lambda x: x.find_element_by_xpath("//*[@text='扫码换电']"))
scan = driver.find_elements_by_xpath("//*[@text='扫码换电']")
if scan:
print("登录成功")
return True
except:
print("登录失败")
return False
def run_two():
desired_caps_and5 = {
'platformName': 'Android', # 被测手机是安卓
'platformVersion': '5.1.1', # 手机安卓版本
'deviceName': 'HuaWei P30', # 设备名,安卓手机可以随意填写
'appPackage': 'com.chinatower.fghd.customer', # 启动APP Package名称
'appActivity': 'com.ckd.fgbattery.activity.User_Login_Activity', # 启动Activity名称
'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True
'resetKeyboard': True, # 执行完程序恢复原来输入法
'noReset': True, # 不要重置App
'newCommandTimeout': 5000,
'UDID':'127.0.0.1:62026',
'automationName': 'uiAutomator2'
# 'app': r'd:\apk\bili.apk',
}
driver2 = webdriver.Remote('http://localhost:4725/wd/hub', desired_caps_and5)
#login_pwd(2,2)
time.sleep(10)
print("第二个")
pwd = driver2.find_element_by_xpath('//*[@text = "密码登录"]')
print(pwd.get_attribute('text'))
TouchAction(driver2).tap(pwd).perform()
pwd.click()
driver2.find_element_by_id("user_phone_et").send_keys('1')
driver2.find_element_by_id("sms_code_et").send_keys('2')
driver2.find_element_by_id("btn_res").click()
# 登录错误时,无法跳转进入页面
try:
WebDriverWait(driver2, 3, 1).until(lambda x: x.find_element_by_xpath("//*[@text='扫码换电']"))
scan = driver2.find_elements_by_xpath("//*[@text='扫码换电']")
if scan:
print("登录成功")
return True
except:
print("登录失败")
return False
# @data(*data_sum)
# @unpack
# def login_pwd(sum_a,sum_b):
# pwd = driver.find_element_by_xpath('//*[@text = "密码登录"]')
# print(pwd.get_attribute('text'))
# TouchAction(self.driver).tap(pwd).perform()
# pwd.click()
# driver.find_element_by_id("user_phone_et").send_keys(sum_a)
# driver.find_element_by_id("sms_code_et").send_keys(sum_b)
# driver.find_element_by_id("btn_res").click()
# # 登录错误时,无法跳转进入页面
# try:
# WebDriverWait(driver, 3, 1).until(lambda x: x.find_element_by_xpath("//*[@text='扫码换电']"))
# scan = driver.find_elements_by_xpath("//*[@text='扫码换电']")
# if scan:
# print("登录成功")
# return True
# except:
# print("登录失败")
# return False
#
# # 连接Appium Server,初始化自动化环境
# driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# driver_p = webdriver.Remote('http://localhost:4725/wd/hub', desired_caps_and5)
#
# login_pwd(driver,1,1)
# login_pwd(driver_p,2,2)
if __name__ == '__main__':
t1 = threading.Thread(target=run_first,name = 'thread1')
t2 = threading.Thread(target=run_two,name='thread2')
start_time = time.time()
t1.start()
t1.join()
t2.start()
print("当前活跃的线程有:{}".format(threading.current_thread()))
print("正在运行的所有线程:{}".format(threading.enumerate()))
print("正在运行的线程数量:{}".format(threading.active_count()))
t2.join()
end_time = time.time()
print("程序运行耗时:{}".format(end_time - start_time))
房子要一层一层盖,知识要一点一点学。大家在学习过程中要好基础,多上手实操,话不多说,这里狠狠上一次干货!我熬夜整理好的各阶段(功能、接口、自动化、性能、测开)技能学习资料+实操讲解,非常适合私下里学习,比找资料自学高效多了,分享给你们。
领取关 w/x/g/z/h:软件测试小dao
敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。
边栏推荐
- 联想混合云Lenovo xCloud:4大产品线+IT服务门户
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- Routing information protocol rip
- How to understand distributed architecture and micro service architecture
- 数据分片介绍
- [Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
- Data type - floating point (C language)
- Tips for using jeditabletable
- ES6_ Arrow function
- Greenplum6.x常用语句
猜你喜欢
ncs成都新电面试经验
[Yugong series] February 2022 U3D full stack class 005 unity engine view
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
Greenplum6.x常用语句
Greenplum6.x监控软件搭建
let const
登山小分队(dfs)
Compilation and linking of programs
AVL平衡二叉搜索树
随机推荐
Golan idea IntelliJ cannot input Chinese characters
Teach you how to select PCB board by hand (II)
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
Gson转换实体类为json时报declares multiple JSON fields named
Several ways of lambda used in functions in kotlin (higher-order functions)
Data analysis methodology and previous experience summary 2 [notes dry goods]
数据库存储---表分区
Mock. JS usage details
关于基于kangle和EP面板使用CDN
How to understand distributed architecture and micro service architecture
Greenplum 6.x common statements
NCS Chengdu New Electric interview Experience
How to integrate app linking services in harmonyos applications
JEditableTable的使用技巧
[hard core science popularization] working principle of dynamic loop monitoring system
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
Implementation method of data platform landing
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
Uniapp wechat applet monitoring network
[step on the pit] Nacos registration has been connected to localhost:8848, no available server