在软件开发过程中,自动化测试和开发工具是提高效率、降低成本的重要手段。本文将介绍一些流行的自动化工具,如Prism、IE Spy、Spy++等,它们主要基于屏幕抓取技术(Screen Scraping Technology)来实现自动化。在开始任何自动化项目之前,需要进行需求分析活动,这与传统的软件开发生命周期(SDLC)中的需求阶段有所不同。
需求分析的目的是计算完成整个过程所需的总时间,识别计算机化用户过程中的重复步骤,研究可以节省的鼠标点击次数,进行时间和动作研究,估算自动化的潜在节省,评估涉及过程的应用程序以及复制和粘贴数据的活动。
以一个典型的抵押贷款评估过程为例,假设有一个后台处理的呼叫中心代理,他收到了100个客户潜在名单的Excel表格,并在电话中向客户解释抵押贷款计算。自动化流程的顺序如下:
接下来,将通过一个案例研究来展示如何使用C++语言编写自动化脚本。
首先,需要启动抵押贷款潜在客户生成的Web应用程序和EMI计算器。以下是启动这些应用程序的代码示例:
private void LaunchApplications()
{
// 启动抵押贷款潜在客户生成Web应用程序
string URL = "http://yourInternalSite.com/lead-generation.aspx";
object o = null;
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();
app = (SHDocVw.WebBrowser)ie;
app.Visible = true;
app.Navigate(URL, ref o, ref o, ref o, ref o);
while (app.Busy) {
htmldoc = (HtmlDocument)app.Document;
}
System.Threading.Thread.Sleep(2000);
// 启动EMI计算器
System.Diagnostics.Process.Start(@"D:\Automation\EMI_Calculator.exe");
}
接下来,需要将抵押贷款潜在客户Excel列表导入自动化表单。以下是导入Excel文件的代码示例:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "浏览文本文件";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "xlsx";
openFileDialog1.Filter = "Excel文件 (*.xlsx)|*.xlsx|所有文件 (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
textBox1.Text = openFileDialog1.FileName;
}
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet DtSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\Dell Drivers\custList.xlsx';Extended Properties=Excel 12.0;");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
MyCommand.TableMappings.Add("Table", "Net-informations.com");
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
dataGridView1.DataSource = DtSet.Tables[0];
MyConnection.Close();
}
用户流程的下一步是点击选定的客户,以找出客户的EMI。以下是点击选定客户的代码示例:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) {
string selstrCustomerName = Convert.ToString(senderGrid[1, e.RowIndex].Value);
// 将名称粘贴到Lead Generator Web应用程序的搜索文本框中
pasteNameIntoLeadGenerator(selstrCustomerName);
}
}
在抵押贷款潜在客户Web应用程序中搜索选定的客户名称,并找出客户申请表单,获取贷款金额。以下是搜索客户并将名称粘贴到抵押贷款潜在客户应用程序的代码示例:
private void pasteNameIntoLeadGenerator(string selstrCustomerName)
{
var elems = wb.Document.GetElementByTagName("INPUT");
foreach (HtmlElement elem in elems) {
if (elem.GetAttribute("name") == "txtCustomerName" && elem.GetAttribute("type") == "text") {
elem.setAttribute("value", selstrCustomerName);
}
}
// 允许UI更新
System.Threading.Thread.Sleep(2000);
// 执行点击操作
foreach (HtmlElement elem in elems) {
if (elem.GetAttribute("type") == "button" && elem.GetAttribute("name") == "go") {
elem.InvokeMember("click");
}
}
// 再次允许网页刷新
System.Threading.Thread.Sleep(2000);
// 再次扫描HTML集合
foreach (HtmlElement elem in elems) {
if (elem.GetAttribute("name") == "txtLoanAmount" && elem.GetAttribute("type") == "text") {
string LoanAmount = elem.getAttribute("value");
}
}
}
为了粘贴值,首先需要识别EMI计算器屏幕,并使用Spy++获取控件ID。以下是识别窗口并发送消息到控件的代码示例:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", SetLastError = false)]
public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);
public const uint WM_SETTEXT = 0x000C;
private void InteropSetText(IntPtr iptrHWndDialog, int iControlID, string strTextToSet)
{
IntPtr iptrHWndControl = GetDlgItem(iptrHWndDialog, iControlID);
HandleRef hrefHWndTarget = new HandleRef(null, iptrHWndControl);
SendMessage(hrefHWndTarget, WM_SETTEXT, IntPtr.Zero, strTextToSet);
}
IntPtr handle = FindWindowByCaption(IntPtr.Zero, "EMI Calculator");
// 通过Spy++获取的控制ID传递值
int decValueofControl = int.Parse("0001042A", System.Globalization.NumberStyles.HexNumber);
InteropSetText(handle, decValueofControl, LoanAmount);
// 类似地,将贷款期限、利率值从自动化表单粘贴到EMI计算器