Im trying to implement the following methods from selenium-webdriver (ruby)
require "rubygems"
require "selenium-webdriver"
require "test/unit"
class SwitchToPopup3 < Test::Unit::TestCase
def setup
@driver = Selenium::WebDriver.for :firefox
@base_url = (URL of my test website)
@driver.manage.timeouts.implicit_wait = 30
@verification_errors = []
end
def teardown
@driver.quit
assert_equal [], @verification_errors
end
def test_switch_to_popup3
.
.
puts @driver.get_all_window_ids()
puts @driver.get_all_window_titles()
puts @driver.get_all_window_names()
.
.
end
The error I keep getting is
NoMethodError: undefined method `get_all_window_ids' for # <Selenium::WebDriver::Driver:0x101e4b040 browser=:chrome>
/Users/rsucgang/Documents/Aptana Studio 3 Workspace/Testing/SwitchToPopup2.rb:37:in `test_switch_to_popup3'
I've studied the documentation for the ruby bindings for selenium-webdriver
http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/Client/GeneratedDriver.html#get_all_window_titles-instance_method
Ultimately my goal here is to run my automation script:
I've googled and researched this on the internet and I have not gotten any much information.
Thanks and please let me know if you need more information.
Justin, you have a good approach. But there is a gotcha in assuming that the window handles will return in the correct order. This isn't always the case across all browsers. I outline a slightly different approach in my free weekly Selenium tip newsletter (elemental-selenium.com).
It goes like this:
@driver.get 'http://the-internet.herokuapp.com/windows'
main_window = @driver.window_handle
@driver.find_element(css: '.example a').click
windows = @driver.window_handles
windows.each do |window|
if main_window != window
@new_window = window
end
end
@driver.switch_to.window(main_window)
@driver.title.should_not =~ /New Window/
@driver.switch_to.window(@new_window)
@driver.title.should =~ /New Window/
You can see the full tip here.
The problem is that the get_all_window_ids
is for Selenium::Client rather than Selenium::Webdriver. I believe that Selenium::Client is the old version Selenium and the API is not the same as Selenium::Webdriver (see here). Since you are using Selenium::Webdriver, this explains why you get an 'undefined method' error.
For Selenium::Webdriver, the only way I know how to switch between windows is using:
@driver.switch_to.window("<window_handle>")
You can get all the known window_handles by:
@driver.window_handles
#=> Returns all window handles as an array of strings
If you want to switch to the popup you just opened you can do the following. Note that this assumes .window_handles
are in the order that the windows were opened, which I believe is true:
@driver.switch_to.window @driver.window_handles.last
To summarize, assuming you only care about accessing the popup (and not about accessing by name) you can do:
#Click control that opens popup
@driver.find_element(:id, 'button that opens popup').click
#Switch to popup
@driver.switch_to.window @driver.window_handles.last
#Do actions in new popup
@driver.find_element(:id, 'id of element in popup').click
Note that if after working with the popup, you will want to return to the original window, then I suggest you do the following. By passing a block to the switch_to.window
, the block will be executed in the popup and when the block ends @driver
will automatically point back to the original window.
#Click control that opens popup
@driver.find_element(:id, 'button that opens popup').click
#Switch to popup
@driver.switch_to.window( @driver.window_handles.last ){
#Do actions in new popup
@driver.find_element(:id, 'id of element in popup').click
}
#Continue with original window
@driver.find_element(:id, 'button in original window').click
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With