Skip to content

Instantly share code, notes, and snippets.

@samnissen
Last active August 29, 2015 14:24
Show Gist options
  • Save samnissen/76bc20505ba93257261e to your computer and use it in GitHub Desktop.
Save samnissen/76bc20505ba93257261e to your computer and use it in GitHub Desktop.
Confirming access to alerts in a StackOverflow question
#!/bin/env ruby
# encoding: utf-8
# Trying to replicate error described in a StackOverflow Question
# http://stackoverflow.com/questions/31065711/watir-webdriver-does-not-show-dialogs-in-firefox-when-finished-testing
require 'os'
require 'headless'
require 'watir-webdriver'
def click_on_button(b)
b.goto "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert"
frame_container = b.div(:class => "container").div(:class => "iframecontainer")
iframe = frame_container.div(:class => "iframewrapper").iframe(:id => "iframeResult")
iframe.button(:onclick => "myFunction()").click
result = true if b.alert.exists?
b.alert.close if b.alert.exists?
return [result, b]
end
begin
h = Headless.new if OS.linux?
h.start if h
b = Watir::Browser.new :firefox
results = click_on_button(b)
success = results[0]
b = results[1]
raise 'Unable to find alert on the first pass.' unless success
sleep(5)
click_on_button(b)
success = results[0]
raise 'Unable to find alert on the second pass.' unless success
ensure
h.destroy_sync if h
b.close if b
end
# => No errors raised.
# => FF 38, selenium-webdriver (2.46.2), watir-webdriver (0.7.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment