2014-11-14 90 views
2

我在應用程序中使用Selenium WebDriver,並且我有殺死webdrivers和瀏覽器實例的代碼。但是,我在想,如果用戶在運行應用程序之前打開了任何IE瀏覽器,則該代碼不僅會殺死我的應用程序產生的IE進程,還會殺死用戶在運行應用程序之前打開的IE實例。如何僅殺死由我的應用程序啓動的進程

有沒有一種方法可以跟蹤由我的應用程序啓動的進程,以便我可以過濾此方法以僅殺死由我的應用程序產生的IE進程,或者確定IE驅動程序和瀏覽器實例是由我的應用程序產生的,或者兩者兼而有之?

public void KillAllBrowsersAndWebDrivers() 
{ 

var webDrivers = Process.GetProcessesByName("IEDriverServer").Select(p => p.Id); 
var browsers = Process.GetProcessesByName("iexplore").Select(p => p.Id); 
var processIds = webDrivers.Concat(browsers); 
// do some stuff with PID, if you want to kill them, do the following 
foreach (var pid in processIds) 
{ 
    try 
    { 
     Process.GetProcessById(pid).Kill(); 
     Logger.Log(Loglevel.Debug, "Kill Process:{0}", pid); 
    } 
    catch (Exception) 
    { 
     Logger.Log(Loglevel.Error, "Error killing process: {0}", pid); 
    } 
} 

}

回答

2

所有你需要做的就是讓你所創建的所有進程的列表。 這是一個非常簡單的流程管理器。此代碼很容易出錯,而且沒有異常處理

private static List<Process> processes = new List<Process>(); 
static void Main(string[] args) 
{ 
    int PID = StoreProcess (yourProcess); 
    KillProcess(PID);  
} 

/// <summary> 
/// Stores the process in a list 
/// </summary> 
/// <returns>The PID</returns> 
/// <param name="prc">The process to be stored</param> 
public static int StoreProcess(Process prc) 
{ 
    int PID = prc.Id; // Get the process PID and store it in an int called PID 
    processes.Add (prc); // Add this to our list of processes to be kept track of 
    return PID; // Return the PID so that the process can be killed/changed at a later time 
} 

/// <summary> 
/// Kills a process 
/// </summary> 
/// <param name="PID">The PID of the process to be killed.</param> 
public static void KillProcess(int PID) 
{ 
    // Search through the countless processes we have and try and find our process 
    for (int i = 0; i <= processes.Count; i++) { 
     if (processes [i] == null) 
     { 
      continue; // This segment of code prevents NullPointerExceptions by checking if the process is null before doing anything with it 
     } 
     if (processes [i].Id == PID) { // Is this our process? 
      processes [i].Kill(); // It is! Lets kill it 
      while (!processes [i].HasExited) { } // Wait until the process exits 
      processes [i] = null; // Mark this process to be skipped the next time around 
      return; 
     } 
    } 
    // Couldn't find our process!!! 
    throw new Exception ("Process not found!"); 
} 

優勢

  • 您可以保留您所初始化的所有進程的跟蹤,並在任何終止逐一時間

缺點

  • 我不相信有任何
1

另一種可能的解決方案是讓運行前產卵任何新進程的進程的列表。然後殺死那些不在以前運行的進程列表中的那些。

public void KillOnlyProcessesSpawnedBySelenium() 
{ 
    // get a list of the internet explorer processes running before spawning new processes 
    var pidsBefore = Process.GetProcessesByName("iexplore").Select(p => p.Id).ToList(); 

    var driver = new Driver(Settings); 
    var driver1 = driver.InitiateDriver(); // this method creates new InternetExplorerDriver 
    var driver2 = driver.InitiateDriver(); 
    var driver3 = driver.InitiateDriver(); 
    driver1.Navigate().GoToUrl("http://google.com"); 
    driver2.Navigate().GoToUrl("http://yahoo.com"); 
    driver3.Navigate().GoToUrl("http://bing.com"); 

    var pidsAfter = Process.GetProcessesByName("iexplore").Select(p => p.Id); 

    var newInternetExplorerPids = pidsAfter.Except(pidsBefore); 

    // do some stuff with PID, if you want to kill them, do the following 
    foreach (var pid in newInternetExplorerPids) 
    { 
     Debug.WriteLine("Killing pid: {0}", pid); 
     Process.GetProcessById(pid).Kill(); 
    } 

    Assert.IsTrue(pidsBefore.Count > 0); 
    // determine if each process before the drivers spawned are running 
    foreach (var running in pidsBefore.Select(pid => Process.GetProcessById(pid).IsRunning())) 
    { 
     Assert.IsTrue(running); 
    } 
} 

這裏是用來確定是否進程仍在運行與否的擴展方法...

public static bool IsRunning(this Process process) 
{ 
    if (process == null) 
     throw new ArgumentNullException("process"); 

    try 
    { 
     Process.GetProcessById(process.Id); 
    } 
    catch (ArgumentException) 
    { 
     return false; 
    } 
    return true; 
} 
+2

「哦,不!我的SO頁面關閉掉了什麼!」 - 只是爲了記住這個不推薦,因爲它會殺死你的應用程序運行後開始的任何進程。這是好的,如果這是一臺專用機器。 – rcdmk 2014-12-11 18:04:32

相關問題