实例介绍
【实例简介】
黑盒测试案例
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Automation; using System.Diagnostics; using System.Threading; using System.IO; using System.Windows; using System.Runtime.InteropServices; namespace Automation { public partial class Form1 : Form { 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; public Form1() { InitializeComponent(); } ///<summary> ///根据传入的路径启动相应的可执行程序,并返回进程ID ///</summary> public static Int32 StartExe(string strExePath) { if (null == strExePath) { return 0; } Process ps = Process.Start(strExePath); Thread.Sleep(3000); return ps.Id; } ///<summary> ///根据进程ID,查找相应窗体,并返回窗体句柄 ///</summary> public static AutomationElement GetWindowHandle(Int32 pid) { int error = 0; AutomationElement targetWindow = null; //int iWaitTime = 0; try { Process ps = Process.GetProcessById(pid); targetWindow = AutomationElement.FromHandle(ps.MainWindowHandle); while (null == targetWindow) { //if (iWaitTime > iWaitSecond) //{ // break; //} Thread.Sleep(500); targetWindow = AutomationElement.FromHandle(ps.MainWindowHandle); } return targetWindow; } catch (System.Exception ex) { Thread.Sleep(3000); error ; if (error < 5) { return GetWindowHandle(pid); } string msg = "没有找到指定的窗口,请确认窗口已经启动!"; throw new InvalidProgramException(msg, ex); } } ///<summary> ///根据窗口句柄以及Element的AutomationID,返回Element句柄 ///</summary> public static AutomationElement GetElementHandle(AutomationElement parentWindowHandle, string sAutomationID, ControlType type) { PropertyCondition condition = null; PropertyCondition codEdit = null; AndCondition andConditon = null; AutomationElement targetHandle = null; try { if (null == parentWindowHandle) { return null; } condition = new PropertyCondition(AutomationElement.AutomationIdProperty, sAutomationID); codEdit = new PropertyCondition(AutomationElement.ControlTypeProperty, type); andConditon = new AndCondition(condition, codEdit); targetHandle = parentWindowHandle.FindFirst(TreeScope.Children, andConditon); return targetHandle; } catch (System.Exception ex) { string msg = "没有找到指定的控件,请确认控件的AutomationID是否正确!"; throw new InvalidProgramException(msg, ex); } } ///<summary> ///根据窗口句柄以及对话框的name,返回对话框句柄 ///</summary> public static AutomationElement GetWindowByName(AutomationElement parent, string name, int iWaitSecond) { AutomationElement targetWindow = null; int iWaitTime = 0; if (parent == null) { throw new Exception("Parent element is null!"); } try { while (null == targetWindow) { if (iWaitTime > iWaitSecond) { break; } iWaitTime ; Thread.Sleep(500); PropertyCondition nameProperty = new PropertyCondition(AutomationElement.NameProperty, name); PropertyCondition typeProperty = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window); PropertyCondition controlTypeProperty = new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "对话"); AndCondition andCondition = new AndCondition(nameProperty, typeProperty, controlTypeProperty); targetWindow = parent.FindFirst(TreeScope.Descendants, andCondition); } return targetWindow; } catch //(System.Exception ex) { return null; //string msg = "没有找到指定的窗口,请确认窗口已经启动!"; //throw new InvalidProgramException(msg, ex); } } public static AutomationElementCollection GetElementsHandle(AutomationElement parentWindowHandle, string sAutomationID, ControlType type) { PropertyCondition condition = null; PropertyCondition codEdit = null; AndCondition andConditon = null; AutomationElementCollection targetHandle = null; try { if (null == parentWindowHandle) { return null; } condition = new PropertyCondition(AutomationElement.AutomationIdProperty, sAutomationID); codEdit = new PropertyCondition(AutomationElement.ControlTypeProperty, type); andConditon = new AndCondition(condition, codEdit); targetHandle = parentWindowHandle.FindAll(TreeScope.Children, andConditon); return targetHandle; } catch (System.Exception ex) { string msg = "没有找到指定的控件,请确认控件的AutomationID是否正确!"; throw new InvalidProgramException(msg, ex); } } ///<summart> ///根据Button按钮句柄,进行鼠标左键单击 ///</summary> public static bool ButtonLeftClick(AutomationElement ButtonHandle) { object objButton = null; InvokePattern ivkpButton = null; try { if (null == ButtonHandle) { return false; } if (!ButtonHandle.TryGetCurrentPattern(InvokePattern.Pattern, out objButton)) { return false; } ivkpButton = (InvokePattern)objButton; ivkpButton.Invoke(); return true; } catch (System.Exception ex) { string msg = "鼠标左键单击失败!"; throw new InvalidProgramException(msg, ex); } } ///<summary> ///根据TextEdit句柄,在TextEdit内填写数据 ///只能设置单行输入的TextEdit ///</summary> public static bool SetTextEditData(AutomationElement TextEditHandle, string strData) { ValuePattern vpTextEdit = null; if (!TextEditHandle.Current.IsEnabled) { throw new InvalidOperationException("The control is not enabled.\n\n"); } if (!TextEditHandle.Current.IsKeyboardFocusable) { throw new InvalidOperationException("The control is not focusable.\n\n"); } vpTextEdit = TextEditHandle.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; if (null == vpTextEdit) { return false; } if (vpTextEdit.Current.IsReadOnly) { throw new InvalidOperationException("The control is read-only."); } vpTextEdit.SetValue(strData); return true; } [DllImport("user32.dll")] extern static bool SetCursorPos(int x, int y); [DllImport("user32.dll")] extern static void mouse_event(int mouseEventFlag, int incrementX, int incrementY, uint data, UIntPtr extraInfo); public void ClickLeftMouse(AutomationElement element) { //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, new UIntPtr(0)); mouse_event(MOUSEEVENTF_LEFTUP, IncrementX, IncrementY, 0, new UIntPtr(0)); } private void button1_Click(object sender, EventArgs e) { string path = "D:\\ABBYY FineReader 14\\FineReaderOCR.exe"; Int32 ProcessId = StartExe(path); AutomationElement mwh = GetWindowHandle(ProcessId); AutomationElement d = GetWindowByName(mwh, "ABBYY FineReader 14 OCR 编辑器", 5); //2657 if (d != null) { SendKeys sendkeys1 = new SendKeys(); sendkeys1.Sendkeys(d, "%N"); AutomationElement btnYes = GetElementHandle(d, "7", ControlType.Button); ButtonLeftClick(btnYes); } AutomationElement pwh = GetElementHandle(mwh, "40766", ControlType.Pane); AutomationElement twh = GetElementHandle(pwh, "40215", ControlType.ToolBar); AutomationElement bth = GetElementHandle(twh, "Item 40215", ControlType.Button); ButtonLeftClick(bth); AutomationElement ow = GetWindowByName(mwh, "打开图像", 3000); AutomationElement cbFile = GetElementHandle(ow, "1148", ControlType.ComboBox); AutomationElement eFile = GetElementHandle(cbFile, "1148", ControlType.Edit); SetTextEditData(eFile, @"G:\C#\ElectronicFillingSystem\ElectronicFillingSystem\bin\Debug\1-007-27.jpg"); AutomationElement btnOpen = GetElementHandle(ow, "1", ControlType.Button); ButtonLeftClick(btnOpen); AutomationElement oa = GetWindowByName(mwh, "添加图像至 OCR 项目", 2); //2657 while (oa != null) { oa = GetWindowByName(mwh, "添加图像至 OCR 项目", 2); AutomationElement btn = GetElementHandle(oa, "2657", ControlType.Button); //2657 if(btn == null) { break; } if (btn != null && btn.Current.Name == "关闭(C)") { ButtonLeftClick(btn); break; } } AutomationElementCollection ps = GetElementsHandle(mwh, "", ControlType.Pane); foreach(AutomationElement p in ps) { AutomationElement pane = GetElementHandle(p, "40761", ControlType.Pane); if(pane != null) { AutomationElement pane1 = GetElementHandle(pane, "", ControlType.Pane); AutomationElement pane2 = GetElementHandle(pane1, "1", ControlType.Pane); ClickLeftMouse(pane2); SendKeys sendkeys = new SendKeys(); sendkeys.Sendkeys(pane2, "^A"); sendkeys.Sendkeys(pane2, "^C"); IDataObject iData = Clipboard.GetDataObject(); if (iData.GetDataPresent(DataFormats.Text)) { //MessageBox.Show((string)iData.GetData(DataFormats.Text)); writerControlExt1.RTFText = (string)iData.GetData(DataFormats.Rtf); Process ps1 = Process.GetProcessById(ProcessId); ps1.Kill(); MessageBox.Show("剪贴板已粘贴", "提示"); } else { MessageBox.Show("目前剪贴板中数据不可转换为文本", "错误"); } } } } } }
标签: 黑盒
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论