//public static void HandleRunningInstance(Process instance) //{ // // 相同時透過ShowWindowAsync還原,以及SetForegroundWindow將程式至於前景 // ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); // tcp.F_SetForegroundWindow(instance.MainWindowHandle); //} long Form1_Load() { cThirdPartyToolControl tpc = new cThirdPartyToolControl(); string[] file_name = { "CoilStatus_250_Sync_20160331", "HoldingRegister_250_Sync_20160401", "InputRegister_250_Sync_20160331", "InputStatus_250_Sync_20160401" }; try { Process[] processes = Process.GetProcessesByName("ModSim32"); foreach (Process p in processes) { // 關閉目前程序前先等待 1000 毫秒 p.WaitForExit(1000); p.CloseMainWindow(); } EventLog.AddLog("Check existed ModSim and Close it"); System.Threading.Thread.Sleep(1000); string str = this.GetType().Assembly.Location; str = str.Substring(0, str.LastIndexOf(@"\")); string sourceDirName = @"\ModSim"; string destDirName = @"c:\ModSim"; bool copySubDirs = true; DirectoryCopy(str + sourceDirName, destDirName, copySubDirs); string sExePath = Directory.GetCurrentDirectory(); // 如果是用iATester執行初始exe 也是指到iATester執行檔位置 // 如果是用初始exe檔直接執行 則是指到初始exe檔執行位置 string sCurrentFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(this.GetType()).Location); //sCurrentFilePath無論用什麼執行 就是指到初始exe檔執行位置 for (int i = 0; i < file_name.Length; i++) { string sourceFile = sCurrentFilePath + "\\ModSim\\" + file_name[i]; string destFile = sExePath + "\\" + file_name[i]; EventLog.AddLog("copy " + sourceFile + " to " + destFile); System.IO.File.Copy(sourceFile, destFile, true); } } catch (Exception ex) { EventLog.AddLog(@"Error occurred Delete ModSim and Move it to C:\ : " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } try { Process ModSim = new Process(); // FileName 是要執行的檔案 ModSim.StartInfo.FileName = @"C:\ModSim\ModSim32.exe"; ModSim.Start(); EventLog.AddLog("Excute ModSim"); } catch (Exception ex) { EventLog.AddLog(@"Error occurred Excute ModSim: " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } try { System.Threading.Thread.Sleep(3000); //EventLog.AddLog("enter"); tpc.F_KeybdEvent(tpc.V_VK_ESCAPE, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); //EventLog.AddLog("wait 1 sec"); System.Threading.Thread.Sleep(1000); foreach (Process pTarget in Process.GetProcesses()) { if (pTarget.ProcessName == "ModSim32") // 取得處理序名稱並與指定程序名稱比較 { //HandleRunningInstance(pTarget); ShowWindowAsync(pTarget.MainWindowHandle, WS_SHOWNORMAL); tpc.F_SetForegroundWindow((int)pTarget.MainWindowHandle); } } EventLog.AddLog("Load script"); //string[] file_name = { "CoilStatus_250_Sync_20160331" }; for (int i = 0; i < file_name.Length; i++) { System.Threading.Thread.Sleep(2000); SendKeys.SendWait("^o"); System.Threading.Thread.Sleep(1000); int iRecipeList_Handle = tpc.F_FindWindow("#32770", "Open"); StringBuilder lpString = new StringBuilder(10); int bb = tpc.F_GetWindowText(iRecipeList_Handle, lpString, 100); int iEnterText2 = tpc.F_FindWindowEx(iRecipeList_Handle, 0, "Edit", ""); if (iEnterText2 > 0) { byte[] ch = (ASCIIEncoding.ASCII.GetBytes(file_name[i])); for (int j = 0; j < ch.Length; j++) { //SendMessage(PW, WM_CHAR, ch, 0); tpc.F_PostMessage(iEnterText2, tpc.V_WM_CHAR, ch[j], 0); //System.Threading.Thread.Sleep(100); } tpc.F_PostMessage(iEnterText2, tpc.V_WM_KEYDOWN, tpc.V_VK_RETURN, 0); //tpc.F_PostMessage(iEnterText2, tpc.V_WM_CHAR, 'a', 0); } } } catch (Exception ex) { EventLog.AddLog(@"Error occurred Load file: " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } try { EventLog.AddLog("connect to TCP"); System.Threading.Thread.Sleep(2000); SendKeys.SendWait("%c"); System.Threading.Thread.Sleep(1000); SendKeys.SendWait("%c"); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_RETURN, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_UP, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_RETURN, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); System.Threading.Thread.Sleep(2000); tpc.F_KeybdEvent(tpc.V_VK_RETURN, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); } catch (Exception ex) { EventLog.AddLog(@"Error occurred connect to TCP: " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } try { if (master != null) { master.Dispose(); } if (tcpClient != null) { tcpClient.Close(); } tcpClient = new TcpClient(); IAsyncResult asyncResult = tcpClient.BeginConnect(ipAddress, tcpPort, null, null); asyncResult.AsyncWaitHandle.WaitOne(3000, true); //wait for 3 sec if (!asyncResult.IsCompleted) { tcpClient.Close(); EventLog.AddLog("Cannot connect to ModSim."); return(-1); } //tcpClient = new TcpClient(ipAddress, tcpPort); // create Modbus TCP Master by the tcp client //document->Modbus.Device.Namespace->ModbusIpMaster Class->Create Method master = ModbusIpMaster.CreateIp(tcpClient); master.Transport.Retries = 0; //don't have to do retries master.Transport.ReadTimeout = 1500; //this.Text = "On line " + DateTime.Now.ToString(); EventLog.AddLog("Connect to ModSim."); //read DI(1xxxx), start address=0, points=4 byte slaveID = 1; bool[] status = master.ReadInputs(slaveID, 0, 1); if ((status[0] == true) || (status[0] == false)) { } else { EventLog.AddLog("read DI(10001) fail"); return(-1); } //read DO(00001), start address=0, points=1 bool[] coils = master.ReadCoils(slaveID, 0, 1); if ((coils[0] == true) || (coils[0] == false)) { } else { EventLog.AddLog("read DO(00001) fail"); return(-1); } //read AI(30001), start address=0, points=1 ushort[] register = master.ReadInputRegisters(1, 0, 1); if (register[0] > 0) { } else { EventLog.AddLog("read AI(30001) fail"); return(-1); } //read AO(40001), start address=0, points=1 ushort[] holding_register = master.ReadHoldingRegisters(1, 0, 1); if (holding_register[0] > 0) { } else { EventLog.AddLog("read AO(40001) fail"); return(-1); } } catch (Exception ex) { EventLog.AddLog(@"Error occurred verification: " + ex.ToString()); Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } #region Result judgement int iTotalSeleniumAction = dataGridView1.Rows.Count; for (int i = 0; i < iTotalSeleniumAction - 1; i++) { DataGridViewRow row = dataGridView1.Rows[i]; string sSeleniumResult = row.Cells[2].Value.ToString(); if (sSeleniumResult != "PASS") { bFinalResult = false; EventLog.AddLog("Test Fail !!"); EventLog.AddLog("Fail TestItem = " + row.Cells[0].Value.ToString()); EventLog.AddLog("BrowserAction = " + row.Cells[1].Value.ToString()); EventLog.AddLog("Result = " + row.Cells[2].Value.ToString()); EventLog.AddLog("ErrorCode = " + row.Cells[3].Value.ToString()); EventLog.AddLog("ExeTime(ms) = " + row.Cells[4].Value.ToString()); break; } } if (bFinalResult) { Result.Text = "PASS!!"; Result.ForeColor = Color.Green; EventLog.AddLog("Test Result: PASS!!"); return(0); } else { Result.Text = "FAIL!!"; Result.ForeColor = Color.Red; EventLog.AddLog("Test Result: FAIL!!"); return(-1); } #endregion }
private void StartViewRecipeData() { // Start view EventLog.AddLog("Start view"); api.SwitchToCurWindow(0); api.SwitchToFrame("rightFrame", 0); api.ByXpath("//tr[2]/td/a/font").Click(); PrintStep("Start View"); // Control browser EventLog.AddLog("Control browser"); int iIE_Handl, iIE_Handl_2, iIE_Handl_3, iIE_Handl_4, iIE_Handl_5, iIE_Handl_6, iIE_Handl_7, iWA_MainPage = 0; switch (slanguage) { case "ENG": iIE_Handl = tpc.F_FindWindow("IEFrame", "Node : TestSCADA - main:untitled"); iIE_Handl_2 = tpc.F_FindWindowEx(iIE_Handl, 0, "Frame Tab", ""); iIE_Handl_3 = tpc.F_FindWindowEx(iIE_Handl_2, 0, "TabWindowClass", "Node : TestSCADA - Internet Explorer"); iIE_Handl_4 = tpc.F_FindWindowEx(iIE_Handl_3, 0, "Shell DocObject View", ""); iIE_Handl_5 = tpc.F_FindWindowEx(iIE_Handl_4, 0, "Internet Explorer_Server", ""); iIE_Handl_6 = tpc.F_FindWindowEx(iIE_Handl_5, 0, "AfxOleControl42s", ""); iIE_Handl_7 = tpc.F_FindWindowEx(iIE_Handl_6, 0, "AfxWnd42s", ""); iWA_MainPage = tpc.F_FindWindowEx(iIE_Handl_7, 0, "ActXBroadWinBwviewWClass", "Advantech View 001 - main:untitled"); break; case "CHT": iIE_Handl = tpc.F_FindWindow("IEFrame", "節點 : TestSCADA - main:untitled"); iIE_Handl_2 = tpc.F_FindWindowEx(iIE_Handl, 0, "Frame Tab", ""); iIE_Handl_3 = tpc.F_FindWindowEx(iIE_Handl_2, 0, "TabWindowClass", "節點 : TestSCADA - Internet Explorer"); iIE_Handl_4 = tpc.F_FindWindowEx(iIE_Handl_3, 0, "Shell DocObject View", ""); iIE_Handl_5 = tpc.F_FindWindowEx(iIE_Handl_4, 0, "Internet Explorer_Server", ""); iIE_Handl_6 = tpc.F_FindWindowEx(iIE_Handl_5, 0, "AfxOleControl42s", ""); iIE_Handl_7 = tpc.F_FindWindowEx(iIE_Handl_6, 0, "AfxWnd42s", ""); iWA_MainPage = tpc.F_FindWindowEx(iIE_Handl_7, 0, "ActXBroadWinBwviewWClass", "Advantech View 001 - main:untitled"); break; case "CHS": iIE_Handl = tpc.F_FindWindow("IEFrame", "节点 : TestSCADA - main:untitled"); iIE_Handl_2 = tpc.F_FindWindowEx(iIE_Handl, 0, "Frame Tab", ""); iIE_Handl_3 = tpc.F_FindWindowEx(iIE_Handl_2, 0, "TabWindowClass", "节点 : TestSCADA - Internet Explorer"); iIE_Handl_4 = tpc.F_FindWindowEx(iIE_Handl_3, 0, "Shell DocObject View", ""); iIE_Handl_5 = tpc.F_FindWindowEx(iIE_Handl_4, 0, "Internet Explorer_Server", ""); iIE_Handl_6 = tpc.F_FindWindowEx(iIE_Handl_5, 0, "AfxOleControl42s", ""); iIE_Handl_7 = tpc.F_FindWindowEx(iIE_Handl_6, 0, "AfxWnd42s", ""); iWA_MainPage = tpc.F_FindWindowEx(iIE_Handl_7, 0, "ActXBroadWinBwviewWClass", "Advantech View 001 - main:untitled"); break; case "JPN": iIE_Handl = tpc.F_FindWindow("IEFrame", "ノード : TestSCADA - main:untitled"); iIE_Handl_2 = tpc.F_FindWindowEx(iIE_Handl, 0, "Frame Tab", ""); iIE_Handl_3 = tpc.F_FindWindowEx(iIE_Handl_2, 0, "TabWindowClass", "ノード : TestSCADA - Internet Explorer"); iIE_Handl_4 = tpc.F_FindWindowEx(iIE_Handl_3, 0, "Shell DocObject View", ""); iIE_Handl_5 = tpc.F_FindWindowEx(iIE_Handl_4, 0, "Internet Explorer_Server", ""); iIE_Handl_6 = tpc.F_FindWindowEx(iIE_Handl_5, 0, "AfxOleControl42s", ""); iIE_Handl_7 = tpc.F_FindWindowEx(iIE_Handl_6, 0, "AfxWnd42s", ""); iWA_MainPage = tpc.F_FindWindowEx(iIE_Handl_7, 0, "ActXBroadWinBwviewWClass", "Advantech View 001 - main:untitled"); break; case "KRN": iIE_Handl = tpc.F_FindWindow("IEFrame", "노드 : TestSCADA - main:untitled"); iIE_Handl_2 = tpc.F_FindWindowEx(iIE_Handl, 0, "Frame Tab", ""); iIE_Handl_3 = tpc.F_FindWindowEx(iIE_Handl_2, 0, "TabWindowClass", "노드 : TestSCADA - Internet Explorer"); iIE_Handl_4 = tpc.F_FindWindowEx(iIE_Handl_3, 0, "Shell DocObject View", ""); iIE_Handl_5 = tpc.F_FindWindowEx(iIE_Handl_4, 0, "Internet Explorer_Server", ""); iIE_Handl_6 = tpc.F_FindWindowEx(iIE_Handl_5, 0, "AfxOleControl42s", ""); iIE_Handl_7 = tpc.F_FindWindowEx(iIE_Handl_6, 0, "AfxWnd42s", ""); iWA_MainPage = tpc.F_FindWindowEx(iIE_Handl_7, 0, "ActXBroadWinBwviewWClass", "Advantech View 001 - main:untitled"); break; case "FRN": iIE_Handl = tpc.F_FindWindow("IEFrame", "Noeud : TestSCADA - main:untitled"); iIE_Handl_2 = tpc.F_FindWindowEx(iIE_Handl, 0, "Frame Tab", ""); iIE_Handl_3 = tpc.F_FindWindowEx(iIE_Handl_2, 0, "TabWindowClass", "Noeud : TestSCADA - Internet Explorer"); iIE_Handl_4 = tpc.F_FindWindowEx(iIE_Handl_3, 0, "Shell DocObject View", ""); iIE_Handl_5 = tpc.F_FindWindowEx(iIE_Handl_4, 0, "Internet Explorer_Server", ""); iIE_Handl_6 = tpc.F_FindWindowEx(iIE_Handl_5, 0, "AfxOleControl42s", ""); iIE_Handl_7 = tpc.F_FindWindowEx(iIE_Handl_6, 0, "AfxWnd42s", ""); iWA_MainPage = tpc.F_FindWindowEx(iIE_Handl_7, 0, "ActXBroadWinBwviewWClass", "Advantech View 001 - main:untitled"); break; default: iIE_Handl = tpc.F_FindWindow("IEFrame", "Node : TestSCADA - main:untitled"); iIE_Handl_2 = tpc.F_FindWindowEx(iIE_Handl, 0, "Frame Tab", ""); iIE_Handl_3 = tpc.F_FindWindowEx(iIE_Handl_2, 0, "TabWindowClass", "Node : TestSCADA - Internet Explorer"); iIE_Handl_4 = tpc.F_FindWindowEx(iIE_Handl_3, 0, "Shell DocObject View", ""); iIE_Handl_5 = tpc.F_FindWindowEx(iIE_Handl_4, 0, "Internet Explorer_Server", ""); iIE_Handl_6 = tpc.F_FindWindowEx(iIE_Handl_5, 0, "AfxOleControl42s", ""); iIE_Handl_7 = tpc.F_FindWindowEx(iIE_Handl_6, 0, "AfxWnd42s", ""); iWA_MainPage = tpc.F_FindWindowEx(iIE_Handl_7, 0, "ActXBroadWinBwviewWClass", "Advantech View 001 - main:untitled"); break; } /* * int iIE_Handl = tpc.F_FindWindow("IEFrame", "Node : TestSCADA - main:untitled"); * int iIE_Handl_2 = tpc.F_FindWindowEx(iIE_Handl, 0, "Frame Tab", ""); * int iIE_Handl_3 = tpc.F_FindWindowEx(iIE_Handl_2, 0, "TabWindowClass", "Node : TestSCADA - Internet Explorer"); * int iIE_Handl_4 = tpc.F_FindWindowEx(iIE_Handl_3, 0, "Shell DocObject View", ""); * int iIE_Handl_5 = tpc.F_FindWindowEx(iIE_Handl_4, 0, "Internet Explorer_Server", ""); * int iIE_Handl_6 = tpc.F_FindWindowEx(iIE_Handl_5, 0, "AfxOleControl42s", ""); * int iIE_Handl_7 = tpc.F_FindWindowEx(iIE_Handl_6, 0, "AfxWnd42s", ""); * int iWA_MainPage = tpc.F_FindWindowEx(iIE_Handl_7, 0, "ActXBroadWinBwviewWClass", "Advantech View 001 - main:untitled"); */ if (iWA_MainPage > 0) { tpc.F_PostMessage(iWA_MainPage, tpc.V_WM_KEYDOWN, tpc.V_VK_ESCAPE, 0); System.Threading.Thread.Sleep(1000); } else { EventLog.AddLog("Cannot get Start View WebAccess Main Page handle"); } // Login keyboard EventLog.AddLog("admin login"); int iLoginKeyboard_Handle; switch (slanguage) { case "ENG": iLoginKeyboard_Handle = tpc.F_FindWindow("#32770", "Login"); break; case "CHT": iLoginKeyboard_Handle = tpc.F_FindWindow("#32770", "登入"); break; case "CHS": iLoginKeyboard_Handle = tpc.F_FindWindow("#32770", "登录"); break; case "JPN": iLoginKeyboard_Handle = tpc.F_FindWindow("#32770", "ログイン"); break; case "KRN": iLoginKeyboard_Handle = tpc.F_FindWindow("#32770", "로그인"); break; case "FRN": iLoginKeyboard_Handle = tpc.F_FindWindow("#32770", "Connexion"); break; default: iLoginKeyboard_Handle = tpc.F_FindWindow("#32770", "Login"); break; } int iEnterText = tpc.F_FindWindowEx(iLoginKeyboard_Handle, 0, "Edit", ""); if (iEnterText > 0) { tpc.F_PostMessage(iEnterText, tpc.V_WM_CHAR, 'a', 0); System.Threading.Thread.Sleep(100); tpc.F_PostMessage(iEnterText, tpc.V_WM_CHAR, 'd', 0); //d System.Threading.Thread.Sleep(100); tpc.F_PostMessage(iEnterText, tpc.V_WM_CHAR, 'm', 0); //m System.Threading.Thread.Sleep(100); tpc.F_PostMessage(iEnterText, tpc.V_WM_CHAR, 'i', 0); //i System.Threading.Thread.Sleep(100); tpc.F_PostMessage(iEnterText, tpc.V_WM_CHAR, 'n', 0); //n System.Threading.Thread.Sleep(100); tpc.F_PostMessage(iEnterText, tpc.V_WM_KEYDOWN, tpc.V_VK_RETURN, 0); System.Threading.Thread.Sleep(500); tpc.F_PostMessage(iWA_MainPage, tpc.V_WM_KEYDOWN, tpc.V_VK_F8, 0); System.Threading.Thread.Sleep(1000); } else { EventLog.AddLog("Cannot get Login keyboard handle"); } int iRecipeList_Handle; switch (slanguage) { case "ENG": iRecipeList_Handle = tpc.F_FindWindow("#32770", "Recipe List"); break; case "CHT": iRecipeList_Handle = tpc.F_FindWindow("#32770", "配方列表"); break; case "CHS": iRecipeList_Handle = tpc.F_FindWindow("#32770", "配方列表"); break; case "JPN": iRecipeList_Handle = tpc.F_FindWindow("#32770", "レシピ一覧"); break; case "KRN": iRecipeList_Handle = tpc.F_FindWindow("#32770", "레시피 리스트"); break; case "FRN": iRecipeList_Handle = tpc.F_FindWindow("#32770", "Liste des recettes"); break; default: iRecipeList_Handle = tpc.F_FindWindow("#32770", "Recipe List"); break; } //int iRecipeList_Handle = tpc.F_FindWindow("#32770", "Recipe List"); int iEnterText2 = tpc.F_FindWindowEx(iRecipeList_Handle, 0, "Edit", ""); if (iEnterText2 > 0) { tpc.F_PostMessage(iEnterText2, tpc.V_WM_KEYDOWN, tpc.V_VK_RETURN, 0); } else { EventLog.AddLog("Cannot get Recipe List handle"); } if (iWA_MainPage > 0) { tpc.F_KeybdEvent(tpc.V_VK_SHIFT, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); tpc.F_PostMessage(iWA_MainPage, tpc.V_WM_KEYDOWN, tpc.V_VK_F1, 0); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_SHIFT, 0, tpc.V_KEYEVENTF_KEYUP, 0); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_DOWN, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); System.Threading.Thread.Sleep(1000); tpc.F_KeybdEvent(tpc.V_VK_RETURN, 0, tpc.V_KEYEVENTF_EXTENDEDKEY, 0); System.Threading.Thread.Sleep(1000); } else { EventLog.AddLog("Cannot get Start View WebAccess Main Page handle"); } }