2011-04-19 05:07:42 -04:00
|
|
|
# -*- encoding: UTF-8 -*-
|
|
|
|
|
2011-02-26 17:02:00 -05:00
|
|
|
require 'spec_helper'
|
2011-09-26 10:33:25 -04:00
|
|
|
require 'capybara/webkit'
|
2011-02-26 17:02:00 -05:00
|
|
|
|
2012-11-16 23:42:55 -05:00
|
|
|
module TestSessions
|
|
|
|
Webkit = Capybara::Session.new(:reusable_webkit, TestApp)
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara::SpecHelper.run_specs TestSessions::Webkit, "webkit"
|
|
|
|
|
2011-02-26 17:02:00 -05:00
|
|
|
describe Capybara::Session do
|
2011-04-14 10:16:56 -04:00
|
|
|
subject { Capybara::Session.new(:reusable_webkit, @app) }
|
|
|
|
after { subject.reset! }
|
|
|
|
|
|
|
|
context "slow javascript app" do
|
|
|
|
before(:all) do
|
|
|
|
@app = lambda do |env|
|
|
|
|
body = <<-HTML
|
|
|
|
<html><body>
|
|
|
|
<form action="/next" id="submit_me"><input type="submit" value="Submit" /></form>
|
|
|
|
<p id="change_me">Hello</p>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
var form = document.getElementById('submit_me');
|
|
|
|
form.addEventListener("submit", function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
setTimeout(function () {
|
|
|
|
document.getElementById("change_me").innerHTML = 'Good' + 'bye';
|
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body></html>
|
|
|
|
HTML
|
|
|
|
[200,
|
|
|
|
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
|
|
|
|
[body]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
@default_wait_time = Capybara.default_wait_time
|
|
|
|
Capybara.default_wait_time = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
after { Capybara.default_wait_time = @default_wait_time }
|
|
|
|
|
|
|
|
it "waits for a request to load" do
|
|
|
|
subject.visit("/")
|
|
|
|
subject.find_button("Submit").click
|
|
|
|
subject.should have_content("Goodbye");
|
|
|
|
end
|
|
|
|
end
|
2011-04-14 10:33:40 -04:00
|
|
|
|
|
|
|
context "simple app" do
|
|
|
|
before(:all) do
|
|
|
|
@app = lambda do |env|
|
|
|
|
body = <<-HTML
|
|
|
|
<html><body>
|
|
|
|
<strong>Hello</strong>
|
2011-04-19 05:07:42 -04:00
|
|
|
<span>UTF8文字列</span>
|
2011-04-19 07:51:38 -04:00
|
|
|
<input type="button" value="ボタン" />
|
2011-04-14 10:33:40 -04:00
|
|
|
</body></html>
|
|
|
|
HTML
|
|
|
|
[200,
|
2011-04-19 05:07:42 -04:00
|
|
|
{ 'Content-Type' => 'text/html; charset=UTF-8', 'Content-Length' => body.length.to_s },
|
2011-04-14 10:33:40 -04:00
|
|
|
[body]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-19 05:07:42 -04:00
|
|
|
before do
|
2011-04-14 10:33:40 -04:00
|
|
|
subject.visit("/")
|
2011-04-19 05:07:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "inspects nodes" do
|
2011-04-14 10:33:40 -04:00
|
|
|
subject.all(:xpath, "//strong").first.inspect.should include("strong")
|
|
|
|
end
|
2011-04-19 05:07:42 -04:00
|
|
|
|
2011-04-19 07:51:38 -04:00
|
|
|
it "can read utf8 string" do
|
2011-04-19 05:07:42 -04:00
|
|
|
utf8str = subject.all(:xpath, "//span").first.text
|
|
|
|
utf8str.should eq('UTF8文字列')
|
|
|
|
end
|
2011-04-19 07:51:38 -04:00
|
|
|
|
|
|
|
it "can click utf8 string" do
|
|
|
|
subject.click_button('ボタン')
|
|
|
|
end
|
2011-04-14 10:33:40 -04:00
|
|
|
end
|
2011-08-23 11:18:08 -04:00
|
|
|
|
2011-08-26 10:10:20 -04:00
|
|
|
context "response headers with status code" do
|
2011-08-23 02:58:20 -04:00
|
|
|
before(:all) do
|
|
|
|
@app = lambda do |env|
|
|
|
|
params = ::Rack::Utils.parse_query(env['QUERY_STRING'])
|
|
|
|
if params["img"] == "true"
|
2011-08-23 11:18:08 -04:00
|
|
|
body = 'not found'
|
|
|
|
return [404, { 'Content-Type' => 'image/gif', 'Content-Length' => body.length.to_s }, [body]]
|
2011-08-23 02:58:20 -04:00
|
|
|
end
|
|
|
|
body = <<-HTML
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<img src="?img=true">
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML
|
|
|
|
[200,
|
2011-08-26 10:10:20 -04:00
|
|
|
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s, 'X-Capybara' => 'WebKit'},
|
2011-08-23 02:58:20 -04:00
|
|
|
[body]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should get status code" do
|
|
|
|
subject.visit '/'
|
|
|
|
subject.status_code.should == 200
|
|
|
|
end
|
2011-08-26 09:58:31 -04:00
|
|
|
|
|
|
|
it "should reset status code" do
|
|
|
|
subject.visit '/'
|
|
|
|
subject.status_code.should == 200
|
|
|
|
subject.reset!
|
|
|
|
subject.status_code.should == 0
|
|
|
|
end
|
2011-08-26 10:10:20 -04:00
|
|
|
|
|
|
|
it "should get response headers" do
|
|
|
|
subject.visit '/'
|
|
|
|
subject.response_headers['X-Capybara'].should == 'WebKit'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should reset response headers" do
|
|
|
|
subject.visit '/'
|
|
|
|
subject.response_headers['X-Capybara'].should == 'WebKit'
|
|
|
|
subject.reset!
|
|
|
|
subject.response_headers['X-Capybara'].should == nil
|
|
|
|
end
|
2011-08-23 02:58:20 -04:00
|
|
|
end
|
2012-12-11 21:46:17 -05:00
|
|
|
|
|
|
|
context "slow iframe app" do
|
|
|
|
before do
|
|
|
|
@app = Class.new(ExampleApp) do
|
|
|
|
get '/' do
|
|
|
|
<<-HTML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<script>
|
|
|
|
function hang() {
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if(xhr.readyState == 4){
|
|
|
|
document.getElementById('p').innerText = 'finished'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xhr.open('GET', '/slow', true);
|
|
|
|
xhr.send();
|
|
|
|
document.getElementById("f").src = '/iframe';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<a href="#" onclick="hang()">Click Me!</a>
|
|
|
|
<iframe src="about:blank" id="f"></iframe>
|
|
|
|
<p id="p"></p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/slow' do
|
|
|
|
sleep 1
|
2012-12-11 23:48:30 -05:00
|
|
|
status 204
|
2012-12-11 21:46:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
get '/iframe' do
|
|
|
|
status 204
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not hang the server" do
|
|
|
|
subject.visit("/")
|
|
|
|
subject.click_link('Click Me!')
|
|
|
|
Capybara.using_wait_time(5) do
|
|
|
|
subject.should have_content("finished")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-14 10:16:56 -04:00
|
|
|
end
|