diff --git a/spec/driver_rendering_spec.rb b/spec/driver_rendering_spec.rb index c9ff9eb..f3794dc 100644 --- a/spec/driver_rendering_spec.rb +++ b/spec/driver_rendering_spec.rb @@ -6,18 +6,13 @@ describe Capybara::Webkit::Driver, "rendering an image" do include AppRunner let(:driver) do - driver_for_app do |env| - body = <<-HTML - - -

Hello World

- - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + + +

Hello World

+ + + HTML end before(:each) do diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 4167697..e806eaa 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -7,37 +7,37 @@ describe Capybara::Webkit::Driver do context "iframe app" do let(:driver) do - driver_for_app do |env| - params = ::Rack::Utils.parse_query(env['QUERY_STRING']) - if params["iframe"] == "true" - # We are in an iframe request. - p_id = "farewell" - msg = "goodbye" - iframe = nil - else - # We are not in an iframe request and need to make an iframe! - p_id = "greeting" - msg = "hello" - iframe = "" + driver_for_app do + get "/" do + if in_iframe_request? + p_id = "farewell" + msg = "goodbye" + iframe = nil + else + p_id = "greeting" + msg = "hello" + iframe = "" + end + <<-HTML + + + + + + #{iframe} + + + + HTML + end + + def in_iframe_request? + params[:iframe] == "true" end - body = <<-HTML - - - - - - #{iframe} - - - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] end end @@ -108,43 +108,37 @@ describe Capybara::Webkit::Driver do context "error iframe app" do let(:driver) do - driver_for_app do |env| - case env["PATH_INFO"] - when "/inner-not-found" - [404, {}, []] - when "/outer" - body = <<-HTML + driver_for_app do + get "/inner-not-found" do + invalid_response + end + + get "/" do + <<-HTML - + HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - else - body = "" - return [200, {'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s}, [body]] end end end - before { driver.visit("/") } - it "raises error whose message references the actual missing url" do - expect { driver.visit("/outer") }.to raise_error(Capybara::Webkit::InvalidResponseError, /inner-not-found/) + expect { driver.visit("/") }.to raise_error(Capybara::Webkit::InvalidResponseError, /inner-not-found/) end end context "redirect app" do let(:driver) do - driver_for_app do |env| - if env['PATH_INFO'] == '/target' - content_type = "

#{env['CONTENT_TYPE']}

" - [200, {"Content-Type" => "text/html", "Content-Length" => content_type.length.to_s}, [content_type]] - elsif env['PATH_INFO'] == '/form' - body = <<-HTML + driver_for_app do + get '/target' do + "

#{env['CONTENT_TYPE']}

