Open the Default Browser in Ruby

Open the default browser in Ruby

Cross-platform solution:

First, install the Launchy gem:

$ gem install launchy

Then, you can run this:

require 'launchy'

Launchy.open("http://stackoverflow.com")

Open browser with URL in Ruby

It looks like you are pushing the Browser with too many urls at the same time.

Using sleep seems to work fine.

15.times {|i| `open http://google.com?q=#{i}` }
# LSOpenURLsWithRole() failed with error -1712 for the URL http://google.com?q=5.
# LSOpenURLsWithRole() failed with error -1712 for the URL http://google.com?q=6.
# LSOpenURLsWithRole() failed with error -1712 for the URL http://google.com?q=12.
# LSOpenURLsWithRole() failed with error -1712 for the URL http://google.com?q=14.
# => 15
15.times {|i| sleep(0.2); `open http://google.com?q=#{i}` }
# => 15

Open local HTML file in default web browser

You can use the launchy gem.

First, install the gem:

$ [sudo] gem install launchy

Then, in your ruby code:

require 'rubygems'
require 'launchy'

Launchy::Browser.run("/path/to/file.html")

change Watir::Browser.new to IE by default

Yes you can do so. For that You have to change browser.rb file,

def initialize(browser = :firefox, *args) 
to
def initialize(browser = :IE, *args)

and add IE extension on system path.

How do I launch a website (with GET parameters) in the default browser in Ruby?

Yep, it's pretty old Launchy's bug. It can be fixed adding ^ in the url before & char (as the guy who opened an issue suggests). I'll make a patch ASAP. So, right now you can use just plain system method for this job:

system("start http://google.com?a=1^&b2=2")


Related Topics



Leave a reply



Submit