# Time    : 2024/3/20 21:49
# Author  : wangchao
# File    : VisualRegression.py
# Description : 引入视觉对比方式，通过图片对比来检测页面是否有变化

from visual_regression_tracker import Config, IgnoreArea
from visual_regression_tracker.playwright import PlaywrightVisualRegressionTracker, PageTrackOptions, \
	PageScreenshotOptions, Agent, ElementHandleScreenshotOptions, ElementHandleTrackOptions


def visual_regression(page_obj, browser_type, page_name, element_name, element_locator):

	config = Config(

		# apiUrl - URL where backend is running(图像对比服务部署在单独的服务器)
		apiUrl='http://127.0.0.1:4200',

		# project - Project name or ID
		project='e34e6e08-86d3-425f-b939-c19894894144',

		# apiKey - User apiKey
		apiKey='DEFAULTUSERAPIKEYTOBECHANGED',

		# ciBuildId - Current git commit SHA
		ciBuildId='commit_sha',

		# branch - Current git branch
		branchName='master',

		# enableSoftAssert - Log errors instead of exceptions
		enableSoftAssert=True,
	)

	vrt = PlaywrightVisualRegressionTracker(browser_type, config=config)

	with vrt:
		vrt.trackPage(page_obj, page_name, PageTrackOptions(
			diffTollerancePercent=1.34,
			ignoreAreas=[
				IgnoreArea(
					x=100,
					y=200,
					width=300,
					height=400,
				)
			],
			screenshotOptions=PageScreenshotOptions(
				full_page=True,
				omit_background=True,
			),
			agent=Agent(
				os='win10 IOT LTSC',
				device='装备大屏',
			)
		))

		vrt.trackElementHandle(element_locator, element_name, ElementHandleTrackOptions(
			diffTollerancePercent=1.34,
			ignoreAreas=[
				IgnoreArea(
					x=1,
					y=2,
					width=30,
					height=10,
				)
			],
			screenshotOptions=ElementHandleScreenshotOptions(
				omit_background=True,
			),
			agent=Agent(
				os='win10 IOT LTSC',
				device='装备大屏',
				viewport='1200x12'
			)
		))
