1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00
capybara-webkit/spec/selenium_compatibility_spec.rb
Joe Ferris aeafb90498 Make caching behavior more like Selenium
We disabled caching entirely, because it sometimes led to unexpected
behavior in tests, and Qt 4.8 didn't behave the same as Qt 5.x.

However, this introduced issues:

* Selenium does perform caching, so capybara-webkit behaved differently
  when switching drivers
* Without any caching, assets were sometimes requested twice for the
  same page

This commit re-enables caching and attempts to improve the way we behave
on cached pages so that testing is less unpredictable. It adds tests to
ensure we continue to behave similarly to Selenium.
2015-05-15 10:32:22 -04:00

128 lines
3.6 KiB
Ruby

require 'spec_helper'
describe Capybara::Webkit, 'compatibility with selenium' do
include AppRunner
it 'generates the same events as selenium when filling out forms', selenium_compatibility: true do
run_application_for_html(<<-HTML)
<html><body>
<form onsubmit="return false">
<label for="one">One</label><input type="text" name="one" id="one" />
<label for="two">Two</label><input type="text" name="two" id="two" />
<label for="three">Three</label><input type="text" name="three" id="three" readonly="readonly" />
<label for="textarea">Textarea</label>
<textarea name="textarea" id="textarea"></textarea>
<input type="submit" value="Submit" id="submit" />
</form>
<script type="text/javascript">
window.log = [];
var recordEvent = function (event) {
log.push(event.target.id + '.' + event.type);
};
var elements = document.getElementsByTagName("input");
var events = ["mousedown", "mouseup", "click", "keyup", "keydown",
"keypress", "focus", "blur", "input", "change"];
for (var i = 0; i < elements.length; i++) {
for (var j = 0; j < events.length; j++) {
elements[i].addEventListener(events[j], recordEvent);
}
}
</script>
</body></html>
HTML
compare_events_for_drivers(:reusable_webkit, :selenium) do
visit "/"
fill_in "One", :with => "some value"
fill_in "One", :with => "a new value"
fill_in "Two", :with => "other value"
fill_in "Three", :with => "readonly value"
fill_in "Textarea", :with => "last value"
click_button "Submit"
end
end
it 'generates the same requests and responses as selenium', selenium_compatibility: true do
requests = []
app = Class.new(ExampleApp) do
before do
unless request.path_info =~ /favicon\.ico/
requests << request.path_info
end
end
get '/' do
<<-HTML
<html>
<head>
<script src="/one.js"></script>
<script src="/two.js"></script>
</head>
<body>Original</body>
</html>
HTML
end
get '/:script.js' do
''
end
get '/requests' do
<<-HTML
<html>
<body>
#{requests.sort.join("\n")}
</body>
</html>
HTML
end
end
run_application app
compare_for_drivers(:reusable_webkit, :selenium) do |session|
responses = []
session.visit "/"
responses << record_response(session)
session.visit "/"
responses << record_response(session)
session.visit "/requests"
responses << record_response(session)
requests.clear
responses.join("\n\n")
end
end
def compare_events_for_drivers(first, second, &block)
compare_for_drivers(first, second) do |session|
session.instance_eval(&block)
session.evaluate_script("window.log")
end
end
def compare_for_drivers(first, second, &block)
for_driver(first, &block).should == for_driver(second, &block)
end
def for_driver(name, &block)
session = Capybara::Session.new(name, AppRunner.app)
result = yield session
result
end
def record_response(session)
[
session.current_url,
normalize_body(session.body)
].join("\n")
end
def normalize_body(body)
if body.length > 0
Nokogiri::HTML.parse(body).at("//body").text.strip
else
"(empty)"
end
end
end