Popup support added for selenium.

This commit is contained in:
jarra 2010-08-27 21:00:08 +02:00 committed by Jonas Nicklas
parent 0bf8796829
commit 80df9fff5a
8 changed files with 108 additions and 1 deletions

View File

@ -39,6 +39,10 @@ class Capybara::Driver::Base
raise Capybara::NotSupportedByDriverError
end
def within_popup(popup_handle)
raise Capybara::NotSupportedByDriverError
end
def wait?
false
end

View File

@ -140,6 +140,13 @@ class Capybara::Driver::Selenium < Capybara::Driver::Base
browser.switch_to.window old_window
end
def within_popup(popup_handle)
old_window = browser.window_handle
browser.switch_to.window popup_handle
yield
browser.switch_to.window old_window
end
private
def url(path)

View File

@ -30,7 +30,7 @@ module Capybara
:all, :attach_file, :body, :check, :choose, :click_link_or_button, :click_button, :click_link, :current_url, :drag, :evaluate_script,
:field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content?, :has_css?,
:has_no_content?, :has_no_css?, :has_no_xpath?, :has_xpath?, :locate, :save_and_open_page, :select, :source, :uncheck,
:visit, :wait_until, :within, :within_fieldset, :within_table, :within_frame, :has_link?, :has_no_link?, :has_button?,
:visit, :wait_until, :within, :within_fieldset, :within_table, :within_frame, :within_popup, :has_link?, :has_no_link?, :has_button?,
:has_no_button?, :has_field?, :has_no_field?, :has_checked_field?, :has_unchecked_field?, :has_no_table?, :has_table?,
:unselect, :has_select?, :has_no_select?, :current_path, :scope_to, :click
]
@ -201,6 +201,19 @@ module Capybara
end
end
##
#
# Execute the given block within the given popup given the id of that popup. Only works on
# some drivers (e.g. Selenium)
#
# @param [String] locator Id of the popup
#
def within_popup(popup_handle)
driver.within_popup(popup_handle) do
yield
end
end
##
#
# Retry executing the block until a truthy result is returned or the timeout time is exceeded

View File

@ -154,6 +154,47 @@ shared_examples_for "driver with frame support" do
end
end
shared_examples_for "driver with popup support" do
describe '#within_popup' do
before(:each) do
@driver.visit('/within_popups')
end
after(:each) do
@driver.within_popup("firstPopup") do
@driver.evaluate_script('window.close()')
end
@driver.within_popup("secondPopup") do
@driver.evaluate_script('window.close()')
end
end
it "should find the div in firstPopup" do
@driver.within_popup("firstPopup") do
@driver.find("//*[@id='divInPopupOne']")[0].text.should eql 'This is the text of divInPopupOne'
end
end
it "should find the div in secondPopup" do
@driver.within_popup("secondPopup") do
@driver.find("//*[@id='divInPopupTwo']")[0].text.should eql 'This is the text of divInPopupTwo'
end
end
it "should find the divs in both popups" do
@driver.within_popup("secondPopup") do
@driver.find("//*[@id='divInPopupTwo']")[0].text.should eql 'This is the text of divInPopupTwo'
end
@driver.within_popup("firstPopup") do
@driver.find("//*[@id='divInPopupOne']")[0].text.should eql 'This is the text of divInPopupOne'
end
end
it "should find the div in the main window after finding a div in a popup" do
@driver.within_popup("secondPopup") do
@driver.find("//*[@id='divInPopupTwo']")[0].text.should eql 'This is the text of divInPopupTwo'
end
@driver.find("//*[@id='divInMainWindow']")[0].text.should eql 'This is the text for divInMainWindow'
end
end
end
shared_examples_for "driver with cookies support" do
describe "#reset!" do
it "should set and clean cookies" do

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>This is the title of the first popup</title>
</head>
<body>
<div id="divInPopupOne">This is the text of divInPopupOne</div>
</body>
</html>

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>This is the title of popup two</title>
</head>
<body>
<div id="divInPopupTwo">This is the text of divInPopupTwo</div>
</body>
</html>

View File

@ -0,0 +1,25 @@
<html>
<head>
<title>With Popups</title>
<script language="javascript" type="text/javascript">
<!--
function popItUp(name, url) {
newwindow=window.open(url,name,'height=200,width=150');
if (window.focus) { newwindow.focus() }
return false;
}
function init() {
popItUp('firstPopup', '/popup_one');
popItUp('secondPopup', '/popup_two');
}
window.onload = init;
// -->
</script>
</head>
<body>
<div id="divInMainWindow">This is the text for divInMainWindow</div>
</body>
</html>

View File

@ -8,6 +8,7 @@ describe Capybara::Driver::Selenium do
it_should_behave_like "driver"
it_should_behave_like "driver with javascript support"
it_should_behave_like "driver with frame support"
it_should_behave_like "driver with popup support"
it_should_behave_like "driver without status code support"
it_should_behave_like "driver with cookies support"
end