Can a watir browser object be re-used in a later Ruby process?

后端 未结 5 995
一向
一向 2021-01-14 05:39

So let\'s say pretty often a script runs that opens a browser and does web things:

require \'watir-webdriver\'

$browser = Watir::Browser.new(:firefox, :prof         


        
5条回答
  •  梦谈多话
    2021-01-14 06:33

    Not so much a solution but a workaround for part 1 of my question, using pkill. Posting here since it turned out to be a lot less trivial than I had hoped.

    After the ruby script exits, its spawned processes (which may not at all belong in the same PID tree anymore, like firefox-bin) have a predictable "session leader" which turned out to be the parent of the bash shell calling rubyprogram.rb in my case. Available as $PPID in Bash, for when you have to go higher than $$.

    Thus to really clean up unwanted heavyweight processes eg. after a ruby crash:

    #!/bin/bash
    # This is the script that wraps on top of Ruby scripts
    
    ./ruby_program_using_watirwebdriver_browser.rb  myparams &  # spawn ruby in background but keep going below:
    
    sleep 11 # give Ruby a chance to launch its web browser
    
    pstree -panu $$  # prints out a process tree starting under Bash, the parent of Ruby. Firefox may not show!
    
    wait  # now wait for Ruby to exit or crash
    
    pkill -s $PPID firefox-bin  # should only kill firefox-bin's caused above, not elsewhere on the system
    
    # Another way without pkill, will also print out what's getting killed if anything:
    awk '$7=="firefox-bin" && $3=="'$PPID'" {print $1}' <(ps x -o pid,pgid,sess,ppid,tty,time,comm) | xargs -rt kill
    

    OPTIONAL And since I use a dedicated Xvfb Xwindows server just for webdriving on DISPLAY :99, I can also count on xkill:

    timeout 1s  xwininfo -display :99 -root -all |awk '/("Navigator" "Firefox")/ {print $1}' |xargs -rt  xkill -display :99 -id  
    # the timeout is in case xkill decides to wait for user action, when window id was missing
    

提交回复
热议问题