I have found that you can actually find the webdriven browser's PID from deep inside the @browser
object (reading all protected and private components), and then renice
it with a negative number to increase priority, which might require sudo
to be allowed by a non-root user.
I've explored exporting this object to an ASCII form for storage, which actually works, though importing it back was the subject of another question. Try this (I do it just for fun every time my code fires up a new Watir::Browser
):
require "yaml"
File.open("browserObj.yaml", 'w').write YAML::dump($browser)
Then when you peek inside this file browserObj.yaml, it gives you all sorts of interesting info, like:
server_url: !ruby/object:URI::HTTP
fragment:
host: 127.0.0.1
opaque:
parser:
password:
path: /hub/
port: 7055
query:
registry:
scheme: http
user:
timeout:
launcher: !ruby/object:Selenium::WebDriver::Firefox::Launcher
binary: !ruby/object:Selenium::WebDriver::Firefox::Binary
process: !ruby/object:ChildProcess::Unix::ForkExecProcess
args:
- ./firefox.sh
- -no-remote
- -foreground
detach: false
duplex: false
environment: {}
exit_code:
io:
pid: 6114
started: true
Notice the PID in the 2nd last line, which your code can easily detect and do whatever with at this point.
That is even safer than simply parsing the hierarchical process tree with eg. pstree -panu $PPID
to find child browser processes.
In my own stuff I actually don't bother (eg. when I need to kill the proper Firefox process and not others) because I go by DISPLAY. All my desktop/interactive user stuff happens on DISPLAY :0, while my Watir Webdriver stuff happens on DISPLAY :99 hosted by Xvfb or Xephyr, which I can more selectively kill
/xkill
with the help of tools like xprop
and xwininfo
.
EDIT
For completeness, here's the Unix/Cygwin command I use to send a kill
command to the watir-webdriver browser's pid if I need to:
awk '/pid:/ {print $2;}' browserObj.yaml |xargs -rt kill