# Time    : 2024/7/21 16:06
# Author  : wangchao
# File    : Test_VariableManagement.py
# Description : 变量管理测试类

import os
from time import sleep

import allure
import pytest
import re

from playwright.sync_api import expect

from BasePage.BasePage import BasePage
from BuildInLibrary.BuildInLibrary import BuildInLibrary
from Common.AllurePretty import PrettyAllure
from Pages.ProjectManagementPage.ProjectManagementPage import ProjectManagementPage
from Pages.VariableManagementPage.VariableManagementPage import VariableManagementPage
from Config import Config
from Utils.ReadYaml import ReadYaml
from Utils.VisualRegression import visual_regression


@allure.epic("CMS2.0UI自动化测试")
@allure.feature("变量管理")
class TestVariableManagement:

	@allure.story("全局导入变量")
	@PrettyAllure.PrettyAllureCaseWarpper
	@pytest.mark.run(order=1)
	@pytest.mark.parametrize("CaseData", ReadYaml(
		os.path.join(Config.test_datas_dir, "VariableManagementData/TestGlobalImportVariable.yaml")).read())
	def test_global_import_variable(self, page, CaseData: dict):
		bp = BasePage(page)
		vmp = VariableManagementPage(page)
		bp._goto_url_variable_management()
		vmp.import_global_variable_group(CaseData["参数化值-导入文件名"])
		expect(vmp.import_variable_success_toast_locator).to_have_text("导入成功") #断言导入成功

	@allure.story("全局导出变量")
	@pytest.mark.run(order=1)
	@PrettyAllure.PrettyAllureCaseWarpper
	@pytest.mark.parametrize("CaseData", ReadYaml(
		os.path.join(Config.test_datas_dir, "VariableManagementData/TestGlobalExportVariable.yaml")).read())
	def test_global_export_variable(self, page, CaseData: dict):
		bp = BasePage(page)
		vmp = VariableManagementPage(page)
		bp._goto_url_variable_management()
		vmp.export_global_variable_group()
		expect(vmp.export_variable_success_toast_locator).to_have_text("导出成功") #断言导出成功

	@allure.story("IO变量导入")
	@pytest.mark.run(order=1)
	@PrettyAllure.PrettyAllureCaseWarpper
	@pytest.mark.parametrize("CaseData", ReadYaml(
		os.path.join(Config.test_datas_dir, "VariableManagementData/TestIOVariableImport.yaml")).read())
	def test_io_variable_import(self, page, CaseData: dict):
		bp = BasePage(page)
		vmp = VariableManagementPage(page)
		bp._goto_url_variable_management()
		vmp.import_io_variable_group(CaseData["参数化值-导入文件名"])
		expect(vmp.import_variable_success_toast_locator).to_have_text("导入成功") #断言导入成功

	@allure.story("IO变量导出")
	@pytest.mark.run(order=1)
	@PrettyAllure.PrettyAllureCaseWarpper
	@pytest.mark.parametrize("CaseData", ReadYaml(
		os.path.join(Config.test_datas_dir, "VariableManagementData/TestIOVariableExport.yaml")).read())
	def test_io_variable_export(self, page, CaseData: dict):
		bp = BasePage(page)
		vmp = VariableManagementPage(page)
		bp._goto_url_variable_management()
		vmp.export_io_variable_group()
		expect(vmp.export_variable_success_toast_locator).to_have_text("导出成功") #断言导出成功

	@allure.story("内部变量导入")
	@pytest.mark.run(order=1)
	@PrettyAllure.PrettyAllureCaseWarpper
	@pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "VariableManagementData/TestInternalVariableImport.yaml")).read())
	def test_inner_variable_import(self, page, CaseData: dict):
		bp = BasePage(page)
		vmp = VariableManagementPage(page)
		bp._goto_url_variable_management()
		vmp.import_internal_variable_group(CaseData["参数化值-导入文件名"])
		expect(vmp.import_variable_success_toast_locator).to_have_text("导入成功")

	@allure.story("内部变量导出")
	@pytest.mark.run(order=1)
	@PrettyAllure.PrettyAllureCaseWarpper
	@pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "VariableManagementData/TestInternalVariableExport.yaml")).read())
	def test_inner_variable_export(self, page, CaseData: dict):
		bp = BasePage(page)
		vmp = VariableManagementPage(page)
		bp._goto_url_variable_management()
		vmp.export_internal_variable_group()
		expect(vmp.export_variable_success_toast_locator).to_have_text("导出成功")

	"""批量处理变量"""
	@allure.story("批量处理变量—修改变量")
	@pytest.mark.run(order=1)
	@PrettyAllure.PrettyAllureCaseWarpper
	@pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "VariableManagementData/TestBatchProcessVariable.yaml")).read())
	def test_batch_process_variable(self, page, CaseData: dict):
		bp = BasePage(page)
		vmp = VariableManagementPage(page)
		bp._goto_url_variable_management()
		#page.pause()
		vmp.batch_handle_variable()
		expect(vmp.batch_modification_variable_success_toast_locator).to_have_text("保存成功")