" + end + + get '/form' do + <<-HTML
@@ -153,15 +147,14 @@ describe Capybara::Webkit::Driver do HTML - [200, {"Content-Type" => "text/html", "Content-Length" => body.length.to_s}, [body]] - else - [301, {"Location" => "/target"}, [""]] + end + + post '/redirect' do + redirect '/target' end end end - before { driver.visit("/") } - it "should redirect without content type" do driver.visit("/form") driver.find("//input").first.click @@ -183,9 +176,11 @@ describe Capybara::Webkit::Driver do context "css app" do let(:driver) do - body = "css" - driver_for_app do |env| - [200, {"Content-Type" => "text/css", "Content-Length" => body.length.to_s}, [body]] + driver_for_app do + get "/" do + headers "Content-Type" => "text/css" + "css" + end end end @@ -202,31 +197,26 @@ describe Capybara::Webkit::Driver do context "hello app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - - - - - -
Spaces not normalized 
-
-
Can't see me
-
- - - - - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + + + + + +
Spaces not normalized 
+
+
Can't see me
+
+ + + + + + HTML end before { driver.visit("/") } @@ -387,24 +377,19 @@ describe Capybara::Webkit::Driver do context "console messages app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - - - - - - - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + + + + + + + + HTML end before { driver.visit("/") } @@ -422,38 +407,33 @@ describe Capybara::Webkit::Driver do context "form app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - - - - - - - - - - - - -
- - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + +
+ + + + + + + + + + +
+ + HTML end before { driver.visit("/") } @@ -627,34 +607,28 @@ describe Capybara::Webkit::Driver do context "dom events" do let(:driver) do - driver_for_app do |env| - body = <<-HTML + driver_for_html(<<-HTML) + + Link + + - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + var elements = document.getElementsByClassName("watch"); + for (var i = 0; i < elements.length; i++) { + var element = elements[i]; + element.addEventListener("mousedown", recordEvent); + element.addEventListener("mouseup", recordEvent); + element.addEventListener("click", recordEvent); + } + + + HTML end before { driver.visit("/") } @@ -667,51 +641,46 @@ describe Capybara::Webkit::Driver do context "form events app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - -
- - - - - - - - - - -
- - - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + var elements = document.getElementsByClassName("watch"); + for (var i = 0; i < elements.length; i++) { + var element = elements[i]; + element.addEventListener("focus", recordEvent); + element.addEventListener("keydown", recordEvent); + element.addEventListener("keypress", recordEvent); + element.addEventListener("keyup", recordEvent); + element.addEventListener("input", recordEvent); + element.addEventListener("change", recordEvent); + element.addEventListener("blur", recordEvent); + element.addEventListener("mousedown", recordEvent); + element.addEventListener("mouseup", recordEvent); + element.addEventListener("click", recordEvent); + } + + + HTML end before { driver.visit("/") } @@ -749,43 +718,38 @@ describe Capybara::Webkit::Driver do context "mouse app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - -
Change me
-
Push me
-
Release me
-
- -
- - Next - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + +
Change me
+
Push me
+
Release me
+
+ +
+ + Next + + HTML end before { driver.visit("/") } @@ -826,19 +790,14 @@ describe Capybara::Webkit::Driver do context "nesting app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - -
-
Expected
-
-
Unexpected
- - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + +
+
Expected
+
+
Unexpected
+ + HTML end before { driver.visit("/") } @@ -850,42 +809,38 @@ describe Capybara::Webkit::Driver do end context "slow app" do - let(:driver) do - @result = "" - driver_for_app do |env| - if env["PATH_INFO"] == "/result" - sleep(0.5) - @result << "finished" - end - body = %{Go} - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end - end - - before { driver.visit("/") } - it "waits for a request to load" do + result = "" + driver = driver_for_app do + get "/result" do + sleep(0.5) + result << "finished" + "" + end + + get "/" do + %{Go} + end + end + driver.visit("/") driver.find("//a").first.click - @result.should == "finished" + result.should == "finished" end end context "error app" do let(:driver) do - driver_for_app do |env| - if env['PATH_INFO'] == "/error" - [404, {}, []] - else - body = <<-HTML + driver_for_app do + get "/error" do + invalid_response + end + + get "/" do + <<-HTML
HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] end end end @@ -908,21 +863,19 @@ describe Capybara::Webkit::Driver do context "slow error app" do let(:driver) do - driver_for_app do |env| - if env['PATH_INFO'] == "/error" - body = "error" + driver_for_app do + get "/error" do sleep(1) - [304, {}, []] - else - body = <<-HTML + invalid_response + end + + get "/" do + <<-HTML

hello

HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] end end end @@ -939,21 +892,20 @@ describe Capybara::Webkit::Driver do context "popup app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - - -

success

- - HTML - sleep(0.5) - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] + driver_for_app do + get "/" do + sleep(0.5) + return <<-HTML + + +

success

+ + HTML + end end end @@ -966,18 +918,17 @@ describe Capybara::Webkit::Driver do context "custom header" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - -

#{env['HTTP_USER_AGENT']}

-

#{env['HTTP_X_CAPYBARA_WEBKIT_HEADER']}

-

#{env['HTTP_ACCEPT']}

- / - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] + driver_for_app do + get "/" do + <<-HTML + +

#{env['HTTP_USER_AGENT']}

