Open Firefox browser with Ruby automation script - ruby

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.

You can start any program in ruby with:
`firefox http://www.google.com`
or
system("firefox http://www.google.com")

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

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

I encountered two issues while getting this running:
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.
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")
open's -a option Opens with the specified application.
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"
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.

Related

Not able to change default download directory for chrome with selenium hub docker and ruby watir

After a few days of searching and experimenting with any of the solutions I could find online, I give up and want to get some help from the community.
Ruby gems (ruby 2.5.1):
watir 6.11.0
selenium-webdriver 3.4.1
Docker:
selenium/node-chrome-debug:3.14
selenium/hub:3.14
My ruby code:
prefs = {
download: {
prompt_for_download: false,
default_directory: download_directory
}
}
browser = Watir::Browser.new(:chrome, url: selenium_hub_url, options: {prefs: prefs})
Our set-up is:
Run a selenium/hub and a selenium/node-chrome-debug. Something that might be different is that we are mounting the /tmp of the base OS as /hosttmp/tmp in the node container
Make the selenium/node-chrome-debug talk to selenium/hub
Make the browser automation talk to the selenium/hub using the code provided above
The problem is that I was never able to set the default download directory. However, all other parts are working correctly. The VNC window shows the browser is working correctly despite the default download directory settings. It is always /home/seluser/Downloads
Things I have tried:
Other people's ideas such as different ways to specify the options and preferences. (e.g. using the Capabilities)
Docker security-related settings such as: --privileged --security-opt apparmor:unconfined --cap-add SYS_ADMIN
On the base OS, chmod 777 for the download_directory. The download_directory, for example, /tmp/tmp.123 on the base OS, which is mounted as /hosttmp/tmp/tmp.123 in the chrome node container, I could see it and make a few read/write operations in this folder inside the container or on the base OS
Tweaks about the interesting ruby symbol/string stuff when creating a Hash object.
Does anyone have more ideas about what could lead to this situation? What else I could try? And is there any log that I could refer to. There is no error or warning when running the code. Thanks in advance.
I'm using Java+Docker+Selenium+Chrome for automation test and also met similar issue with you. Please find my solutions below and try if it works for your case.
Don't set default download directory in the options, just leave "/home/seluser/Downloads" as it is.
When you start up the chrome node on docker, please add the parameter of volume that could transfer the downloaded files to the directory you want.
e.g. docker run -d -p 5900:5900 --link myhub:hub -v :/home/seluser/Downloads selenium/node-chrome-debug:3.14.0
In my case, the JDK environment and my test script is on Linux machine while the selenium webdriver & browser are all on docker, so once the file downloaded by browser it cannot saved directly on Linux machine, you have to mount the local directory with default directory on docker. Then you could find the file saved in the directory you want.
Thanks & Regards!
Jing
Did you define options = Selenium::WebDriver::Chrome::Options.new?
We use
options = Selenium::WebDriver::Chrome::Options.new
prefs = {
prompt_for_download: false,
default_directory: download_directory
}
options.add_preference(download: prefs)
and then you would want something like
browser = Watir::Browser.new(:chrome, url: selenium_hub_url, options: options)
But maybe the main problem is just that you are using
options: {prefs: prefs}
instead of
options: {download: prefs}
Okay, by digging into the source code of the Watir and Selenium-Webdriver, I think I know the 'root cause'.
I have created an issue since I am not sure if this is a bug or a 'feature' The issue
Also, I have a workaround for my case, in watir/capabilities.rb:
Change
#selenium_browser = browser == :remote || options[:url] ? :remote : browser
to
#selenium_browser = browser == :remote ? :remote : browser
This shouldn't be the final solution as it might not be a good idea. Will wait for what the Watir people say about this.

Headless operations don't work inside Sinatra route

