項目搭建(三):自定義DLL

說明:程序中有些自定義的控件類型在TestStack.White框架中沒有涉及,須要引入自定義的DLL,經過鼠標點擊事件處理框架

使用:將自定義的ClassLibrary2.dll拷貝到項目/bin/debug目錄下,項目添加引用選擇dll。ui

  --該dll定義了Control Patterns爲空的點擊事件類BaseClass;spa

  --該dll定義了查找控件屬性類PropertyConditions;debug

 

1、 C#生成DLL文件code

1.  在VS2015,文件->新建項目->項目類型visual C#->類庫(注意必須是類庫)orm

   --即新建一個由純.cs 類庫文件組成的程序集。blog

2.  編寫代碼  詳細代碼見附錄:事件

  -文件名:Class1.csci

  -namespace:BaseTestelement

  -類名:BaseClass

3. 生成->生成[解決方案名]

  這樣你的\Projects\ClassLibrary2\ClassLibrary2\bin\Debug文件夾或者\Projects\ClassLibrary2\ClassLibrary2\obj\Debug文件夾裏便會自動生成 dll文件,該文件名稱與項目名稱一致,即爲ClassLibrary2.dll。

 

2、引用DLL文件

 a. 首先把dll文件放到應用程序...\bin\Debug\下;

 b. 而後在解決方案中添加引用:右鍵鼠標-->添加引用-->瀏覽-->選擇dll放置路徑後點擊「肯定」。

 c. 在應用文件頭處使用using BaseTest;

   調用 dll中方法時使用BaseClass.方法名

 

3、附錄:自定義類庫代碼

using System;
using System.Text;
using System.Windows;
using System.Windows.Automation;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace BaseTest
{
    public class BaseClass
    {
        #region ClickMouse
        #region Import DLL

        /// <summary>
        /// Add mouse move event
        /// </summary>
        /// <param name="x">Move to specify x coordinate</param>
        /// <param name="y">Move to specify y coordinate</param>
        /// <returns></returns>

        [DllImport("user32.dll")]

        extern static bool SetCursorPos(int x, int y);

        /// <summary>
        /// Mouse click event
        /// </summary>
        /// <param name="mouseEventFlag">MouseEventFlag </param>
        /// <param name="incrementX">X coordinate</param>
        /// <param name="incrementY">Y coordinate</param>
        /// <param name="data"></param>
        /// <param name="extraInfo"></param>

        [DllImport("user32.dll")]

        extern static void mouse_event(int mouseEventFlag, int incrementX, int incrementY, uint data, UIntPtr extraInfo);
        const int MOUSEEVENTF_MOVE = 0x0001;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;

        #endregion

        public static void ClickLeftMouse(int processId, string automationId)
        {
            AutomationElement element = FindElementById(processId, automationId);
            if (element == null)
            {
                throw new NullReferenceException(string.Format("Element with AutomationId '{0}' and Name '{1}' can not be find.",
                    element.Current.AutomationId, element.Current.Name));
            }
            Rect rect = element.Current.BoundingRectangle;
            int IncrementX = (int)(rect.Left + rect.Width / 2);
            int IncrementY = (int)(rect.Top + rect.Height / 2);
            //Make the cursor position to the element.
            SetCursorPos(IncrementX, IncrementY);
            //Make the left mouse down and up.
            mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero);
            mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero);
        }
        #endregion

        public static void ClickLeftMouseByName(int processId, string name)
        {
            AutomationElement element = FindElementByName(processId, name);
            if (element == null)
            {
                throw new NullReferenceException(string.Format("Element with Name '{0}' and Name '{1}' can not be find.",
                    element.Current.Name, element.Current.Name));
            }
            Rect rect = element.Current.BoundingRectangle;
            int IncrementX = (int)(rect.Left + rect.Width / 2);
            int IncrementY = (int)(rect.Top + rect.Height / 2);
            //Make the cursor position to the element.
            SetCursorPos(IncrementX, IncrementY);
            //Make the left mouse down and up.
            mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, 0, UIntPtr.Zero);
            mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, UIntPtr.Zero);
        }


        /// <summary>
        /// Get the automation elemention of current form.
        /// </summary>
        /// <param name="processId">Process Id</param>
        /// <returns>Target element</returns>
        public static AutomationElement FindWindowByProcessId(int processId)
        {
            AutomationElement targetWindow = null;
            int count = 0;
            try
            {
                Process p = Process.GetProcessById(processId);
                targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
                return targetWindow;
            }
            catch (Exception ex)
            {
                count++;
                StringBuilder sb = new StringBuilder();
                string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
                if (count > 5)
                {
                    throw new InvalidProgramException(message, ex);
                }
                else
                {
                    return FindWindowByProcessId(processId);
                }
            }
        }

        // Get the automation element by automation Id.
        public static AutomationElement FindElementById(int processId, string automationId)

        {
            AutomationElement aeForm = FindWindowByProcessId(processId);
            AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
            new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
            return tarFindElement;
        }

        // Get the automation element by Name.
        public static AutomationElement FindElementByName(int processId, string name)

        {
            AutomationElement aeForm = FindWindowByProcessId(processId);
            AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
            new PropertyCondition(AutomationElement.NameProperty, name));
            return tarFindElement;
        }
        
        // get the button element and click
         public static void ClickButtonById(int processId,string buttonId)
        {
            AutomationElement element = FindElementById(processId,buttonId);
            if (element == null)
            {
                throw new NullReferenceException(string.Format("Element with AutomationId '{0}' can not be find.", element.Current.Name));
            }
            InvokePattern currentPattern = GetInvokePattern(element);
            currentPattern.Invoke();
        }
        
         #region InvokePattern helper
        /// <summary>
        /// Get InvokePattern
        /// </summary>
        /// <param name="element">AutomationElement instance</param>
        /// <returns>InvokePattern instance</returns>
        public static InvokePattern GetInvokePattern(AutomationElement element)
        {
            object currentPattern;
            if (!element.TryGetCurrentPattern(InvokePattern.Pattern, out currentPattern))
            {
                throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the InvokePattern.",
                    element.Current.AutomationId, element.Current.Name));
            }
            return currentPattern as InvokePattern;
        }

        #endregion
    }
}
相關文章
相關標籤/搜索