# -*- coding:utf-8 -*- 
"""
describe：allure常用方法抽离，截图方法实现
Author：wangchao
Time: 2023/12/2
"""
import uuid
import allure
import functools
import os.path
import pytest
import time

from Config import Config


class PrettyAllure(object):

	@classmethod
	def PrettyAllureCase(cls, page, CaseData):
		allure.dynamic.feature(CaseData.get("模块"))
		allure.dynamic.story(CaseData.get("功能"))
		allure.dynamic.severity(CaseData.get("优先级"))
		allure.dynamic.title(f'{CaseData.get("用例编号")}_{CaseData.get("用例标题")}')
		if CaseData.get("是否执行") != "Y":
			allure.dynamic.description("用例指定跳过")
			pytest.skip("用例指定跳过")

	@classmethod
	def PrettyAllureScreenShot(cls, pw_locator):
		picture_name = f"元素操作截图{str(uuid.uuid4())}.png"
		filename = os.path.join(Config.test_screenshot_dir, picture_name)
		pw_locator.screenshot(path=filename)
		allure.attach.file(source=filename, name=picture_name, attachment_type=allure.attachment_type.PNG)

	@classmethod
	def PrettyAllureVideo(cls, page, CaseData):
		allure.dynamic.story(CaseData.get("功能"))
		# 录制视频
		video = page.video
		video_name = f"用例操作录屏{str(uuid.uuid4())}.webm"
		video_path = os.path.join(Config.test_video_dir, video_name)
		video.save_as(video_path)
		# 添加视频到Allure报告
		with open(video_path, "rb") as video_file:
			allure.attach(body=video_file.read(),
						  attachment_type=allure.attachment_type.MP4,
						  attachmentsubtype=None,
						  name=allure.dynamic.story(CaseData.get("功能")))

	@classmethod
	def PrettyAllureCaseWarpper(cls, func):
		"""装饰器函数"""

		@functools.wraps(func)
		def inner(*args, **kwargs):
			# 添加用例信息
			cls.PrettyAllureCase(page=kwargs.get("page"), CaseData=kwargs.get("CaseData"))
			# 运行用例
			r = func(*args, **kwargs)
			return r

		return inner

	@classmethod
	def PrettyAllureScreenShotWarpper(cls, func):
		"""装饰器函数"""

		@functools.wraps(func)
		def inner(*args, **kwargs):
			# 运行用例
			r = func(*args, **kwargs)
			# 添加截图
			cls.PrettyAllureScreenShot(pw_locator=kwargs.get("pw_locator"))
			return r

		return inner

	@classmethod
	def PrettyAllureVideoWarpper(cls, func):
		"""装饰器函数"""

		@functools.wraps(func)
		def inner(*args, **kwargs):
			# 运行用例
			r = func(*args, **kwargs)
			# 添加视频
			cls.PrettyAllureVideo(page=kwargs.get("page"), CaseData=kwargs.get("CaseData"))
			return r

		return inner


if __name__ == '__main__':
	pass