I am using the headless and selenium-webdriver gems to launch a headless Firefox browser:
headless = Headless.new(
video: {
frame_rate: 12,
codec: 'libx264'
}
)
headless.start
driver = Selenium::WebDriver.for(:firefox)
With this code I can write the following:
driver.navigate.to("http://google.com")
Yet the following raises an error after I visit '/' in the browser:
get '/' do
driver.navigate.to("http://google.com")
erb :root
end
The error is as follows:
*** Errno::ECONNREFUSED Exception: Failed to open TCP connection to 127.0.0.1:7055 (Connection refused - connect(2) for "127.0.0.1" port 7055)
I'm pretty sure this is because the driver.navigate is not being called in the headless scope, therefore it can't connect to the Firefox instance.
I have also tried using the modular sinatra style, but the same error occurred.
workaround
What I ended up doing is separating the headless server in a separate script. This script has loop and gets input, printing the output of running the command in the headless scope. Then from the sinatra server, i use PTY.spawn to instantiate the server and pass around its stdin and stdout so I can use it in my Sinatra app. This way the headless script is only run once (therefore multiple headless servers aren't started) and I can connect to it from my Sinatra routes. I can't interact with the headless script's variables or methods directly - I need to just use i/o.
I am hoping for an answer which hows how to make the original code work, though (when the sinatra app's routes are called in headless scope)
One possible trick that can help:
this = self
get '/' do
this.driver.navigate.to("http://google.com")
erb :root
end
I ended up getting this working.
At first I thought that what fixed it was doing the headless environment initialization in the scope of a sinatra route, i.e.
get '/' { do_initialization_here }
The real fix may have been in the way I was calling Headless.new (I originally had a bunch of options tacked on and I removed all them).

How do I run my selenium ruby scripts on different browsers?

I have written my script specifically for FF. Now I wish to run that same script on Chrome as well as IE. Also I want to run my tests in following order :
Open browser1.
Run script on that browser.
Close browser1.
Open browser2.
Run script on that browser.
Close browser2.
Please help.
In order to run your tests on :
1.Chrome : you will need to install latest Chrome driver, unzip it and paste its path in the environment variables.
2.IE : you will need to install IEDriver server unzip it and paste its path in the environment variables and enable protected mode for each zone in following way (Internet options->security tab->enable protected mode checkbox).
For running your tests as per the way you mentioned, not sure what framework you're using or whatever, but you can do this with a loop. You can do the following :
def all_browsers
browsers = [:firefox,:chrome,:ie].each do |br|
$driver = Selenium::WebDriver.for br
$driver.manage.window.maximize
$driver.navigate.to("http://google.com")
end
$driver.quit
end

Using watir to control multiple firefox instances

I'm using watir in a Windows environment with FireFox 3.6 via FireWatir. I can successfully run a single watir test without issue. I need to be able to either:
a. Create and manage multiple browser instances or tabs from a single test script
or
b. Run two test scripts simultaneously from separate instances of ruby
Currently attempts at both a and b result in both Browser.new commands returning the same browser instance, thus the scripts step all over one-another.
Are either of these scenarios possible? I've seen some posts from 2008/2009 about a multiple browser branch, did this code eventually make it into the general release?
Thanks,
Jeff
I have just tried it with watir-webdriver gem (installation instructions), and it works:
$ irb
>> require "watir-webdriver"
=> true
>> b1 = Watir::Browser.new :ff
=> #<Watir::Browser:0x101574930 url="about:blank" title="">
>> b1.goto "google.com"
=> "http://www.google.hr/"
>> b2 = Watir::Browser.new :ff
=> #<Watir::Browser:0x1023658f0 url="about:blank" title="">
>> b2.goto "yahoo.com"
=> "http://www.yahoo.com/"
Vapir-firefox is a much-improved fork of Firewatir that resolves its issues with handling multiple windows (among many other improvements).
http://vapir.org/

ies4linux with selenium and rspec

I use selenium, usually with firefox, to test my rails apps and it's all fine. I want to run my tests in IE6 as well. I'm in ubuntu, using the ruby selenium-client gem. For IE6 i use ies4linux, this is an executable which is at /home/max/.ies4linux/bin/ie6
I'm editing my selenium conf to try to get it to use the above, but can't get it working. Here's what i have at the moment:
SELENIUM_CONF = {
:client_options => {
:url => "http://awebsite.com",
:host => "localhost",
:port => 4444,
:browser => "*iexplore /home/max/.ies4linux/bin/ie6",
:javascript_framework => :jquery
}
}
Then when i make a browser in my scripts i call
Selenium::Client::Driver.new(SELENIUM_CONF[:client_options])
It's not happy with what i have in the :browser field at the moment - i get this error:
"Failed to start new browser session: Error while launching browser"
I also tried
:browser => "/home/max/.ies4linux/bin/ie6",
But got a "Browser not supported" error as it expects one from list, *iexplore in this case.
Can anyone tell me how i can get this working?
thanks, max
I don't think there's a way to do that. Selenium uses iehta, and some not-so-used parts of ie that could not be bundled in ie4linux, and even if there were, I wouldn't trust on a test that runs IE under linux.
Why not creating a VM using VirtualBox and pointing your tests to an RC server inside the VM?

Resources