+

#{env['HTTP_X_CAPYBARA_WEBKIT_HEADER']}

+

#{env['HTTP_ACCEPT']}

+ / + + HTML + end end end @@ -1021,16 +972,11 @@ describe Capybara::Webkit::Driver do context "no response app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - -
- - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + +
+ + HTML end before { driver.visit("/") } @@ -1059,23 +1005,18 @@ describe Capybara::Webkit::Driver do context "custom font app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - - - - - -

Hello

- - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + + + + + +

Hello

+ + + HTML end before { driver.visit("/") } @@ -1091,21 +1032,15 @@ describe Capybara::Webkit::Driver do context "cookie-based app" do let(:driver) do - @cookie = 'cookie=abc; domain=127.0.0.1; path=/' - driver_for_app do |env| - request = ::Rack::Request.new(env) - - body = <<-HTML - - - - HTML - [200, - { 'Content-Type' => 'text/html; charset=UTF-8', - 'Content-Length' => body.length.to_s, - 'Set-Cookie' => @cookie, - }, - [body]] + driver_for_app do + get "/" do + headers 'Set-Cookie' => 'cookie=abc; domain=127.0.0.1; path=/' + <<-HTML + + + + HTML + end end end @@ -1122,7 +1057,7 @@ describe Capybara::Webkit::Driver do end it "uses a custom cookie" do - driver.browser.set_cookie @cookie + driver.browser.set_cookie 'cookie=abc; domain=127.0.0.1; path=/' driver.visit "/" echoed_cookie.should == "abc" end @@ -1151,18 +1086,13 @@ describe Capybara::Webkit::Driver do context "remove node app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - -
-

Hello

-
- - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + +
+

Hello

+
+ + HTML end before { driver.visit("/") } @@ -1185,48 +1115,43 @@ describe Capybara::Webkit::Driver do context "app with a lot of HTML tags" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - - - My eBook - - - - -
- - - - - - - - - -
ChapterPage
Intro1
Chapter 11
Chapter 21
-
+ driver_for_html(<<-HTML) + + + My eBook + + + + +
+ + + + + + + + + +
ChapterPage
Intro1
Chapter 11
Chapter 21
+
-

My first book

-

Written by me

-
-

Let's try out XPath

-

in capybara-webkit

-
+

My first book

+

Written by me

+
+

Let's try out XPath

+

in capybara-webkit

+
-

Chapter 1

-

This paragraph is fascinating.

-

But not as much as this one.

+

Chapter 1

+

This paragraph is fascinating.

+

But not as much as this one.

-

Chapter 2

-

Let's try if we can select this

- - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end +

Chapter 2

+

Let's try if we can select this

+ + + HTML end before { driver.visit("/") } @@ -1253,23 +1178,18 @@ describe Capybara::Webkit::Driver do context "css overflow app" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - - - - - -
Overflow
- - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html(<<-HTML) + + + + + +
Overflow
+ + + HTML end before { driver.visit("/") } @@ -1281,26 +1201,23 @@ describe Capybara::Webkit::Driver do context "javascript redirect app" do let(:driver) do - driver_for_app do |env| - if env['PATH_INFO'] == '/redirect' - body = <<-HTML + driver_for_app do + get '/redirect' do + <<-HTML HTML - else - body = "

finished

" end - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] + + get '/' do + "

finished

" + end end end - before { driver.visit("/") } - it "loads a page without error" do 10.times do driver.visit("/redirect") @@ -1311,28 +1228,23 @@ describe Capybara::Webkit::Driver do context "localStorage works" do let(:driver) do - driver_for_app do |env| - body = <<-HTML - - - - - - - HTML - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + if (localStorage.refreshCounter++ > 0) { + document.getElementById("output").innerHTML = "localStorage is enabled"; + } + } + + + + HTML end before { driver.visit("/") } @@ -1346,11 +1258,13 @@ describe Capybara::Webkit::Driver do context "form app with server-side handler" do let(:driver) do - driver_for_app do |env| - if env["REQUEST_METHOD"] == "POST" - body = "

