说明:程序中有些自定义的控件类型在TestStack.White框架中没有涉及,需要引入自定义的DLL,通过鼠标点击事件处理

使用:将自定义的ClassLibrary2.dll拷贝到项目/bin/debug目录下,项目添加引用选择dll。

--该dll定义了Control Patterns为空的点击事件类BaseClass;

--该dll定义了查找控件属性类PropertyConditions;

一、 C#生成DLL文件

1.  在VS2015,文件->新建项目->项目类型visual C#->类库(注意必须是类库)

--即新建一个由纯.cs 类库文件组成的程序集。

2.  编写代码  详细代码见附录:

-文件名:Class1.cs

-namespace:BaseTest

-类名:BaseClass

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

这样你的\Projects\ClassLibrary2\ClassLibrary2\bin\Debug文件夹或者\Projects\ClassLibrary2\ClassLibrary2\obj\Debug文件夹里便会自动生成 dll文件,该文件名称与项目名称一致,即为ClassLibrary2.dll。

二、引用DLL文件

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

b. 然后在解决方案中添加引用:右键鼠标-->添加引用-->浏览-->选择dll放置路径后点击“确定”。

c. 在应用文件头处使用using BaseTest;

调用 dll中方法时使用BaseClass.方法名

三、附录:自定义类库代码

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 / );
int IncrementY = (int)(rect.Top + rect.Height / );
//Make the cursor position to the element.
SetCursorPos(IncrementX, IncrementY);
//Make the left mouse down and up.
mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, , UIntPtr.Zero);
mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, , 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 / );
int IncrementY = (int)(rect.Top + rect.Height / );
//Make the cursor position to the element.
SetCursorPos(IncrementX, IncrementY);
//Make the left mouse down and up.
mouse_event(MOUSEEVENTF_LEFTDOWN, IncrementX, IncrementY, , UIntPtr.Zero);
mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, , 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 = ;
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 > )
{
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
}
}

最新文章

  1. Webdriver设置Chrome属性
  2. 读javascript高级程序设计13-JSON
  3. CSS 定义上划线、下划线、删除线代码
  4. HDU 5996:dingyeye loves stone(阶梯博弈)
  5. linux设置系统日期时间
  6. 蟠桃记 AC 杭电
  7. Node.js学习 - Modules
  8. linux logrotate配置
  9. 手动搭建apache james邮件服务器,实现邮件功能
  10. 【开发技术】Eclipse插件Call Hierarchy简介及设置
  11. echarts 自定义配置带单位的 tooltip 提示框方法 和 圆环数据 tooltip 过长超出屏幕
  12. [Bash]LeetCode194. 转置文件 | Transpose File
  13. 猜测的rpc负载均衡原理,基于dubbo的架构
  14. nlp知识
  15. springMVC 返回时间格式转换
  16. FTP 150 Opening BINARY mode data connection for MLSD 读取目录列表失败
  17. java Comparable 和 Comparator接口区别
  18. 封装Ajax框架!(代码篇)
  19. git工具的使用总结
  20. asp启用父路径

热门文章

  1. 基于Netty重构RPC框架
  2. [暑假集训Day1T1]黑暗城堡
  3. weBDrriver API接口方法小记
  4. Swift编程语言学习1.6——可选值
  5. vue脚手架安装
  6. XMPP即时通讯协议使用(三)——订阅发布、断开重连与Ping
  7. postman 上一个接口的返回值作为下一个接口的入参
  8. KVC、KVO 理解
  9. Sass-@each
  10. ansible笔记(一)--架构图以及工作原理