2014-11-14 82 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