Monday 9 May 2011

Microsoft CodedUI Tip - Kill Unwanted Processes During MyTestInitialize

Here's a quick way to kill any unwanted processes that may be running before your test starts, in my case I'm targeting any notepad applications that may be open.

MyTestInitialize allows you to run some code before the test starts, from MyTestInitialize i call my process kill.

     public void MyTestInitialize()  
     {  
       // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.  
       // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463  
       TerminateProcess.YerOuttaHere();  
     }  

The kill process uses System.Diagnostics and kills any notepad process.


 using System;  
 using System.Threading;  
 using System.Diagnostics;  
 public class TerminateProcess  
 {  
   public static void YerOuttaHere()  
   {  
     Process[] processes = Process.GetProcessesByName("notepad");  
     foreach (Process process in processes)  
     {  
       process.Kill();  
       process.WaitForExit();  
     }  
   }  
 }  

No comments:

Post a Comment