Last active
August 29, 2015 14:24
-
-
Save samnissen/c9b9ffc3c49e2b828ca5 to your computer and use it in GitHub Desktop.
Confirming access to alerts in a StackOverflow question, take 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' | |
require 'yaml' | |
class MyBrowser | |
attr_accessor :b, :h | |
def initialize | |
setup | |
end | |
def confirm_alert_appears | |
@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? | |
raise 'Could not access alerts' unless result | |
end | |
private | |
def setup | |
if !@b | |
decapitate if OS.linux? | |
@b = Watir::Browser.new :firefox | |
end | |
end | |
def decapitate | |
if !@h | |
@h = Headless.new | |
@h.start | |
end | |
end | |
end | |
begin | |
m = MyBrowser.new | |
m.confirm_alert_appears | |
# Save the MyBrowser object to file | |
File.open('some_file.txt', 'w') {|f| f.write(YAML.dump(m)) } | |
# ... | |
# Do whatever you want to do, | |
# wait however long you want to wait. | |
m = nil | |
# ... | |
sleep(5) | |
m = YAML.load(File.read('some_file.txt')) | |
m.confirm_alert_appears | |
ensure | |
m.b.close if m.b | |
m.h.destroy_sync if m.h | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment