Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill IEDriverServer.exe console window after running an InternetExplorerDriver Selenium test

I'm running Selenium Webdriver through a Visual Studio Unit Test and using the InternetExplorerDriver. This fires up IEDriverServer.exe in a console window.

This works great and the test executes. Once test execution finishes i'd like that cmd.exe window killed so that it's not hanging around for me to have to do manual cleanup. I have thousands of tests so you can imagine the headache of managing this.

Is there an elegant way to handle this without having to do post test execution and killing cmd.exe processes with kill.exe, etc? I've tried InternetExplorerDriverService.HideCommandPromptWindow = true, but that just runs cmd.exe in hidden mode and leaves the process running until it's manually killed. I've also tried InternetExplorerDriverService.SuppressInitialDiagnosticInformation and all that does is suppress some of the information written to the cmd.exe window.

like image 239
Captain Kirk Avatar asked Dec 01 '22 16:12

Captain Kirk


1 Answers

To complement the awser, some informations about the subject. There are 3 main methods that that you can use as you need:

.Close(): - This method is used to close the current open window. It closes the current open window on which driver has focus on.

.Quit(): - This method is used to destroy the instance of WebDriver. It closes all Browser Windows associated with that driver and safely ends the session. Quit() calls Dispose method to achieve this.

.Dispose(): - This method closes all Browser windows and safely ends the session emphasized text

In your case, as you need to kill the process, the best choice is use .Quit().

Sometimes you're unable to end the process when your selenium instance broke up, and the current and all next tests starts to fail, or even when you cancel a current debugging test suit. In this cases, you really need to kill the process.

As @Prateek saw, one of they is using Runtime.getRuntime(). In C#, the better way to do it is:

foreach (var proc in Process.GetProcessesByName("IEDriverServer"))
{
    proc.Kill();
}

You can use it in the constructor where you start your WebDriver (but before to start it!) or, if you're using some lib as NUnit, you can create a global OneTimeSetUp. A kind of method that runs only one time before all test.

Also, if you don't like to put code or create new methods to do this job, you can configure in yout main .csproj the pre-build and post-build to execute your cmd command. Whenever you will run your tests or build your project, it will kill the opened webdriver instance.

To configure it you will need:

  1. Right button in your .csproj and click in "Properties";

  2. Navigate to "Build Events";

  3. Paste the command taskkill /f /fi "pid gt 0" /im IEDriverServer.exe in pre-build textarea, post-build or in both.

like image 182
Striter Alfa Avatar answered Dec 04 '22 07:12

Striter Alfa