Congrats!

" - else - body = <<-HTML + driver_for_app do + post "/" do + "

Congrats!

" + end + + get "/" do + <<-HTML Form @@ -1362,9 +1276,6 @@ describe Capybara::Webkit::Driver do HTML end - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] end end @@ -1376,31 +1287,34 @@ describe Capybara::Webkit::Driver do end end - def key_app_body(event) - body = <<-HTML - - Form - -
-
-
- - - - - HTML - body + def driver_for_key_body(event) + driver_for_app do + get "/" do + <<-HTML + + Form + +
+
+
+ + + + + HTML + end + end end def charCode_for(character) @@ -1419,12 +1333,7 @@ describe Capybara::Webkit::Driver do end context "keypress app" do - let(:driver) do - driver_for_app do |env| - body = key_app_body("keypress") - [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]] - end - end + let(:driver) { driver_for_key_body "keypress" } before { driver.visit("/") } @@ -1486,39 +1395,20 @@ describe Capybara::Webkit::Driver do end context "keydown app" do - let(:driver) do - driver_for_app do |env| - body = key_app_body("keydown") - [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]] - end - end - + let(:driver) { driver_for_key_body "keydown" } before { driver.visit("/") } - it_behaves_like "a keyupdown app" end context "keyup app" do - let(:driver) do - driver_for_app do |env| - body = key_app_body("keyup") - [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]] - end - end - + let(:driver) { driver_for_key_body "keyup" } before { driver.visit("/") } - it_behaves_like "a keyupdown app" end context "null byte app" do let(:driver) do - driver_for_app do |env| - body = "Hello\0World" - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] - end + driver_for_html("Hello\0World") end before { driver.visit("/") } @@ -1530,25 +1420,22 @@ describe Capybara::Webkit::Driver do context "javascript new window app" do let(:driver) do - driver_for_app do |env| - request = ::Rack::Request.new(env) - if request.path == '/new_window' - body = <<-HTML + driver_for_app do + get '/new_window' do + <<-HTML

bananas

HTML - else - params = request.params - sleep params['sleep'].to_i if params['sleep'] - body = "My New Window

finished

" end - [200, - { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, - [body]] + + get "/" do + sleep params['sleep'].to_i if params['sleep'] + "My New Window

finished

" + end end end @@ -1597,8 +1484,8 @@ describe Capybara::Webkit::Driver do end it "supports finding a window by url" do - driver.visit("/new_window") - driver.within_window(driver_url(driver, "/test?")) do + driver.visit("/new_window?test") + driver.within_window(driver_url(driver, "/?test")) do driver.find("//p").first.text.should == "finished" end end @@ -1622,47 +1509,41 @@ describe Capybara::Webkit::Driver do end end - context "javascript new window cookie app" do - let(:session_id) { '12345' } + it "preserves cookies across windows" do + session_id = '12345' + driver = driver_for_app do + get '/new_window' do + <<-HTML + + + + HTML + end - let(:driver) do - driver_for_app do |env| - request = ::Rack::Request.new(env) - response = ::Rack::Response.new - case request.path - when '/new_window' - response.write <<-HTML - - - - HTML - when '/set_cookie' - response.set_cookie('session_id', session_id) - end - response + get '/set_cookie' do + response.set_cookie 'session_id', session_id end end - before { driver.visit("/") } - - it "should preserve cookies across windows" do - driver.visit("/new_window") - driver.cookies['session_id'].should == session_id - end + driver.visit("/new_window") + driver.cookies['session_id'].should == session_id end context "timers app" do let(:driver) do - driver_for_app do |env| - case env["PATH_INFO"] - when "/success" - [200, {'Content-Type' => 'text/html'}, ['']] - when "/not-found" - [404, {}, []] - when "/outer" - body = <<-HTML + driver_for_app do + get "/success" do + '' + end + + get "/not-found" do + 404 + end + + get "/outer" do + <<-HTML