windows之UIAutomation压测

来源:互联网 发布:群组推荐算法 编辑:程序博客网 时间:2024/06/14 00:05

1、了解需求,制定方案

这里写图片描述
这里写图片描述
这里写图片描述

2、UIAutomation写用例

[Flags]enum MouseEventFlag : uint{   Move = 0x0001,      //移动鼠标   LeftDown = 0x0002,  //模仿鼠标左键按下   LeftUp = 0x0004,    //模仿鼠标左键抬起   RightDown = 0x0008, //模仿鼠标右键按下   RightUp = 0x0010,   //模仿鼠标右键抬起     MiddleDown = 0x0020, // 模仿鼠标中键抬起   MiddleUp = 0x0040,   // 模仿鼠标中键抬起   XDown = 0x0080,   XUp = 0x0100,   Wheel = 0x0800,   VirtualDesk = 0x4000,   Absolute = 0x8000      //标示是否采取绝对坐标 }class TestCase{    [DllImport("user32.dll")]    extern static void mouse_event(MouseEventFlag mouseEventFlag, int incrementX, int incrementY, uint data, UIntPtr extraInfo);    // 笔迹书写    public static bool Handwriting(int n)    {       OperateObject.mainwin = GetObject.GetMainWindow();       if (OperateObject.mainwin == null)       {          return false;       }       AutomationElement penBtn = GetObject.GetPenBtn(OperateObject.mainwin);       if (penBtn == null)       {           return false;       }       //主界面笔迹按钮坐标位置       Rect rectPen = penBtn.Current.BoundingRectangle;       int penBtnFY = (int)(rectPen.Top);       //白板的坐标位置       Rect rectMainBoard = OperateObject.mainwin.Current.BoundingRectangle;       int mainWinBFX = (int)(rectMainBoard.Left);       int mainWinBFY = (int)(rectMainBoard.Top);       int mainWinBEX = (int)(rectMainBoard.Left + rectMainBoard.Width);       int mainWinBEY = (int)(rectMainBoard.Top + rectMainBoard.Height);       int[] x = new int[n + 1];       int[] y = new int[n + 1];       for (int i = 0; i < n + 1; i++)       {           x[i] = GetObject.GetRandomInt(mainWinBFX + 50, mainWinBEX - 20);           y[i] = GetObject.GetRandomInt(mainWinBFY + 50, penBtnFY - 20);       }        for (int i = 0; i < n; i++)        {            SimulateOpera.DragElementByPosition(x[i], y[i], x[i + 1], y[i + 1]);        }        return true;    }    // 关闭进程    static public void KillProcess(string processName)    {        Process[] myproc = Process.GetProcesses();        foreach (Process item in myproc)        {            if (item.ProcessName == processName)            {                item.Kill();                Thread.Sleep(2000);             }        }    }    // 判断某一个进程是否打开    static public bool IsProcessExisted(string processName)    {       Process[] temp = Process.GetProcessesByName(processName);       if (temp.Length > 0)return true;       else return false;    }    // 插入素材    static public bool InsertMaterial(string materialOpenPath)    {        OperateObject.mainwin = GetObject.GetMainWindow();        if (OperateObject.mainwin == null)        {            return false;        }        Thread.Sleep(200);        //点击菜单按钮        if (OperateObject.menuBtnClick() == false)        {            return false;        }        Thread.Sleep(100);        //点击导入按钮        if (OperateObject.importBtnClick() == false)        {            return false;        }        Thread.Sleep(100);        //点击文件按钮        if (OperateObject.importFileBtnClick() == false)        {           return false;        }        Thread.Sleep(200);        //输入保存文件的文件名和路径        if (OperateObject.SetOpenFileName(materialOpenPath) == false)         {            return false;         }         Thread.Sleep(200);         //点击弹窗打开窗口的打开按钮         if (OperateObject.PowOpenBtnClick() == false)         {             return false;         }         Thread.Sleep(200);         return true;     }     //重启软件     public static void RestartEasiNote(string ENPath)      {         String exeName = "Note";         bool ifENProcessExist = TestCase.IsProcessExisted(exeName);         if (ifENProcessExist == true)         {             TestCase.KillProcess(exeName);//强制关闭EasiNote程序         }         //打开EN程序,并判断是否存在异常关闭界面,若存在则清除之         Process.Start(ENPath);         Thread.Sleep(5000);         AutomationElement unNormalCloseBtn = GetObject.GetUnNormalCloseBtn();         if (unNormalCloseBtn != null)         {             bool uncb = unNormalCloseBtn.Current.IsOffscreen;             if (uncb == false)            {                //点击清除按钮                SimulateOpera.ClickImage(unNormalCloseBtn);                Thread.Sleep(2000);             }         }     }}

调用:

DirectoryInfo dir = new DirectoryInfo(materialOpenPath);var files = dir.GetFiles();var filenum = files.Length;int fileIndex = -1;TestCase.RestartEasiNote(ENPath);//重启软件bool bl = TestCase.IsProcessExisted(proNote);while (bl){    TestCase.Handwriting(1);//书写    //插入素材    string filenames = "";    for (int i = 0; i < materialnumber; i++)    {        fileIndex++;        if (fileIndex >= filenum)        {            fileIndex = 0;         }         filenames = filenames + "\"" + files[fileIndex].Name + "\" ";    }    TestCase.InsertMaterial(materialOpenPath + "\\" + filenames);    bl = TestCase.IsProcessExisted(proNote);}

执行效果:
这里写图片描述

这里写图片描述

原创粉丝点击