执行测试用例时,自动触发屏幕录制,单用例单录制
录制方式采用多线程,主线程开始,另开一个线程,跑录制脚本
web端
录制检测的是电脑页面,录制电脑页面展示的数据。
from datetime import datetime
from PIL import ImageGrab
from cv2.cv2 import *
import numpy as np
# from cv2.cv2 import VideoWriter_fourcc
from pynput import keyboard
import threading
from commons.common import base
import os
from logs.user_log import UserLog
get_user_log = UserLog()
logger = get_user_log.get_log()
Basepath = base()
file_name = os.path.join(Basepath, 'video')
# flag = False
def video_record(file):
global name
name = datetime.now().strftime(f"{
file}%Y-%m-%d %H-%M-%S") # 当前时间(文件命名)
name = file_name + '\\' + name
screen = ImageGrab.grab() # 获取当前屏幕
width, high = screen.size # 获取当前屏幕的大小
fourcc = VideoWriter_fourcc('X', 'V', 'I', 'D') # MPEG-4编码,文件后缀可为.avi .asf .mov等
video = VideoWriter('%s.avi' % name, fourcc, 16, (width, high)) # (文件名,编码器,帧率,视频宽高)
logger.info("record start !!!")
while True:
if flag:
logger.info("record end !!!")
video.release() # 释放
break
img = ImageGrab.grab() # 图片为RGB模式
# print('====================')
imm = cvtColor(np.array(img), COLOR_RGB2BGR) # 转为opencv的BGR模式
video.write(imm)
# def video_info():
# 视频信息
# video = VideoCapture('%s.avi' % name)
video = VideoCapture('%s.avi' % name)
fps = video.get(CAP_PROP_FPS)
frames = video.get(CAP_PROP_FRAME_COUNT)
logger.info('帧率=%.1f' % (fps))
logger.info('帧数=%.1f' % (frames))
logger.info('分辨率=(%d,%d)' % (int(video.get(CAP_PROP_FRAME_WIDTH)), int(video.get(CAP_PROP_FRAME_HEIGHT))))
logger.info('时间=%.2f秒' % (int(frames) / fps))
return
def end_recording(key): # 监听按键
global flag
# if key == keyboard.Key.home:
# if key == keyboard.Key.enter:
if key:
flag = True
# return False # 返回False,键盘监听结束
def start_recording(filename):
# run(flag)
global flag
flag = False
th = threading.Thread(target=video_record, args=(filename,))
# th.setDaemon(True)
th.start()
# 开始录制
start_recording(filename)
sleep(10)
# 结束录制
end_recording()
拼接git
from selenium import webdriver
import threading
import os
import time
from PIL import Image
def clear_dir(path):
"""创建或清空目录"""
if not os.path.isdir(path):
os.mkdir(path) # 创建目录
else: # 清空目录
[os.remove(os.path.join(path, file_name)) for file_name in os.listdir(path)]
def shot(dr, img_dir):
"""循环截图函数"""
i = 0
clear_dir(img_dir) # 清空目录
while True:
img_file = os.path.join(img_dir, f'{
i}.png')
try:
dr.save_screenshot(img_file)
except:
return
i += 1
# Selenium操作
img_dir = 'img' # 临时图片目录
dr = webdriver.Chrome()
t = threading.Thread(target=shot, args=(dr, img_dir)) # 新建线程
t.start() # 启动截图线程
dr.get('https://www.baidu.com')
dr.find_element('id', 'kw').send_keys('简书 韩志超')
dr.find_element('id', 'su').click()
time.sleep(1)
dr.get('https://www.qq.com')
dr.back()
time.sleep(2)
dr.quit()
# 图片拼接成gif
img_list = os.listdir(img_dir) # 列出目录所有图片
img_list.sort(key=lambda x: int(x[:-4])) # 排序
first_img = Image.open(os.path.join(img_dir, img_list[0])) # 第一张图片对象
else_imgs = [Image.open(os.path.join(img_dir, img)) for img in img_list[1:]] # 剩余图片对象
first_img.save("record.gif", append_images=else_imgs,
duration=300, # 每张图片的过过渡时间
save_all=True) # 拼接保存,如果想要循环播放可以加上loop=0
代码原文链接:https://www.cnblogs.com/superhin/p/13857521.html
Android端
Android端面向真机与模拟器,一下代码还在测试当中。
def settingtime():
while True:
starttime = time.strftime("%H:%M:%S");
recordcmd=“adb shell screenrecord /sdcard/demo.mp4”
pullfile=“adb pull /sdcard/demo.mp4 /Users/shixinfa/Desktop/0402”
AVATAR_STRANGER_PATH = “/Users/shixinfa/Desktop/0402”
os.system(recordcmd)
os.system(pullfile)
image = os.path.join(AVATAR_STRANGER_PATH, “demo.mp4”)
# 分离文件名和目录
dirname, filename = os.path.split(image)
# 改名
new_file = os.path.join(dirname, starttime+".mov")
print(“image=” + image, “dirname=” + dirname, “filename=” + filename,“new_file=”+new_file)
os.rename(image, new_file)
settingtime()
文章评论