基本信息
源码名称:Automation(黑盒测试案例)
源码大小:1.06M
文件格式:.rar
开发语言:C#
更新时间:2019-03-20
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
黑盒测试案例
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("目前剪贴板中数据不可转换为文本", "错误");
}
}
}
}
}
}