Watir question

Is there anyway could I able to print the index of the current browser?

If you mean take all of the windows and find the index of one, yes:

irb(main):007:0> b1.windows.count
=> 2
irb(main):008:0> b2.windows.count
=> 2
irb(main):009:0> b1.windows.index b1
=> 0
irb(main):010:0> b1.windows.index b2
=> 1

yes this is what I want. Thank you so much.

RAJ

begin
puts $browser.windows.index($browser)
rescue =>e
puts e.class
end

hi
it gives me NoMethodError

So I used the way which I’ve given below.

begin
puts $browser.windows.count - 1
rescue =>e
puts e.class
end

Now I am getting the index of the window

You need to pass an argument to #index: the browser object you want the
index of.

hi

if i write the code like

puts $browser.windows.index($browser)

then it throws this error

puts $browser.windows.index($browser)

You still haven’t shown the error message, despite mentioning it twice.

hi

if i print $browser.windows.index then it’s printing
#Enumerable::Enumerator:0x11c36f88

“browser” as a method does not appear anywhere in the code you have
shown. This is referring to a line of code you haven’t posted here.

oh extremely sorry. I was about to paste the error message but
mistakenly pasted puts statement twice. Here it is,

NoMethodError: undefined method `browser’ for #Watir::IE:0x43895e8

The error message is incomplete, and so is your code.
Please attach the code file and post the complete error message
including line number, or this is impossible to diagnose.

puts $browser.windows.index($browser)

Error
C:/Ruby187/lib/ruby/gems/1.8/gems/watir-classic-3.0.0/lib/watir-classic/window.rb:57:in
==': undefined methodbrowser’ for #Watir::IE:0x118d9a18
(NoMethodError)
from C:/WATIR/Life/Main/Test.rb:24:in `index’
from C:/WATIR/Life/Main/Test.rb:24

I don’t have that version of watir-classic. Can you try updating the gem
and see if the problem persists?

hi,

I am very sorry to say that Updation to the higher version of Ruby or
watir is not possible for me since I have already been in a project
where I am using Ruby 1.8.7 and Watir 3.0.0 and watir-classic-3.0.0.

This looks like a bug which was fixed at some point between 3.0.0 and
3.7.0.

Oh ok thank you. I will try to update in future as soon as this project
comes to end.

ok i have written the below code

begin
puts $browser.windows.index($browser)
rescue =>e
puts e.class
end

it writes in the console

NoMethodError