Skip to content

Instantly share code, notes, and snippets.

@jarib
Forked from jdelStrother/capybara_iframes.rb
Created July 11, 2011 14:59
Show Gist options
  • Save jarib/1076026 to your computer and use it in GitHub Desktop.
Save jarib/1076026 to your computer and use it in GitHub Desktop.
require 'selenium-webdriver'
require 'rack'
require 'rack/handler/thin'
class IframeServer
def start
@pid = fork { Rack::Handler::Thin.run(self, :Port => 9292) }
poller = Selenium::WebDriver::SocketPoller.new("localhost", 9292, 30)
raise "could not start #{self.inspect}" unless poller.connected?
end
def stop
return unless @pid
Process.kill(:TERM, @pid)
Process.waitall
@pid = nil
end
def call(env)
r = Rack::Request.new(env)
content = case r.path
when '/container.html'
pages[0]
when '/iframe1.html'
pages[1]
when '/iframe2.html'
pages[2]
when '/final.html'
pages[3]
end
[200, {'Content-Type'=>'text/html'}, [content]]
end
def pages
@pages ||= DATA.read.split("===")
end
end
server = IframeServer.new
server.start
begin
driver = Selenium::WebDriver.for :firefox
wait = Selenium::WebDriver::Wait.new(:timeout => 5) # seconds
driver.get "http://localhost:9292/container.html"
driver.switch_to.frame("ThreeD") do
driver.find_element(:xpath => "//input[@value='Continue']").click
end
driver.find_element(:tag_name => "body").text.include?("all done")
puts 'yay, it worked'
ensure
driver.quit
server.stop
end
__END__
<html>
<head>
<style> iframe { border:2px solid red; } </style>
</head>
<body>
<h1> iframe :</h1>
<iframe id='ThreeD' src='/iframe1.html'>iframe content goes here </iframe>
</body>
</html>
===
<html>
<body>
<form action="/iframe2.html">
<input type="submit" value="Continue"/>
</form>
</body>
</html>
===
<html>
<body>
<script>
window.parent.location = '/final.html'
</script>
</body>
</html>
===
<html>
<body>
Yay, all done!!
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment