Open Firefox browser with Ruby automation script

2019-07-15 17:34发布

问题:

How is it possible to open FireFox browser by Ruby(for automation script)? I use @browser = RSpecSeleniumHelper.connect_browser('/admin/', '*firefox'), but it doesn't work.

回答1:

You can start any program in ruby with:

`firefox http://www.google.com`
or
system("firefox http://www.google.com")


回答2:

You can use Watir, as it supports Firefox also: http://wtr.rubyforge.org/platforms.html



回答3:

You may have to check if the Selenium Remote Control is start or not, normally it is running at port 4444.

java -jar selenium-server-xxx.jar

then you can use

@browser = Selenium::Client::Driver.new(
    :host => "localhost",
    :port => 4444,
    :browser => "*firefox", #*iexplore, *firefox3, *safari...
    :url => "http://www.google.com/",
    :timeout_in_second => 60)
@browser.start_new_browser_session

Hope this helps, you can find more demo by download Selenium RC



回答4:

I encountered two issues while getting this running:

  1. If you are running your Ruby app from MacOS, the command firefox may not be properly aliased by default and so may fail without errors printed to your Ruby console.

  2. If you already have an instance of Firefox open, you will get a message saying "Close Firefox - A copy of Firefox is already open. Only one copy of Firefox can be open at a time."

This code fixes both problems:

system("open -a /Applications/Firefox.app/Contents/MacOS/firefox-bin http://www.google.com http://www.cpap.com")
  1. open's -a option Opens with the specified application.
  2. The file path list works for me. If it won't load for you, first drop it and try plain "firefox" and failing that try "/Applications/Firefox.app/Contents/MacOS/firefox"
  3. The example above shows two URLs separated by a space. You can use just one URL or as many as you care to following this pattern.