Files
Main/Assets/Editor/DIY/QATool/AutoUITest.cs

405 lines
12 KiB
C#
Raw Permalink Normal View History

2025-01-25 04:38:09 +08:00
/*
Creat By LuoJiasi;
Plz Put in DIY File;
*/
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using NUnit.Framework;
using Thousandto.Core.Base;
using Thousandto.Code.Center;
using Thousandto.Code.Logic;
using Thousandto.Core.Asset;
using CoroutinePool = UnityEngine.Gonbest.MagicCube.CoroutinePool;
namespace Funcell.DIY.UniScene
{
public class AutoUITest : EditorWindow
{
[MenuItem("Ares/测试工具/自动化测试")]
static void Init()
{
var win = (AutoUITest)EditorWindow.GetWindow(typeof(AutoUITest));
win.Show();
win.titleContent = new GUIContent("自动化测试");
}
#region
protected string _now = DateTime.Now.ToString ("yyyy-MM-dd/") + DateTime.Now.ToString ("HH:mm:ss");
protected string _logplace;//Log存储位置
protected int flag = 0;//测试标记0初始1开始2结束
//测试结果记录到txt文件中文件地址为_logplace
protected void LogTest(string resultfile)
{
if (!File.Exists(_logplace))
{
File.WriteAllText(_logplace, "[Test Start]\r\n");
}
File.AppendAllText(_logplace, DateTime.Now.ToString ("yyyy-MM-dd/") + DateTime.Now.ToString ("HH:mm:ss") + "," + resultfile + "\r\n");
Debug.LogWarning(resultfile);
}
#endregion
private void OnGUI()
{
if(GUILayout.Button("自动化测试"))
{
_logplace = "D:/TestCase/AutoTestResult_" + DateTime.Now.ToString ("yyMMdd") + "-" + DateTime.Now.ToString ("HHmmss") + ".txt";
CoroutinePool.AddTask(AutoFunctionUI());
}
}
private IEnumerator AutoFunctionUI()
{
yield return new WaitForSeconds(0.1f);
String casepath = @"D:\\TestCase\\TestCaseList"; //用例列表的文件夹
var casefiles = Directory.GetFiles(casepath, "*.csv");
String tearup = @"D:\\TestCase\\SetUp.csv"; //用例初始化的文件
String teardown = @"D:\\TestCase\\TearDown.csv"; //用例初始化的文件
foreach (var casefile in casefiles)
{
LogTest("【测试用例】:" + casefile);
string nowID = DateTime.Now.ToString ("dd") + DateTime.Now.ToString ("HHmmss");
LogTest("测试账号:" + nowID);
ArrayList al = new ArrayList();
string strLine = "";
//TearUp 准备环境
FileStream tearupfs = new FileStream(tearup, FileMode.Open, FileAccess.Read);
StreamReader tearupsr = new StreamReader(tearupfs, System.Text.Encoding.GetEncoding(936));
strLine = tearupsr.ReadLine();
while(strLine!=null)
{
al.Add(strLine);
strLine = tearupsr.ReadLine();
}
//读取功能按钮的CSV文件
FileStream casefs = new FileStream(casefile, FileMode.Open, FileAccess.Read);
StreamReader casesr = new StreamReader(casefs, System.Text.Encoding.GetEncoding(936));
strLine = casesr.ReadLine();
while(strLine!=null)
{
al.Add(strLine);
strLine = casesr.ReadLine();
}
//TearDown 环境解除
FileStream teardownfs = new FileStream(teardown, FileMode.Open, FileAccess.Read);
StreamReader teardownsr = new StreamReader(teardownfs, System.Text.Encoding.GetEncoding(936));
strLine = teardownsr.ReadLine();
while(strLine!=null)
{
al.Add(strLine);
strLine = teardownsr.ReadLine();
}
casesr.Close();
tearupsr.Close();
teardownsr.Close();
//String casestrline = "";
//casestrline = casesr.ReadLine();
//while (casestrline != null)
//CoroutinePool.AddTask(StartStopAction(48));//每个用例前48秒会自动停止寻路,这是SetUp的时间
foreach (string testLine in al)
{
LogTest(testLine);
//StopAction();//每条用例执行前停止寻路
List<string> list = new List<string>(testLine.Split(','));
//点击按钮
if (list[0] == "ClickBtn")
{
if (GameObject.Find (list[1]) == null) {yield return new WaitForSeconds(3.0f);}
ClickBtn(list[1]);
yield return new WaitForSeconds(1.0f);
}
//点击有文本的按钮
if (list[0] == "ClickBtnByName")
{
if (GameObject.Find (list[1]) == null) {yield return new WaitForSeconds(3.0f);}
ClickBtnByName(list[1],list[2]);
yield return new WaitForSeconds(1.0f);
}
//按序号点击按钮
if (list[0] == "ClickBtnByIndex")
{
if (GameObject.Find (list[1]) == null) {yield return new WaitForSeconds(3.0f);}
ClickBtnByIndex(list[1],list[2]);
yield return new WaitForSeconds(1.0f);
}
//点击相同按钮多次
if (list[0] == "ClickBtnTimes")
{
for (int i = 1; i <= int.Parse(list[2]); i++)
{
ClickBtn(list[1]);
yield return new WaitForSeconds(1.0f);
}
}
//发送GM命令
if (list[0] == "SendGM")
{
SendGM(list[1]);
yield return new WaitForSeconds(0.1f);
}
//延时等待
if (list[0] == "Wait")
{
float waittime = float.Parse(list[1]);
yield return new WaitForSeconds(waittime);
}
//随机账号
if (list[0] == "InputRA")
{
Input(list[1], nowID);
LogTest("账号是:" + nowID);
}
//输入文本
if (list[0] == "Input")
{
Input(list[1],list[2].ToString());
LogTest("输入的:" + list[2]);
}
//打开功能菜单
if (list[0] == "OpenMenu") {OpenMenu();}
//关闭功能菜单
if (list[0] == "CloseMenu") {CloseMenu();}
//断言存在控件
if (list[0] == "Assert") {AssertGO(list[1]);}
//断言不存在控件
if (list[0] == "AssertNo") {AssertNoGO(list[1]);}
//断言文本
if (list[0] == "AssertText") {AssertText(list[1],list[2]);}
if (list[0] == "AssertActive") {AssertActiveGO(list[1]);}
//退出-切换账号
if (list[0] == "Quit")
{
GameCenter.GameSceneSystem.ReturnToLogin(true);
yield return new WaitForSeconds(10.0f);
}
//退出-切换角色
if (list[0] == "Exit")
{
GameCenter.GameSceneSystem.ReturnToLogin(false,true);
yield return new WaitForSeconds(10.0f);
}
//主角停止行动
if (list[0] == "StopAction"){ StopAction();}
//主角停止对应时间的行动
if (list[0] == "StopActionTime")
{
CoroutinePool.AddTask(StartStopAction(int.Parse(list[1])));
}
//casestrline = casesr.ReadLine();
}
}
LogTest("全部用例运行完毕");
}
//协程停止寻路stoptime秒
private IEnumerator StartStopAction(int stoptime)
{
yield return new WaitForSeconds(0.1f);
for (int i = 1; i <= stoptime*10; i++)
{
LocalPlayer lp = GameCenter.GameSceneSystem.GetLocalPlayer();
if (lp != null && lp.IsXState(EntityStateID.PathMove))
{
Debug.Log ("中止寻路");
GameCenter.MapLogicSwitch.DoPlayerExitPrepare();
}
yield return new WaitForSeconds(0.1f);
}
}
#region
//点击按钮
public void ClickBtn(string file)
{
GameObject go = GameObject.Find (file);
if (go == null) {
LogTest ("【Block】,找不到:" + file);
} else {
UICamera.Notify(go,"OnClick",null);
}
}
//点击包含Label名称为btnname的按钮用于多个按钮有同样的名称btnurl为按钮路径通常有多个同路径按钮btnname为子节点包含的label文本
public void ClickBtnByName(string btnurl, string btnname)
{
GameObject go = GameObject.Find(btnurl);
if (go == null) {
LogTest ("【Block】,找不到:" + btnurl);
return;
}
foreach (Transform child in go.transform.parent)
{
foreach (UILabel child_label in child.GetComponentsInChildren<UILabel>())
{
if (child_label.text.Contains(btnname))
{
UICamera.Notify(child.gameObject, "OnClick", null);
return;
}
}
}
}
//根据index点击btnurl的按钮
public void ClickBtnByIndex(string btnurl, string btnindex)
{
if (GameObject.Find(btnurl))
{
if(GameObject.Find(btnurl).transform.parent.GetChild(int.Parse(btnindex)).gameObject)
{
GameObject go = GameObject.Find(btnurl).transform.parent.GetChild(int.Parse(btnindex)).gameObject;
if (go == null)
{
LogTest ("【Block】,找不到:" + btnurl);
return;
}
UICamera.Notify(go, "OnClick", null);
}
}
}
//发送GM命令
public void SendGM(string GMString)
{
List<string> gmlist = new List<string>(GMString.Split(';'));
foreach (string gm in gmlist)
{
MSG_Chat.ChatReqCS req = new MSG_Chat.ChatReqCS();
req.chattype = 0;
req.recRoleId = 0;
req.condition = gm;
req.chatchannel = 0;
req.Send();
LogTest(gm);
}
}
//输入文本
public void Input(string inputUrl,string inputString)
{
GameObject go = GameObject.Find(inputUrl);
if (go == null) {
LogTest ("【Block】,找不到:" + inputUrl);
}
else {
UIInput name = go.GetComponent<UIInput>();
name.value = inputString;
}
}
//打开菜单
public void OpenMenu()
{
GameObject go = GameObject.Find("UIMainForm/RightBottom2/MenuPanel");
if (!go)
{
LogTest ("【Block】,找不到菜单按钮");
}
else if (!go.activeSelf)
{
ClickBtn("UIMainForm/RightBottom2/MenuBtn");
}
}
//关闭菜单
public void CloseMenu()
{
GameObject go = GameObject.Find("UIMainForm/RightBottom2/MenuPanel");
if (!go)
{
LogTest ("【Block】,找不到菜单按钮");
}
else if (go.activeSelf)
{
ClickBtn("UIMainForm/RightBottom2/MenuBtn");
}
}
//主角停止行动
public void StopAction()
{
LocalPlayer lp = GameCenter.GameSceneSystem.GetLocalPlayer();
if (lp != null)
{
if (lp.IsXState(EntityStateID.PathMove))
{
Debug.Log ("寻路中");
GameCenter.MapLogicSwitch.DoPlayerExitPrepare();//lp.Stop_Action();
}
}
}
#endregion
#region
//断言文本
private void AssertText(string s1, string s2)
{
if (GameObject.Find(s1) != null)
{
UILabel ui = GameObject.Find(s1).GetComponent<UILabel>();
if (ui != null)
{
if (ui.text == s2)
{
LogTest("[Pass],在:" + s1 + "的文本是:" + s2);
return;
}
else if (ui.text.Contains(s2))
{
LogTest("[Pass],在:" + s1 + "包含文本:" + s2);
return;
}
LogTest("【Fail】," + s1 + "不存在文本:" + s2);
}
else
{
foreach (Transform child in GameObject.Find(s1).transform.parent)
{
foreach (UILabel child_label in child.GetComponentsInChildren<UILabel>())
{
if (child_label.text.Contains(s2))
{
LogTest("[Pass],在:" + s1 + "的子路径包含文本:" + s2);
return;
}
}
}
LogTest("【Fail】," + s1 + "不存在文本:" + s2);
}
}
else
{
LogTest("【Block】,找不到: " + s1);
return;
}
}
//断言存在控件
private void AssertGO(string s1)
{
GameObject go = GameObject.Find (s1);
if (go == null) {
LogTest ("【Fail】,找不到:" + s1);
} else {
LogTest ("[Pass],找到:" + s1);
}
}
//断言不存在控件
private void AssertNoGO(string s1)
{
GameObject go = GameObject.Find (s1);
if (go == null) {
LogTest ("[Pass],不存在:" + s1);
} else {
LogTest ("【Fail】,存在不该存在的:" + s1);
}
}
private void AssertActiveGO(string s1)
{
GameObject go = GameObject.Find (s1);
if (go != null)
{
if (go.activeSelf) {LogTest ("[Pass],激活的:" + s1);}
else {LogTest ("【Fail】,未激活的:" + s1);}
}
else {LogTest ("【Fail】,不存在:" + s1);}
}
#endregion
}
}