Using watir to control multiple firefox instances - firefox

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/

Related

how to show element in browed page in selenium testing?

I tried this to do selenium testing
require 'watir'
b = Watir::Browser.start 'http://www.gmail.com'
t = b.text_field id: 'entry_1000000'
t.exists?
t.set 'your name'
t.value
but not fetching any text in browed page(www.gmail.com).
require 'watir'
b = Watir::Browser.start 'http://www.gmail.com'
t = b.text_field id: 'entry_1000000'
t.exists?
t.set 'your name'
t.value
put exact id of that text field . id or xpath or name etc
First lets check if you have whats needed for that script to run, and start from there if not.
Ruby installed
Watir gem installed
Browser installed (chrome or firefox)
Geckodriver downloaded and in path (if firefox installed)
Chromedriver downloaded and in path (if chrome installed)
After that, save the code below in file, lets name it gmail.rb.
require 'watir'
b = Watir::Browser.new :chrome #or :firefox
b.goto "www.gmail.com"
b.text_field(:id => 'identifierId').set "your_email#gmail.com"
b.span(:text => "Next").click
b.text_field(:name => "password").set "your_password"
b.span(:text => "Next").click
Open command prompt or terminal and run the following command from folder where gmail.rb is located
ruby gmail.rb
Browser should open, navigate to gmail, input email, password and submit it, but from there gmail security kicks in so this is not a good use case for automation, at least not this way.
You can try the code above and see how it works, and if not, post errors here. But if you actually need to automate reading gmail, there is a really nice gem that helps you do that https://github.com/gmailgem/gmail

Watir WebDriver browser object loses current window upon opening new tab (FIREFOX)

I want to test clicking on multiple links on the same page, but then be able to switch active tabs. However once I click on a link with target=_blank or if I open a new tab on purpose, it routes me to a new tab and I can't get back to the old tab.
Note: I am using Firefox for testing. Chrome does appear to work properly and provides a window count of 2
Here is a simple IRB snippet to show what I mean:
2.2.1 :001 > require 'watir-webdriver'
=> true
2.2.1 :002 > b = Watir::Browser.new :firefox, :profile => 'default'
=> #<Watir::Browser:0x..fa8c7116334ddce82 url="about:blank" title="">
2.2.1 :003 > b.goto 'amazon.com'
=> "http://amazon.com"
2.2.1 :004 > b.a(:class => 'nav-logo-link').click(:command, :shift)
=> nil
2.2.1 :005 > b.windows.count
=> 1
I see two tabs opened, which the (:command, :shift) option is meant to do.. but how do I go back to the first one if the browser object only has 1 window?
It just seems that I can't find anyone having the same problem so perhaps its a firefox issue or my environment maybe?
Running on Mac OSX, FF version 44, Ruby 2.2.1, watir-webdriver 0.9.1
Check this out:
require 'watir-webdriver'
b = Watir::Browser.new
b.goto "amazon.com"
link = b.a(:class => 'nav-logo-link')
b.execute_script("return arguments[0].target = '_blank'", link)
link.click
puts b.windows.count
Main idea is not to try to use a link in a special way to get two windows but modify the link to get two windows in a natural way.

Unable to automate (AutoIT) in Ruby because of Watir error

I've tried a few times now to run a Watir browser and then use the AutoIt ruby library (au3) to access a right click context menu but it wasn't working, turns out the au3 library is disappearing for some reason (I'm a little new to Ruby but when I require it again after the browser opens it comes back false for some reason:
irb(main):001:0> require "au3"
=> true
irb(main):002:0> require "watir-webdriver"
=> true
irb(main):003:0> browser = Watir::Browser.new :chrome
Starting ChromeDriver (v2.3) on port 9515
[4868:5640:1025/104947:ERROR:textfield.h(176)] NOT IMPLEMENTED
=> #<Watir::Browser:0x449008c8 url="about:blank" title="about:blank">
<to "https://github.com/lmmx/watir-paper-scanner/blob/master/bookworm.rb"
[WARNING:..\..\..\..\flash\platform\pepper\pep_module.cpp(63)] SANDBOXED
=> "https://github.com/lmmx/watir-paper-scanner/blob/master/bookworm.rb"
irb(main):005:0> require "au3"
=> false
irb(main):006:0>
I'm guessing that whatever's SANDBOXED is crucial to running au3? The browser still works fine... Will try and update the watir-related things, but I only installed it a week or 2 so it shouldn't be out of date already - anyone help me fix it?
The reason it's returning false is because you have already used
require "au3"
on the top line of your code, so when you require it again it's already there.

Watir-webdriver: Chrome unhandled error on site where IE and Firefox working fine

So I think this might be an error with the ChromeDriver but wanted to ask here first.
I'm having trouble using just about every webdriver function with chrome, yet IE and Firefox is working fine.
The site is www.bing.com's sign in with Windows live site.
https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1314808262&rver=6.0.5286.0&wp=MBI&wreply=http:%2F%2Fwww.bing.com%2FPassport.aspx%3Frequrl%3Dhttp%253a%252f%252fwww.bing.com%252f&lc=1033&id=264960
Once at that site when using chrome the only two functions I can get to work properly are
#browser.title
#browser.html
are the only functions that work properly. When I try
#browser.links.size
#browser.button[0].flash
#browser.button[0].click
I get
Selenium::WebDriver::Error::UnhandledError:
Using :id etc to reference the objects doesn't work either.
However IE and Firefox can interact with the site fine.
If you need code or clarification let me know, but basically I can't get chrome to interact with that log in site. (Other sites work just fine on chrome)
You will need to install the ChomeDriver, which involves downloading the binary and putting it on your PATH. See: http://watirwebdriver.com/chrome/
This site works perfectly for me in Chrome.
Eg.
$irb
ruby-1.9.2-p290 :001 > require 'watir-webdriver'
=> true
ruby-1.9.2-p290 :002 > b = Watir::Browser.new :chrome
=> #<Watir::Browser:0x6c6e06ba1a564e50 url="about:blank" title="about:blank">
ruby-1.9.2-p290 :003 > b.goto "https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1314808262&rver=6.0.5286.0&wp=MBI&wreply=http:%2F%2Fwww.bing.com%2FPassport.aspx%3Frequrl%3Dhttp%253a%252f%252fwww.bing.com%252f&lc=1033&id=264960"
=> "https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1314808262&rver=6.0.5286.0&wp=MBI&wreply=http:%2F%2Fwww.bing.com%2FPassport.aspx%3Frequrl%3Dhttp%253a%252f%252fwww.bing.com%252f&lc=1033&id=264960"
ruby-1.9.2-p290 :004 > b.title
=> "Welcome to Windows Live"
ruby-1.9.2-p290 :005 > b.html
=> "<!-- ServerInfo: BAYIDSLGN1M37 2011.07.22.19.28.58...

Open Firefox browser with Ruby automation script

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.

Resources