工具使用说明:
1.点击浏览,添加Case的路径。
2.点击测试用例按钮,加载测试case。
3.在自动化测试用例集中选中需要添加的case。
4.点击选择按钮,将选中的case添加到 listview中。
5.双击Loop次数下对应的case,修改测试case需要运行的次数。
6.点击运行按钮,运行测试用例,Log窗口打印控制台输出
// 设定打印输出内容
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.IO;
public struct CItems
{
public string name;
public string filepath;
public int count;
public int loop;
}
namespace AutoTestApp
{
class ExecutedCmd : FormAutoTest
{
List<CItems> lists = new List<CItems>();
//将各项参数存数在结构体容器中
public List<CItems> ChooseItems(FormAutoTest AutoTestForm)
{
for (int i=0; i< AutoTestForm.listView1.Items.Count;i++)
{
CItems items = new CItems();
items.name = AutoTestForm.listView1.Items[i].SubItems[0].Text;
// items.count = AutoTestForm.textBox3.Text;
// items.loop = AutoTestForm.listView1.Items[i].SubItems[1].Text;
items.count = int.Parse(AutoTestForm.textBox3.Text.ToString());
items.loop = int.Parse(AutoTestForm.listView1.Items[i].SubItems[1].Text.ToString());
foreach (string fPath in AutoTestForm.file_Path)
{
if (fPath.Contains(items.name))
{
items.filepath = fPath;
}
}
lists.Add(items);
}
return lists;
}
public void RunScriptscommand( List<CItems> litems, Process cmd)
{
foreach (CItems items in litems)
{
for (int i = 1; i <= items.count; i++ )
{
for (int j = 1; j <=items.loop; j++ )
{
cmd.StandardInput.WriteLine("monkeyrunner {0} {1} {2}",items.filepath, i, j);
}
}
}
} }
}
//界面和按钮事件触发
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace AutoTestApp
{
public partial class FormAutoTest : Form
{
private ListViewItem li;
private int X = 0;
private int Y = 0;
private string subItemText;
private int subItemSelected = 0;
private System.Windows.Forms.TextBox editBox = new System.Windows.Forms.TextBox();
List<CItems> Autolist;
public List<string> file_Path = new List<string>();
public FormAutoTest()
{
InitializeComponent();
editBox.Size = new System.Drawing.Size(0, 0);
editBox.Location = new System.Drawing.Point(0, 0);
listView1.Controls.AddRange(new System.Windows.Forms.Control[] { this.editBox });
editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);
editBox.LostFocus += new System.EventHandler(this.FocusOver);
editBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
editBox.BackColor = Color.LightYellow;
editBox.BorderStyle = BorderStyle.Fixed3D;
editBox.Hide();
editBox.Text = "";
//注册事件
// this.OutputLineGet += new OnOutputLineGetDelegate(MainForm_OutputLineGet);
// this.OutputDataReceived += new DataReceivedEventHandler(cmdOutputDataReceived);
}
private void FormAutoTest_Load(object sender, EventArgs e)
{
// this.reportViewer1.RefreshReport();
}
//将treeview1中选中的脚本在listview1中显示
private void buttonChoose_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
// foreach ( TreeNode nodes in treeView1.Nodes)
// {
// for (int i = 0; i < nodes.Nodes.Count; i++)
// {
// if (nodes.Nodes[i].Checked)
// {
// listView1.Items.Add(nodes.Nodes[i].Text);
// }
//
// }
// }
foreach (TreeNode nodes in treeView1.Nodes)
{
TreeNodeCollection tnodes = nodes.Nodes;
getCheckedNode(tnodes);
}
//将对应的listview1中列填好数据
foreach (ListViewItem litem in listView1.Items)
{
litem.SubItems.Add("1");
litem.SubItems.Add("Monkeyrunner");
}
}
public void getCheckedNode(TreeNodeCollection tnodes)
{
foreach( TreeNode nodes in tnodes)
{
//扫描到最后一集的文件
if(nodes.Checked &&(nodes.Nodes.Count == 0))
{
listView1.Items.Add(nodes.Text);
}
if (nodes.Nodes.Count > 0)
{
getCheckedNode(nodes.Nodes);
}
}
}
// 调用CMD控制台,并把输出在texBox中打印
private void buttonRun_Click(object sender, EventArgs e)
{
ExecutedCmd eCmd = new ExecutedCmd();
Autolist = eCmd.ChooseItems(this);
textBox1.Clear();
//运行打印CMD控制台log
ExeCmd();
}
// 添加目录地址并在texBox中显示
private void buttonfindDir_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
textBox2.Text = folderBrowserDialog1.SelectedPath.ToString().Trim() + "\\";
textBox3.Text = "1";
}
//将测试用例在树形控件中显示
private void buttonTestcase_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
treeView1.BeginUpdate();
if (treeView1.Nodes.Count == 0)
{
TreeNode TNode = new TreeNode("自动化测试测试用例集");
treeView1.Nodes.Add(TNode);
}
foreach (TreeNode tnnode in treeView1.Nodes)
{
if (textBox2.Text == "")
{
;
}
else
{
DirectoryInfo dInfo = new DirectoryInfo(textBox2.Text);
if (Directory.Exists(textBox2.Text))
{
FileSystemInfo[] fsInfos = dInfo.GetFileSystemInfos();
foreach (FileSystemInfo item in fsInfos)
{
if (item is Directory)
{
DirectoryInfo dirInfo = new DirectoryInfo(item.FullName);
TreeNode treeNode = new TreeNode(dirInfo.Name);
tnnode.Nodes.Add(treeNode);
}
else
{
FileInfo fInfo = new FileInfo(item.FullName);
TreeNode treeNode = new TreeNode(fInfo.Name);
tnnode.Nodes.Add(treeNode);
}
}
foreach (TreeNode node in tnnode.Nodes)
{
createdDirectoryTree(node);
}
}
}
}
treeView1.EndUpdate();
}
//递归添加子目录
public void createdDirectoryTree(TreeNode tNode)
{
TreeNode tempNode = tNode;
string filePath ="";
while(tNode.Parent.Parent != null)
{
filePath = tNode.Parent.Text + "\\" + filePath;
tNode = tNode.Parent;
}
tNode = tempNode;
filePath = textBox2.Text + filePath + tNode.Text;
filePath = filePath.ToString().Trim() + "\\";
if (filePath.Contains("py"))
{
file_Path.Add(filePath);
}
try
{
tNode.Nodes.Clear();
DirectoryInfo subDirInfo = new DirectoryInfo(filePath);
foreach (DirectoryInfo dir in subDirInfo.GetDirectories())
{
if (dir.Name != "images")
{
tNode.Nodes.Add(dir.Name);
}
}
foreach (FileInfo file in subDirInfo.GetFiles())
{
// if (file.Directory.Name == "scripts")
// {
tNode.Nodes.Add(file.Name);
// }
//
}
foreach (TreeNode node in tNode.Nodes)
{
createdDirectoryTree(node);
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
//树形控件显示指定目录下目录结构
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode tNode = treeView1.SelectedNode;
/* string filePath ="";
while(tNode.Parent != null)
{
filePath = tNode.Parent.Text + "\\" + filePath;
tNode = tNode.Parent;
}
tNode = treeView1.SelectedNode;
filePath = textBox2.Text + filePath + tNode.Text;
DirectoryInfo subDirInfo = new DirectoryInfo(filePath);
if (Directory.Exists(filePath))
{
FileSystemInfo[] fsInfos = subDirInfo.GetFileSystemInfos();
if (fsInfos.Length > 0)
{
foreach (FileSystemInfo item in fsInfos)
{
if (item is Directory)
{
DirectoryInfo dirInfo = new DirectoryInfo(item.FullName);
TreeNode treeNode = new TreeNode(dirInfo.Name);
tNode.Nodes.Add(treeNode);
}
else
{
FileInfo fInfo = new FileInfo(item.FullName);
TreeNode treeNode = new TreeNode(fInfo.Name);
tNode.Nodes.Add(treeNode);
}
}
}
}*/
listView1.Items.Clear();
listView1.Items.Add(tNode.Text);
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
// treeView1.CheckBoxes
// listView1.Items.Clear();
if (e.Action == TreeViewAction.ByMouse)
{
if (e.Node.Checked)
{
setChildNodeCheckedState(e.Node, true);
}
else
{
setChildNodeCheckedState(e.Node, false);
if (e.Node.Parent != null)
{
setParentNodeCheckedState(e.Node, false);
}
}
}
}
//取消节点选择状态之后,取消所有父节点的选择状态
public void setParentNodeCheckedState(TreeNode currNode, bool state)
{
TreeNode parentNode = currNode.Parent;
parentNode.Checked = state;
if (currNode.Parent.Parent!=null)
{
setParentNodeCheckedState(currNode.Parent, state);
}
}
//选中节点之后,选中所有子节点
public void setChildNodeCheckedState(TreeNode currNode, bool state)
{
TreeNodeCollection nodes = currNode.Nodes;
if (nodes.Count > 0)
{
foreach (TreeNode tn in nodes)
{
tn.Checked = state;
setChildNodeCheckedState(tn, state);
}
}
}
//调用CMD打印函数
//调用函数,
public void ExeCmd()
{
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
// cmd.StartInfo.Arguments = args;
//将cmd的标准输入和输出全部重定向到.NET的程序里
cmd.StartInfo.RedirectStandardInput = true;//标准输入
cmd.StartInfo.RedirectStandardOutput = true;//标准输出
cmd.StartInfo.UseShellExecute = false;//此处必须为false否则引发异常
//
cmd.StartInfo.CreateNoWindow = true;//不显示命令行窗口
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//
ThreadPool.QueueUserWorkItem(new WaitCallback(ExeCmdThread), cmd);
}
public void ExeCmdThread(object obj)
{
Process cmd = obj as Process;
cmd.Start();
//在控制台执行Monkeyrunner命令
ExecutedCmd etCmd = new ExecutedCmd();
etCmd.RunScriptscommand(Autolist, cmd);
cmd.OutputDataReceived += new DataReceivedEventHandler(cmdOutputDataReceived);
cmd.BeginOutputReadLine();
//
Application.DoEvents();
cmd.WaitForExit();
try
{
if ( cmd.Start())
{
while (!cmd.HasExited)
{
string output = cmd.StandardOutput.ReadLine();
if (output != null)
{
ShowMessage(output);
}
}
}
}
catch (System.Exception ex)
{
ShowMessage(ex.ToString());
}
cmd.Close();
}
void cmdOutputDataReceived(object sender, DataReceivedEventArgs e)
{
ShowMessage(e.Data);
}
public delegate void ShowMessageHandler(string msg);
public void ShowMessage(string msg)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowMessageHandler(ShowMessage), new object[] { msg });
}
else
{
if (msg != null)
{
textBox1.AppendText(msg);
textBox1.AppendText("\r\n");
}
}
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
int nStart = X;
int spos = 0;
int epos = listView1.Columns[0].Width;
for (int i = 0; i < listView1.Columns.Count; i++)
{
if (nStart > spos && nStart < epos)
{
subItemSelected = i;
break;
}
spos = epos;
epos += listView1.Columns[i].Width;
}
Console.WriteLine("SUB ITEM SELECTED = " + li.SubItems[subItemSelected].Text);
subItemText = li.SubItems[subItemSelected].Text;
string colName = listView1.Columns[subItemSelected].Text;
if (colName == "Loop次数")
{
Rectangle r = new Rectangle(spos , li.Bounds.Y , epos , li.Bounds.Bottom);
editBox.Size = new System.Drawing.Size(epos - spos , li.Bounds.Bottom-li.Bounds.Top);
editBox.Location = new System.Drawing.Point(spos , li.Bounds.Y);
editBox.Text = subItemText;
editBox.Show() ;
editBox.SelectAll() ;
editBox.Focus();
}
this.Refresh();
}
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
li = this.listView1.GetItemAt(e.X, e.Y);
X = e.X;
Y = e.Y;
}
private void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
li.SubItems[subItemSelected].Text = editBox.Text;
editBox.Hide();
}
if (e.KeyChar == 27)
editBox.Hide();
}
private void FocusOver(object sender, System.EventArgs e)
{
li.SubItems[subItemSelected].Text = editBox.Text;
editBox.Hide();
}
}
}