Skip to content

Instantly share code, notes, and snippets.

@cheezy
Created October 5, 2011 17:48
Show Gist options
  • Save cheezy/1265132 to your computer and use it in GitHub Desktop.
Save cheezy/1265132 to your computer and use it in GitHub Desktop.
Modal dialog example that crashes ruby after close
require 'rubygems'
require 'watir-webdriver'
require 'selenium-webdriver'
def wait_for_new_handle(original_handles, driver)
handles = nil
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until do
handles = driver.wd.window_handles
handles.size == original_handles.size + 1
end
handles
end
def open_modal(browser)
original_handles = browser.wd.window_handles
browser.button(:id => 'launch_modal_button').click
handles = wait_for_new_handle(original_handles, browser)
modal = (handles - original_handles).first
browser.wd.switch_to.window modal
end
dir = Dir.mktmpdir("modal-dialog")
htmls = DATA.read.scan(/--- (.+?) ---\n(.+?)(?=---|\z)/m)
htmls.each do |name, content|
File.open(File.join(dir, name), "w") { |io| io << content}
end
browser = Watir::Browser.new :ie
wait = Selenium::WebDriver::Wait.new
browser.goto "file://#{dir}/modal.html"
open_modal browser
browser.button(:id => 'close_window').click
browser.window(:title => 'Main Modal').when_present.use
browser.close
browser = Watir::Browser.new :ie
browser.goto "file://#{dir}/modal.html"
open_modal browser
open_modal browser
browser.button(:id => 'close_window2').click
browser.window(:title => 'Modal 1').when_present.use
browser.button(:id => 'close_window').click
browser.window(:title => 'Main Modal').when_present.use
browser.close
__END__
--- modal.html ---
<html>
<head>
<title>Main Modal</title>
</head>
<body>
<script type="text/javascript">
function modal() {
var retValue = window.showModalDialog(
"modal_1.html", self,
"status:no;resizable:Yes;help:no;maximize:no;minimize:no;scrollbars:no;");
}
</script>
<input id=launch_modal_button type=button onclick='return modal();' value="Launch a modal" />
</body>
</html>
--- modal_1.html ---
<html>
<head>
<title>Modal 1</title>
<script>
function delay_action(other_function) {
setTimeout(other_function, 3000);
}
function close_window() {
window.returnValue = 22
window.close()
}
function modal1() {
var retValue = window.showModalDialog(
"modal_2.html", self,
"status:no;resizable:Yes;help:no;maximize:no;minimize:no;scrollbars:no;");
}
</script>
</head>
<body>
<h2>Modal 1</h2>
<h3>Close buttons</h3>
<input id=close_window type=button onclick="close_window()" value="Close window"/>
<input id=delayed_close type=button onclick="delay_action('close_window()')" value="Close window with delay" />
<h3>Nested modal</h3>
<input id=launch_modal_button type=button onclick='return modal1();' value="Launch another modal"/>
</body>
</html>
--- modal_2.html ---
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Modal 2</title>
<script>
function delay_action(other_function) {
setTimeout(other_function, 3000);
}
function close_window() {
self.close()
}
</script>
</head>
<body>
<h2>Modal 2</h2>
<h3>Close buttons</h3>
<input id="close_window2" type="button" onclick="close_window()" value="Close window">
<input id="delayed_close2" type="button" onclick="delay_action('close_window()')" value="Close window with delay">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment