Skip to content

Instantly share code, notes, and snippets.

@jarib
Forked from cheezy/gist:1265132
Created October 7, 2011 16:11
Show Gist options
  • Select an option

  • Save jarib/1270685 to your computer and use it in GitHub Desktop.

Select an option

Save jarib/1270685 to your computer and use it in GitHub Desktop.

Revisions

  1. @cheezy cheezy revised this gist Oct 5, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -41,6 +41,7 @@ def open_modal(browser)

    browser.close

    browser = Watir::Browser.new :ie
    browser.goto "file://#{dir}/modal.html"

    open_modal browser
  2. @cheezy cheezy created this gist Oct 5, 2011.
    142 changes: 142 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,142 @@
    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.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>