List<string> fileName = new List<string>();
List<string> fileNameNoPath = new List<string>();
List<string> ReportName = new List<string>();
foreach (string fname in System.IO.Directory.GetFileSystemEntries(Folder, "*.json", SearchOption.AllDirectories))
{
fileName.Add(fname);
}
for (int i = 0; i < fileName.Count; i++)
{
fileNameNoPath.Add(fileName[i].Replace(Folder, "").Replace(".json", ""));
ReportName.Add(fileNameNoPath[i].Substring(fileNameNoPath[i].LastIndexOf("\\") + 1));
Console.WriteLine(fileName[i]);
}
Command cmd = new Command();
cmd.RunCommand(fileName, BaseUrl, Global, Environmental, ReportName);
Console.ReadKey();
public void RunCommand(List<string> ScriptFile, String BaseUrl, String Global, String Environment, List<string> ScriptReporeName)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系統shell啓動
p.StartInfo.RedirectStandardInput = true;//接受來自調用程序的輸入信息
p.StartInfo.RedirectStandardOutput = true;//由調用程序獲取輸出信息
p.StartInfo.RedirectStandardError = true;//重定向標準錯誤輸出
p.StartInfo.CreateNoWindow = true;//不顯示程序窗口
p.StartInfo.StandardOutputEncoding = Encoding.UTF8; //避免中文亂碼
p.Start();//啓動程序
//向cmd窗口發送輸入信息
for (int i = 0; i < ScriptFile.Count; i++)
{
p.StandardInput.WriteLine("newman run " + ScriptFile[i] + " --environment " + Environment + " --globals " + Global + " --global-var baseUrl=" + BaseUrl + " -r htmlextra --reporter-htmlextra-export ApiTestReport\\" + ScriptReporeName[i]+".html");
}
p.StandardInput.WriteLine("exit");
p.WaitForExit();//等待程序執行完退出進程
p.Close();
}
foreach 那邊只是找出某資料夾底下所有的.json檔 (Postman Collections 匯出的腳本 要用Newman來跑 )
Command裡面的for是去看說檔案有幾個就用command執行newman 幾次
之後會產出.html的測試報告
我有15個腳本但為什麼他會跑10個檔案後就停掉了(腳本沒有問題)
找不到問題... 所以上來請教一下
我改成這樣你試試看,一個ScriptFile跑一個newman Process,而不是一個cmd Process跑所有newman
public void RunCommand(List<string> ScriptFile, String BaseUrl, String Global, String Environment, List<string> ScriptReporeName)
{
for(int i = 0 ;i< ScriptFile.Length;i++)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"C:\newman\newman.exe"; // 我猜你的資料夾放這裡
p.StartInfo.CreateNoWindow = true;//不顯示程序窗口
p.StartInfo.StandardOutputEncoding = Encoding.UTF8; //避免中文亂碼
p.StartInfo.Arguments = "run " + ScriptFile[i] + " --environment " + Environment + " --globals " + Global + " --global-var baseUrl=" + BaseUrl + " -r htmlextra --reporter-htmlextra-export ApiTestReport\\" + ScriptReporeName[i]+".html"
p.Start();//啓動程序
p.WaitForExit();//等待程序執行完退出進程
p.Close();
p.Dispose();
p = null;
}
}