Though it is possible in ruby to kill a process by its ID in ruby, it isn’t possible to kill processes by name, I guess because it would work differently across operating systems. This is a pain when using watir/firewatir, because the @browser.close method isn’t particularly reliable; making test runs flakey unless this problem is dealt with.
Process killing needs to be implemented for each platform; you can find out how to kill firefox using ruby on the mac here. The windows version of that is what this post is about.
`taskkill /im Firefox.exe /f /t >nul 2>&1`
Explanation: The above is the line of code you need to kill firefox. Taskkill is an exe that comes as standard on windows which is a little-known way of killing processes (back when I spent most of my time doing UI test automation in java (using RFT), I wrapped PSKill.exe to do the same thing). The /im option is the ‘image’ name (ie: the process name); the /f option tells taskkill to force quite the process, and the /t option performs a ‘tree kill’, ie: kill all child processes as well as the process specified. The whole taskkill command is piped to >nul 2>&1 which means that any output (standard or error) will disappear into a void – doing this means that your test output won’t be filled with useless info. Finally, the whole command is wrapped in ‘backticks’ (`, not single quotes!) which ruby interprets as a command to be executed by the OS, passing any results back to ruby.
Thank you for posting this. It’s funny how when you search the internet for something as simple as “killing win firefox proc” it churns up a response related to your initial environment issue (Watir) Ha!. Great post. Exactly what I needed. I also run multi platform, so your secondary link over to the Mac way to kill a proc was a time saver. For the win.
Excellent! Kind of sucks they haven’t built in a method to grab process by name. The taskkill tip was very helpful though, thanks!