Created
March 18, 2013 20:28
-
-
Save jpurcell001/5190514 to your computer and use it in GitHub Desktop.
Failure case for poltergeist - missed click
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
require 'rubygems' | |
require 'bundler/setup' | |
Bundler.require | |
require "capybara" | |
require "capybara/poltergeist" | |
Capybara.register_driver :webkit do |app| | |
Capybara::Webkit::Driver.new(app) | |
end | |
Capybara.register_driver :poltergeist do |app| | |
Capybara::Poltergeist::Driver.new(app, debug: false) | |
end | |
html = DATA.read | |
app = proc { |env| [200, { "Content-Type" => "text/html" }, [html] ] } | |
def click_checkbox | |
a = @sess.find(".a") | |
@sess.within a.find(".b .c") do | |
@sess.find_field('test').set(true) | |
end | |
end | |
@sess = Capybara::Session.new(:poltergeist, app) | |
@sess.visit("/") | |
(1..100).each do |i| | |
puts "Iteration #{i}" | |
@sess.execute_script "fakeAjax(50)" | |
click_checkbox | |
@sess.has_checked_field?('test') or raise "Iteration #{i} failed" | |
end | |
__END__ | |
<!doctype html> | |
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script language="javascript"> | |
function fakeAjax(startDelay) { | |
$("body").empty(); | |
window.setTimeout(function() { | |
var $checkbox, $el = $("body"), insertionDelay; | |
// Insert the elements that the capybara selectors find within | |
"abc".split('').forEach(function(letter) { | |
$el = $("<div />").addClass(letter).appendTo($el); | |
}); | |
// Insert the element being searched for to click on | |
$checkbox = $("<input type='checkbox' name='test' />").appendTo($el); | |
// After a random amount of time, insert a box that shifts the y-position of | |
// the input checkbox being searched for. | |
insertionDelay = 10 + Math.floor(Math.random() * 10); | |
window.setTimeout(function() { | |
$("<div />").css("height", 100).insertBefore($checkbox); | |
}, insertionDelay); | |
}, startDelay); | |
}; | |
</script> | |
</head> | |
<body /> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment